Install
1
npm install --save-dev @testing-library/dom
After installation, the setupTests.js file will be generated under src.
unit test file location
The unit test file can be placed in the same folder as the text file, named text.test.js. In fact, as long as it is consistent with the text.
In CourtCanva, the project is to keep all the test files together. This way is to create a
__tests__
folder under src. Sometimes test files are written directly under src, but in most cases they are written in the__tests__
folder.
Check which query should be used for test
For testing, just getbyRole(button, name: text in html). or data-testid={}. Everything that starts with data is useless to html, only for test. But the test can get the element with screen.getByTestId(“id”) . Or use the website to find test elements Testing Playground online , Testing Playground plugin to download the plugin. Websites are also available.
Configuration test file
The configuration of the test file is in jest.config.js in the root directory.
Course can use the configuration package written by Kent C. Dodds to Simple configuration like
1
2
3
4
5
6const config = require('kcd-scripts/jest')
module.exports = {
...config
ConverageThreshold: {},
}
Simple summary of the test
Basic common verification writing