Map Paths for Node Modules for Unit Testing

Client side I delete module paths using SystemJS, for example

var systemJsConfig = { baseURL: "./", defaultJSExtensions: true, map: { 'root-components': 'applicationRoot/rootComponents' } }; 

and therefore require('root-components/foo'); will be displayed in applicationRoot/rootComponents/foo .

The problem is that if I run the module with require('root-components/foo'); in Mocha, Node has no idea what this path means. Is there any reasonable way to do this path mapping in Node?

Could this be a proxyquire? I read their documents, but did not find anything to indicate what it was.

It’s easy for unit testing, so I’m happy with any solution that uses some of its own party utility.

+6
source share
3 answers

If your only requirement is so simple, you can make a utility function that overrides require .

The new query requires matching with the path argument, searches for the module in the new path, and you can optionally backtrack for modules that do not exist in the mapped path. The code should look like this:

 const oldRequire = require; require = (path) => { try { const mapped = oldRequire(path.replace('root-components/foo', 'applicationRoot/rootComponents/foo')); if (!mapped) { throw new Error('module not found in mapped directory'); } console.log('module resolved with mapping', path); return mapped; } catch (e) { console.log('using old require without mapped path', path); return oldRequire(path); } }; 
+5
source

You can do modular anti-aliasing in many different ways as described in this value , but an easier way is to simply set the initial root path with NODE_PATH and from there:

Here's what it looks like with the NODE_PATH environment NODE_PATH :

NODE_PATH=./src node src/server/index.js

Then, in all your files, no matter where they are in the hierarchy, it is resolved from the ./src directory. This eliminates the need for aliases as you describe, but I understand that in your case, you may need to move / rename many files.

Example: require('root-components/foo'); => ./src/root-components/foo.js

+1
source

Option 1 - Change NODE_PATH (not recommended):

Modify NODE_PATH to include the path to the module in the shell before running node.js.

 exports NODE_PATH=./path/to/module:$NODE_PATH 

This is not a great option, because it requires a step before starting and - since NODE_PATH contains many paths - it does not always NODE_PATH where the module is loading, and there is a possibility of name conflicts.

Option 2 - Move the module to an external repo

Suppose you move components to a separate rootcomponents repo resource, available on your GitHub profile.

Then you can install it directly through:

 npm install --save github:arackaf/rootcomponents 

Then you should be able to map the project source to the alias System.js.

 var systemJsConfig = { baseURL: "./", defaultJSExtensions: true, map: { 'root-components': 'github:arackaf/rootcomponents' } }; 

From there, it should work as you expected:

 require('root-components/foo'); 

Option 3 - Download the module through the relative path:

The config.map parameter config.map intended only for matching external dependencies with aliases.

One simple alternative is to provide a relative path. Sibling paths are based on the URL.

For example, if you are trying to download:

 src/rootComponents/foo.js 

Demand:

 require('./src/rootComponents/foo') 

Note. All of this assumes that require() statements follow System.js patterns / rules.

Another possible option is to provide the System.paths[] parameter, which creates an alias for the local path. I can’t check how this works (i.e. I have never tried), but can be found here here

+1
source

All Articles