@nrwl/nx with node libraries and coverage using their CLI tools

Viewed 2106

I'm finding it difficult to segregate a testing library that has code coverage for the library under test in with nx when running their cli commands. The goal is to have that within a build process and commit process to evaluate the coverage.

I've got a set up like so (just node, nothing else... well, jest too)

libs
 \--utils
     \-- jest.config.js
 \--utils-test
     \-- jest.config.js

In utils-test it references utils correctly, and I can run tests, however when I start trying to aim for coverage, it’s a an amazing 0%, while when I use wallabyjs I see I have coverage around 98%.

The results are when I run the command nx affected:test --all --codeCoverage --skip-nx-cache

> superlibs@0.0.0 test:all /Users/me/git/libs
> nx affected:test --all --codeCoverage --skip-nx-cache


>  NX   WARNING  Running affected:* commands with --all can result in very slow builds.

  --all is not meant to be used for any sizable project or to be used in CI.
  
  Learn more about checking only what is affected: https://nx.dev/latest/angular/cli/affected#affected.


>  NX  Running target test for projects:

  - utils-test
  
  With flags:
    --codeCoverage=true

———————————————————————————————————————————————

> nx run utils-test:test --codeCoverage 
 PASS   utils-test  libs/utils-test/src/lib/models/countries.test.ts
 PASS   utils-test  libs/utils-test/src/lib/models/states.test.ts
 PASS   utils-test  libs/utils-test/src/lib/randomizers/boolean.test.ts
 PASS   utils-test  libs/utils-test/src/lib/randomizers/numbers.test.ts
 PASS   utils-test  libs/utils-test/src/lib/randomizers/arrays.test.ts
 PASS   utils-test  libs/utils-test/src/lib/randomizers/strings.test.ts
----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |                   
----------|---------|----------|---------|---------|-------------------

Test Suites: 6 passed, 6 total
Tests:       25 passed, 25 total
Snapshots:   0 total
Time:        1.753 s, estimated 2 s
Ran all test suites.

———————————————————————————————————————————————

>  NX   SUCCESS  Running target "test" succeeded

I honestly cannot figure out how to approach the utils-test\jest.config.js. Currently I have for that file:

module.exports = {
  displayName: 'utils-test',
  preset: '../../jest.preset.js',
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
    },
  },
  transform: {
    '^.+\\.[tj]sx?$': 'ts-jest',
  },
  coverageDirectory: '../../coverage/libs/utils-test'
};

Equally I have for the utils\jest.config.js a minimal file (guys at wallabyjs.com helped me with this part):

module.exports = {
  displayName: 'utils',
  preset: '../../jest.preset.js',
};

from the root, I have this jest.config.js file:

module.exports = {
  preset: './jest.preset.js',
  projects: [
    '<rootDir>/libs/utils',
    '<rootDir>/libs/utils-test'
  ],
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.base.json',
    }
  },
  coverageDirectory: './coverage/all'
};

and finally the preset file (which is also in the root):

const nxPreset = require('@nrwl/jest/preset');

module.exports = {
  ...nxPreset,
  testEnvironment: 'node',
  moduleFileExtensions: ['js', 'ts'],
  testMatch: ['**/?(*.)+(spec|test).[jt]s?(x)'],
  transform: {
    '^.+\\.(ts)$': 'ts-jest'
  },
  coverageReporters: ['text'],
  coverageThreshold: {
    global: {
      branches: 90,
      functions: 90,
      lines: 90,
      statements: 90
    }
  }
};

How do you all set up isolated jest tests libraries with coverage?

0 Answers
Related