NPM jest-resolve Package
Usage
Installation
npm install --save-dev jest-resolve
Basic Usage
const { resolve } = require('jest-resolve');
resolve('path/to/module'); // returns the resolved path to the module
Configuration Options
| Option | Description | Default |
|—|—|—|
| rootDir
| The root directory of the project. | The current working directory. |
| extensions
| The list of file extensions to resolve. | [‘.js’, ‘.jsx’, ‘.ts’, ‘.tsx’, ‘.json’] |
| moduleDirectory
| The list of directories to search for modules in. | [‘node_modules’] |
| moduleNameMapper
| A map of module names to their resolved paths. | {} |
| alias
| A map of aliases to their resolved paths. | {} |
| paths
| A map of paths to their resolved directories. | {} |
| symlinks
| Whether to resolve symlinks. | false |
| preserveSymlinks
| Whether to preserve symlinks in the resolved path. | false |
| skipFilter
| Whether to skip filtering out node_modules directories. | false |
Example:
const { resolve } = require('jest-resolve');
resolve('path/to/module', {
rootDir: '/path/to/project',
extensions: ['.js', '.jsx'],
moduleDirectory: ['node_modules', 'lib'],
moduleNameMapper: {
'^path/to/(.*)$': '<rootDir>/$1'
}
});
Best Practices
- Use the
rootDir
option to specify the root directory of your project. This will help to resolve modules relative to your project root. - Use the
extensions
option to specify the list of file extensions to resolve. This will help to resolve modules with different file extensions. - Use the
moduleDirectory
option to specify the list of directories to search for modules in. This will help to resolve modules that are installed in non-standard locations. - Use the
moduleNameMapper
option to map module names to their resolved paths. This can be used to resolve modules with aliases or to map modules to different directories. - Use the
alias
option to map aliases to their resolved paths. This can be used to create shortcuts for commonly used modules or to resolve modules with long paths. - Use the
paths
option to map paths to their resolved directories. This can be used to resolve modules that are located in non-standard locations.
Implementation Guide
The jest-resolve
package is a wrapper around the resolve
function from the path
module. It provides a number of additional features that are useful for resolving modules in a Jest test environment.
The resolve
function takes two arguments:
path
: The path to the module to resolve.options
: An optional object of configuration options.
The resolve
function returns the resolved path to the module. If the module cannot be resolved, the function will throw an error.
The options
object can contain the following properties:
rootDir
: The root directory of the project.extensions
: The list of file extensions to resolve.moduleDirectory
: The list of directories to search for modules in.moduleNameMapper
: A map of module names to their resolved paths.alias
: A map of aliases to their resolved paths.paths
: A map of paths to their resolved directories.symlinks
: Whether to resolve symlinks.preserveSymlinks
: Whether to preserve symlinks in the resolved path.skipFilter
: Whether to skip filtering out node_modules directories.
The rootDir
option is used to specify the root directory of the project. This will help to resolve modules relative to your project root.
The extensions
option is used to specify the list of file extensions to resolve. This will help to resolve modules with different file extensions.
The moduleDirectory
option is used to specify the list of directories to search for modules in. This will help to resolve modules that are installed in non-standard locations.
The moduleNameMapper
option is used to map module names to their resolved paths. This can be used to resolve modules with aliases or to map modules to different directories.
The alias
option is used to map aliases to their resolved paths. This can be used to create shortcuts for commonly used modules or to resolve modules with long paths.
The paths
option is used to map paths to their resolved directories. This can be used to resolve modules that are located in non-standard locations.
The symlinks
option is used to specify whether to resolve symlinks. If true
, symlinks will be resolved. If false
, symlinks will not be resolved.
The preserveSymlinks
option is used to specify whether to preserve symlinks in the resolved path. If true
, symlinks will be preserved in the resolved path. If false
, symlinks will be resolved to their target paths.
The skipFilter
option is used to specify whether to skip filtering out node_modules directories. If true
, node_modules directories will not be filtered out when resolving modules. If false
, node_modules directories will be filtered out when resolving modules.