Nuclear Power Manager

    resolve-uit-path

    1.1.2 • Public • Published

    Resolve UIT Path

    TL;DR

    👉 😱 Node Native:

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

    👉 😌 Babel Namespaces:

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

    👉 😃 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.

    will work:

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

    will work:

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

    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

    will not work

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

    Install

    npm i resolve-uit-path

    DownloadsWeekly Downloads

    92

    Version

    1.1.2

    License

    MIT

    Unpacked Size

    11.3 kB

    Total Files

    7

    Last publish

    Collaborators

    • noisygerman