DEV Community

Cover image for Using unit and integration tests in Jest
Jalal πŸš€
Jalal πŸš€

Posted on β€’ Edited on

2 1

Using unit and integration tests in Jest

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
Enter fullscreen mode Exit fullscreen mode

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"],
};
Enter fullscreen mode Exit fullscreen mode

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

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

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",
 },
Enter fullscreen mode Exit fullscreen mode

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

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (10)

Collapse
 
damnhandy profile image
Ryan McDonough β€’

This was incredibly useful @jalal246 and solved what I was looking for. Thanks for the tip!

Collapse
 
jalal246 profile image
Jalal πŸš€ β€’

Thanks for the kind words. Appreciate it.

Collapse
 
jattoabdul profile image
Jatto Abdulqahhar β€’

How do you merge coverage report from both application?

Collapse
 
damnhandy profile image
Ryan McDonough β€’

That's never an easy problem to solve, with any framework or language. It's especially complicated when integration testing a distributed system. The complexity to solve that just doesn't seem to be worth it IMO.

Collapse
 
cyrfer profile image
John Grant β€’

I don't think you should. Knowing what coverage you have in integration vs unit matters.

Collapse
 
erikgmatos profile image
Erik Garces Matos β€’

I have the same doubt =/

Collapse
 
robinmitra profile image
Robin Mitra β€’

Thanks! Works great for my use-case of separating unit and integration tests on a React Native project.

Collapse
 
jalal246 profile image
Jalal πŸš€ β€’

Glad it helps!

Collapse
 
glebirovich profile image
Gleb Irovich β€’

Jest allows to specify config file as a parameter. You can add it in npm scripts for each project in your monorepo.

Collapse
 
jalal246 profile image
Jalal πŸš€ β€’

Adding separate jest config files is a key here. But still, need to figure out more about it, including opening a server for example, when I have some time to launch end to end test.

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay