Jono Job
Jono Job

Reputation: 3038

Sequential test scenarios for Jest

I've been starting to use create-react-app for my React projects, and as a testing library it comes with Jest.

As part of my React apps, I like to create integration tests (as well as unit tests) as I find it useful to be able to check the happy paths in the app are working as expected. For example: Render (mount) a page using Enzyme, but only mock out the http calls (using Sinon) so that the full React/Redux flow is exercised at once.

Before using create-react-app, I've used Mocha for testing, and I found mocha-steps to be a great extension for integration tests, as it allows tests in a group to be executed in sequence, and handles stopping if a step fails without stopping the entire test run.

Question: Is there any way to get Jest to behave in a similar way? Specifically, I'd like to be able to specify a series of tests in a group (such as in a describe) and have them execute sequentially in order.

I've been looking through the Jest docs, or for any other libraries that extend it, but I've come up empty. For now, it feels like the only option is to have one large test which makes me sad, and I'd prefer not to swap out Jest for Mocha if I can avoid it.

Thanks!

Upvotes: 2

Views: 2496

Answers (1)

Noel Llevares
Noel Llevares

Reputation: 16037

jest does not currently support flagging specific tests to be run serially (like ava's test.serial).

This has been requested before but I don't think they are working on it.

The workaround is for you to identify which test files are to be run concurrently and which ones are to be run serially.

For example, I usually name my unit tests with *.test.js and my integration tests with *.spec.js.

I can then run my unit tests concurrently.

$ jest '(/__tests__/.*\\.test)\\.js$'

And I can run my integration tests serially.

$ jest '(/__tests__/.*\\.spec)\\.ts$' --runInBand

I can combine the two in my package.json.

"scripts": {
    "unit": "jest '(/__tests__/.*\\.test)\\.js$'",
    "integration": "jest '(/__tests__/.*\\.spec)\\.ts$' --runInBand",
    "test": "npm run unit && npm run integration"
}

Running npm test can then run concurrent tests first and then the serial ones next.

Upvotes: 5

Related Questions