How to run Jest tests sequentially?

前端 未结 5 902
抹茶落季
抹茶落季 2020-12-12 15:42

I\'m running Jest tests via npm test. Jest runs tests in parallel by default. Is there any way to make the tests run sequentially?

I have some tests cal

相关标签:
5条回答
  • 2020-12-12 16:21

    Use the serial test runner:

    npm install jest-serial-runner --save-dev
    

    Set up jest to use it, e.g. in jest.config.js:

    module.exports = {
       ...,
       runner: 'jest-serial-runner'
    };
    

    You could use the project feature to apply it only to a subset of tests. See https://jestjs.io/docs/en/configuration#projects-arraystring--projectconfig

    0 讨论(0)
  • 2020-12-12 16:31

    I'm still getting familiar with Jest, but it appears that describe blocks run synchronously whereas test blocks run asynchronously. I'm running multiple describe blocks within an outer describe that looks something like this:

    describe
        describe
            test1
            test2
    
        describe
            test3
    

    In this case, test3 does not run until test2 is complete because test3 is in a describe block that follows the describe block that contains test2.

    0 讨论(0)
  • 2020-12-12 16:39

    CLI options are documented and also accessible by running the command jest --help.

    You'll see the option you are looking for : --runInBand.

    0 讨论(0)
  • 2020-12-12 16:40

    As copied from https://github.com/facebook/jest/issues/6194#issuecomment-419837314

    test.spec.js

    import { signuptests } from './signup'
    import { logintests } from './login'
    
    describe('Signup', signuptests)
    describe('Login', logintests)
    

    signup.js

    export const signuptests = () => {
         it('Should have login elements', () => {});
         it('Should Signup', () => {}});
    }
    

    login.js

    export const logintests = () => {
        it('Should Login', () => {}});
    }
    
    0 讨论(0)
  • 2020-12-12 16:45

    It worked for me ensuring sequential running of nicely separated to modules tests:

    1) Keep tests in separated files, but without spec/test in naming.

    |__testsToRunSequentially.test.js
    |__tests
       |__testSuite1.js
       |__testSuite2.js
       |__index.js
    

    2) File with test suite also should look like this (testSuite1.js):

    export const testSuite1 = () => describe(/*your suite inside*/)
    

    3) Import them to testToRunSequentially.test.js and run with --runInBand:

    import { testSuite1, testSuite2 } from './tests'
    
    describe('sequentially run tests', () => {
       testSuite1()
       testSuite2()
    })
    
    0 讨论(0)
提交回复
热议问题