Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate unit and integration/long running tests on Jest?

Tags:

jestjs

Currently, I have two folders: __tests__ for unit (fast) tests and __integration__ for slow tests.

Then, in package.json:

{
  "scripts": {
    "test": "jest",
    "test:integration": "jest -c '{}'",
    ...
  },
  "jest": {
    "testPathIgnorePatterns": ["/node_modules/", "__integration__"]
  }
}

So, when I want to do TDD, I'm running just npm test and when I want to test the entire project, npm run test:integration.

As Jest is offered as a "no configuration" test framework, I was thinking if there's a better (or proper) way to configure this.

Thank you.

like image 415
geovanisouza92 Avatar asked Feb 17 '17 17:02

geovanisouza92


People also ask

How do you separate unit tests and integration tests?

Starting with the previous structure project, the way to split the Unit Tests (ut) and Integration Tests (it) is to create a specific and dedicated package. For example it package, for integration tests and ut for unit tests. If you want to avoid previous undesirable effects, you have to configure some plugins.

Why do we separate units and integration tests?

Separating things Usually unit tests are fast and even if you run them by mistake, you won't feel uncomfortable. Integration, contract and acceptance tests can take several minutes if the project is huge. That's why it makes sense to separate integration and unit tests.

Can Jest run integration tests?

In general, Jest can run seven integration tests in parallel. The biggest impact on the duration for integration tests is the starting of the different docker containers.

Is it okay to skip integration testing?

The short answer is yes. For software to work properly, all units should integrate and perform as they're expected to. To ensure this is the case, you will need to perform integration tests.


2 Answers

Quoting from this post.

You can try name files like:

index.unit.test.js and api.int.test.js

And with Jest’s pattern matching feature, it makes it simple to run them separately as well. For unit testing run jest unit and for integration testing run jest int.

File structure/location you can define based on your preferences as the pattern matching based on the file name is how jest knows what to run.

Also see jest cli documentation about npm scripts:

If you run Jest via npm test, you can still use the command line arguments by inserting a -- between npm test and the Jest arguments

like image 84
RationalDev likes GoFundMonica Avatar answered Nov 06 '22 05:11

RationalDev likes GoFundMonica


Have you tried jest --watch for TDD? It runs only files related to your git changes, runs errors first and heavily utilise cache for speed.

Other than that, jest -c accepts a path, not a string. You should be good with jest -c jest-integration-config.json, provided that jest-integration-config.json sits in your project's root.

like image 30
Michał Pierzchała Avatar answered Nov 06 '22 07:11

Michał Pierzchała