This article shows you how to use Jest to run TS single tests without compiling, and increase single test coverage to avoid under-testing or code corruption. If you encounter problems, refer to Jest Test ESM
Start the configuration
You can select the following manual configuration mode if you are worried about one-click configuration on the CLI
CLI One-click configuration
TS
$ tnpx create-app-test --type ts
Copy the code
JS
$ tnpx create-app-test --type js
Copy the code
Note that coverage is automatically set to 100%, which can be customized by adding the parameter — Coverage =80
Manual configuration
Install dependencies
npm i -D jest typescript ts-jest @types/jest
Copy the code
Generate the jest. Config. Js
npx ts-jest config:init
Copy the code
Run a single test
npm test
Copy the code
Single test sample
The single test writing specification comes from the single test section of applets best Practices 👮.
import { isFunction, isPromise, values } from '.. /src/lib/lite-lodash'
describe('lite-lodash'.() = > {
describe('isPromise'.() = > {
it('should Promise.resolve be a promise'.() = > {
const input = Promise.resolve();
const actual = isPromise(input);
const expected = true;
expect(actual).toEqual(expected);
});
it('should new Promise be a promise'.() = > {
const input = new Promise(() = > {});
const actual = isPromise(input);
const expected = true;
expect(actual).toEqual(expected);
});
});
});
Copy the code
Running results:
IsPromise ✓ shouldPromiseResolve be a promise ✓ shouldnew Promise be a promise (1 ms)
Copy the code
Coverage reporting and threshold Settings
Thresholds are set to ensure that code quality does not deteriorate with iteration.
Before running the command, ensure that the previous code is committed. Because the configuration file will be overwritten, or jump directly to the jest.config.js section below.
Setting coverage
npx jest --init
Copy the code
➤ It seems that you already have a jest configuration, do you want to override It? … yes
The following questions will help Jest to create a suitable configuration for your project
➤ Would you like to use Typescript for the configuration file? … no
➤ Choose the test environment that will be used for testing › node
➤ ➤ Do you want Jest to add coverage reports? … yes
➤ Which provider should be used to instrument code for coverage? Holds the v8
➤ ➤ Automatically clear mock calls and instances between every test? … no
📝 Configuration file created at jest.config.js
jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: "node",
+ coverageDirectory: "coverage",
+ coverageProvider: "v8",
+ coverageThreshold: {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
},
}
Copy the code
package.json
"scripts": {
"build": "tsc --declaration",
"preversion": "npm run build",
- "test": "jest"
+ "test": "jest --coverage"
},
Copy the code
.gitignore
Coverage reports do not need to be saved to the code base. For details on how to read coverage reports, see writing an article.
+coverage/
Copy the code
Generating coverage reports
npm test
----------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
lite-lodash.ts | 100 | 100 | 100 | 100 |
----------------|---------|----------|---------|---------|-------------------
Copy the code
Deliberately under-test functions to see if they fail
----------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------------|---------|----------|---------|---------|-------------------
All files | 72.73 | 100 | 66.67 | 72.73 |
lite-lodash.ts | 72.73 | 100 | 66.67 | 72.73 | 5-7
----------------|---------|----------|---------|---------|-------------------
Jest: "global" coverage threshold for statements (100%) not met: 72.73%
Jest: "global" coverage threshold for lines (100%) not met: 72.73%
Jest: "global" coverage threshold for functions (100%) not met: 66.67%
npm ERR! code 1
Copy the code
Single test failure Exit code is not equal to 0, resulting in CI failure. The problem of preventing code undertesting or deterioration with coverage is achieved 👍.
reference
- ts-jest
TODO
- Cli One-click configuration