Run Jest test suites in groups

后端 未结 3 923
花落未央
花落未央 2021-01-04 11:59

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:

Test com

3条回答
  •  一个人的身影
    2021-01-04 12:43

    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 environment
    • ProdTest for testing in production environemnt

    If 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.

提交回复
热议问题