npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

resolve-uit-path

v1.1.2

Published

Resolves the path from your spec file at __filename to the corresponding unit-in-test's file

Downloads

11

Readme

Resolve UIT Path

TL;DR

:point_right: :scream: Node Native:

const unitInTest = require( '../../../../../../../../lib/path/back/to/the/module/with/the/unitInTest')

:point_right: :relieved: Babel Namespaces:

const unitInTest = require( '<lib>/path/back/to/the/module/with/the/unitInTest')

:point_right: :smiley: This here module:

const unitInTest = require( resolveUitPath( __filename ) )

Usage

If you define your test files within a spec or test folder at a path that matches your unit in test, you can use this module to get the path to the module to be tested.

For example, if your folder structure looked like this:

project/
├── lib
│   └── path
│       └── back
│           └── to
│               └── the
│                   └── module
│                       └── with
│                           └── the
│                               └── unitInTest
│                                   └── index.js
└── spec
    └── lib
        └── path
            └── back
                └── to
                    └── the
                        └── module
                            └── with
                                └── the
                                    └── unitInTest
                                        └── index.test.js

you would do something like this with relative paths:

const unitInTest = require( '../../../../../../../../lib/path/back/to/the/module/with/the/unitInTest')

or something like this with Babel Namespaces:

const unitInTest = require( '<lib>/path/back/to/the/module/with/the/unitInTest')

This might be fine once or twice, but the more you deal with highly modular code, the more tedious this gets.

Using this little library, you can do this instead in your test file

// file: spec/lib/path/back/to/the/module/with/the/unitInTest/index.test.js

const resolveUitPath = require( 'resolve-uit-path' )();
const uitPath = resolveUitPath( __filename );

describe( `The ${ uitPath } function`, ()=>{

  it( 'should throw if called without arguments', ()=>{

    const uit = require( uitPath );
    expect( uit ).to.throw();

  });

} );

Configuration

The module allows you to configure, which type of extension you want to use for your test files. It defaults to .test.js, but you can change that by instantiating it with an alternative specFileExtension.

For example, if you used the -spec.js extension instead of .test.js, you would configure the module as shown below:


// file: spec/lib/path/back/to/the/module/with/the/unitInTest/index-spec.js

const resolveUitPath = require( 'resolve-uit-path' )( { specFileExtension: '-spec.js'});
const uitPath = resolveUitPath( __filename );

describe( `The ${ uitPath } function`, ()=>{

  it( 'should throw if called without arguments', ()=>{

    const uit = require( uitPath );
    expect( uit ).to.throw();

  });

} );

You can also configure the name of your spec folder:


// file: tests/lib/path/back/to/the/module/with/the/unitInTest/index.test.js

const resolveUitPath = require( 'resolve-uit-path' )( { specDirName: 'tests'});
const uitPath = resolveUitPath( __filename );

describe( `The ${ uitPath } function`, ()=>{

  it( 'should throw if called without arguments', ()=>{

    const uit = require( uitPath );
    expect( uit ).to.throw();

  });

} );

Root Folder

The library assumes that the level-difference between the spec and the uit is 1, meaning that you can store your test in spec, tests, specFiles, etc, but within the spec folder itself, the path must match.

:white_check_mark: will work:

project
├── lib
│   └── path
└── spec
    └── lib
        └── path

:white_check_mark: will work:

project/
├── lib
│   └── path
└── tests
    └── lib
        └── path

:white_check_mark: will work for all spec folders

project
├── lib
│   ├── common
│   │   └── path
│   └── packages
│       ├── package-a
│       │   ├── lib
│       │   │   └── path
│       │   └── spec
│       │       └── lib
│       │           └── path
│       └── package-b
│           └── lib
│               └── path
└── spec
    ├── lib
    │   └── common
    │       └── path
    └── packages
        └── package-b
            └── lib
                └── path

:x: will not work

project
├── lib
│   └── path
└── spec
    ├── integration
    │   └── lib
    │       └── path
    └── unit
        └── lib
            └── path