Error getting coverage report using rewire / cross-env

I am trying to get a coverage report using nyc and it works well if I don't use the cross-env plugin.

cross-env NODE_ENV=test nyc mocha --ui bdd --reporter spec --colors --require babel-core/register tests --recursive

By executing this command, code coverage will not work properly, and the result will be as follows:

 31 passing (1s)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |        0 |        0 |        0 |        0 |                   |
----------|----------|----------|----------|----------|-------------------|

However, if I run this one nyc mocha --ui bdd --reporter spec --colors --require babel-core/register tests --recursive, it will work as expected.

---------------------|----------|----------|----------|----------|-------------------|
File                 |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------------|----------|----------|----------|----------|-------------------|
All files            |    79.45 |    64.29 |    35.71 |    84.62 |                   |
 constants           |      100 |      100 |      100 |      100 |                   |
  index.js           |      100 |      100 |      100 |      100 |                   |
 db                  |    77.05 |    64.29 |    33.33 |    83.02 |                   |
---------------------|----------|----------|----------|----------|-------------------|

The problem is that I need to set the env variable in order to use the rewire plugin, which allows me to run the test correctly (indeed, most of the test does not work because of this).

This is my .bebelrc

{
  "presets": ["es2015"],
  "env": {
    "test": {
      "plugins": ["istanbul", "babel-plugin-rewire"]
    }
  }
}

note . I think the problem is with the babel plugin plugin. Indeed, even deleting cross-env and placing the collection of plugins in the root will give me the same empty coverage result.

+4
2

, babel . , env. , readme, .

rewire/cross-env

( -env) :

npm run test

,

npm run test:cov

test script, ,

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |        0 |        0 |        0 |        0 |                   |
----------|----------|----------|----------|----------|-------------------|

rewire/cross-env

cross-env NODE_ENV=test script . , , , .

npm run test2

script cross-env, babel rewire .

npm run test:cov2

test2 script . .

------------------|----------|----------|----------|----------|-------------------|
File              |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
------------------|----------|----------|----------|----------|-------------------|
All files         |    64.29 |      100 |    54.55 |    66.67 |                   |
 src              |       75 |      100 |       60 |    85.71 |                   |
  a-dependency.js |      100 |      100 |      100 |      100 |                   |
  a-module.js     |       60 |      100 |        0 |       75 |                 6 |
 tests            |       60 |      100 |       50 |       60 |                   |
  test.js         |       60 |      100 |       50 |       60 |... 23,24,26,27,28 |
------------------|----------|----------|----------|----------|-------------------|

- rewire.

babelrc (es2015)

{
  "presets": ["es2015"],
  "env": {
    "test": {
      "plugins": ["istanbul", "rewire"]
    }
  }
}

babel, . rewire npm run test:cov, npm run test:cov2 ( cross-env).

babelrc

{
  "presets": ["es2015"],
  "plugins": ["istanbul", "rewire"]
}

npm run test:cov ( -env) npm run test:cov2 ( -env). .

{
  "presets": ["es2015"]
}

( -env) (, )

istanbul , ( )

------------------|----------|----------|----------|----------|-------------------|
File              |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
------------------|----------|----------|----------|----------|-------------------|
All files         |      100 |      100 |      100 |      100 |                   |
 src              |      100 |      100 |      100 |      100 |                   |
  a-dependency.js |      100 |      100 |      100 |      100 |                   |
  a-module.js     |      100 |      100 |      100 |      100 |                   |
 tests            |      100 |      100 |      100 |      100 |                   |
  test.js         |      100 |      100 |      100 |      100 |                   |
------------------|----------|----------|----------|----------|-------------------|

babelrc (env)

es2015, env. npm install babel-preset-env --save-dev, babelrc .

{
  "presets": [
    ["env", {
      "targets": {
        "node": "current"
      }
    }]
  ],
  "env": {
    "test": {
      "plugins": ["istanbul", "rewire"]
    }
  }
}

npm run test , npm run test:cov . .

- , -env , babel es2015. istanbul babel rewire ( cross-env).

env babel, ( )

> node --version
v9.4.0
> npm --version
5.6.0
>ver
Microsoft Windows [Version 10.0.16299.309]

MacOS

+2

, - . , . jest --coverage (^ 24.8.0) (^ 4.0.1).

:

const rewire = require('rewire');
const source = rewire('/js/source.js');
require('/js/source.js'); <--------------------------------THIS LINE

describe('Test hello function - hello()', function () {
    const hello = source.__get__('hello');

    it('should print out the word hello', function () {
        expect(hello()).toBe('hello');
    });
});
0

Source: https://habr.com/ru/post/1695343/


All Articles