18

Using unit and integration tests in Jest - DEV

 3 years ago
source link: https://dev.to/jalal246/using-jest-puppeteer-with-monorepo-3kj2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Cover image for Using unit and integration tests in Jest

Using unit and integration tests in Jest

Jun 27Updated on Jul 14, 2020

・2 min read

I was trying to use jest-puppeteer with monorepo project i'm working on in roadmap aiming to add integration tests using puppeteer.

And honestly, I didn't have any idea how to load different jest.config.js for different packages I already have in the monorepo.


In case anyone stuck in the same problem as me. The simple solution is to separate the unit test from an integration test. Obvious? didn't seem so for me.

This "separation" should be reflected in the project structure, config files, and running scripts.

Of course, the main problem I faced here, is dealing with the switching environment. I use testEnvironment: "jsdom" and to run puppeteer you have to remove any testing environment and use preset: "jest-puppeteer".

Lesson 1: You can't run all tests at once.
Lesson 2: It doesn't matter if you are using monorepo or not.

So, I ended up with the following structure:

myProj1/
  src/
  test/
    integration/
      myFrist.test.js
    foo.test.js
    bar.test.js
  package.json

myProj2/
  ...
myProj3/
  ...
jest.config.js
jest.integration.config.js
package.json

What's in config files?

Well, not much at all.

In jest.config.js, the main goal is to exclude the integration test.

module.exports = {
  testEnvironment: "jsdom",
  testPathIgnorePatterns: ["test/integration"],
};

Inside jest.integration.config.js, make sure to test only the integration files.

module.exports = {
  preset: "jest-puppeteer",
  testRegex: "/integration/",
};

And finally inside root package.json:

"scripts": {
   "test:unit": "jest",
   "test:e2e": "jest -c jest.integration.config.js",
   "test": "yarn test:unit && yarn test:e2e",
 },

Using jest -c to pass a custom config for Jest. Since I have jest.config.js for the unit test there's no need to pass one, Jest will automatically read it. But that's not the case for test:e2e.

Eventually, the test script will run both integration and unit tests.

show test

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK