Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use different jest.config.js for unit and component tests?

I am trying to create a separate suite of tests for component level testing of an app.

In this separate suite, I will need a custom test environment that bootstraps some persistence layer mocks and these tests will be housed in a separate folder in my project structure from my unit tests.

e.g. the unit tests will be nearby the code in __tests__ folders while the component tests will be in a top-level tests/ directory.

I already have most of my jest.config.js setup, but when I added the projects section to override some of the fields, specifically testMatch and testEnvironment it completely disregarded my configuration specified in global.

Now I've been reading that the projects configuration is mostly about monorepo setups, so I don't think that's the right approach.

Ideally I could just have a separate jest.config.js in my tests/ directory, but I don't see how to set that up if that even works.

Any insight into this problem would be helpful. Thanks ahead of time.

like image 349
GoldFlsh Avatar asked Jan 25 '23 02:01

GoldFlsh


1 Answers

To specify different config files you can use the --config option.

For example if you have a tests/jest.config.js file configuration for your component tests and a jest.config.js file for your unit tests, you can configure the scripts in your package.json so that they look something like this:

"scripts": {
    "tests:unit": "jest --config=jest.config.js",
    "tests:component": "jest --config=tests/jest.config.js"
}
like image 54
mgarcia Avatar answered Jan 29 '23 15:01

mgarcia