Jest Testing using NX affected
Introduction
For the consumer mode , once the application are setup, testing the created apps on every code changes is a key process.
Jest is a testing framework that NX also uses for unit testing of mostly javascript files. For more information about Nx test you can also visit https://nx.dev/l/a/cli/test or for Jest you can visit https://jestjs.io/.
Purpose
This is to be used to test code in development and during the build stages and also to reduce the amount of unnecessary builds. In addition it is to be used for reporting of the test coverage and setting the allowed percentages of permitted minimum coverage.
Setup
You will need the following to set it up
- projects - This has to do with the projects you want covered
- moduleDirectories - The specific directories you will like to be covered in testing
- coverageThreshold: - Set your % coverage level based on branches, functions, lines and statement
- collectCoverageFrom - Set up files types that you want to be covered
For an example you can view below in a file called jest.config.js on the root level
projects: [
'<rootDir>/apps/sample-react-micro-app',
'<rootDir>/libs/ui-kit',
'<rootDir>/apps/demo-react-app',
],
moduleDirectories: ['node_modules', 'src'],
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},
collectCoverageFrom: [
'!src/app/*.svg',
'!src/lib/*.*.tsx',
'!src/*.tsx',
'!*.js',
'!src/*.ts',
'!src/**/*.ts',
'!**/**/*.ts',
'!**/**/*.js',
'!**/**/*.*.tsx',
],
Running in Dev and Build stages
To tun the test set ups you will need this NX command and also add it to the Jenkinsfile for build dtages
nx affected:test --codeCoverage --jestConfig=jest.config.js