New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

resolve-uit-path

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

resolve-uit-path

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

  • 1.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-84.62%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 26 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc