Skip to content

Files required through node --eval or node --require do not get instrumented #144

@ehmicky

Description

@ehmicky

Demo repository

Expected Behavior

Files required through node --eval or node --require should be instrumented.

Observed Behavior

They do not get instrumented.

The demo repository contains two empty files index.js and other.js.

Running the file instrument it correctly:

$ nyc node index.js 
----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |        0 |        0 |        0 |        0 |                   |
 index.js |        0 |        0 |        0 |        0 |                   |
----------|----------|----------|----------|----------|-------------------|

Requiring it through --eval does not instrument it:

$ nyc node -e 'require("./index")'
----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |        0 |        0 |        0 |        0 |                   |
----------|----------|----------|----------|----------|-------------------|

Requiring it through --require does not instrument it:

$ nyc node -r ./index.js other.js 
----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |        0 |        0 |        0 |        0 |                   |
 other.js |        0 |        0 |        0 |        0 |                   |
----------|----------|----------|----------|----------|-------------------|

Forensic Information

Operating System: Ubuntu 18.04
Environment Information: node v11.3.0, npm 6.4.1, nyc 13.1.0

Notes

nyc has a --require flag. However this only works when node is fired as the top-level command, not as a child process.

For example I am testing a library that has a register.js file that performs monkey patching on load (like @babel/register does). The only way to test it without polluting the global environment of the other tests is to run it as a child process. I.e. my unit test runs childProcess.spawn() using --require register.js.

I can work around the problem and avoid --eval or --require by loading a file that fires require() instead, but there might be other use cases that this limitation of nyc might impact.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions