I am writing extensive tests for a new API via Jest and supertest. Prior to running the tests, I am setting up a test database and populating it with users:
I have done it with the --testNamePattern flag. Here is the procedure.
Let’s say that you have two groups of tests:
DevTest for testing in development environmentProdTest for testing in production environemntIf you want to test only those features required for the development environment, you have to add DevTest to the test description:
describe('(DevTest): Test in development environment', () => {
// Your test
})
describe('(ProdTest): Test in production environment', () => {
// Your test
})
describe('Test everywhere', () => {
// Your test
})
After that, you can add commands to your package.json file:
"scripts": {
"test": "jest",
"test:prod": "jest --testNamePattern=ProdTest",
"test:dev": "jest --testNamePattern=DevTest",
"test:update": "jest --updateSnapshot"
}
Command npm test will run all of your tests, since you are not using flag --testNamePattern. If you want to run other tests, just use npm run test:dev for example.
Be careful when naming your test groups though. They are searched with regex in test description and you don't want this command to match other words.