Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you setup multiple jest configs within a single project?

Tags:

reactjs

jestjs

In the create-react-app boilerplate it has both server and client code using the same package.json file. I'd like to have three separate jest configs: client unit tests, server unit tests, and server integration tests. Unfortunately, I haven't been able to get this to work. My plan was to have yarn run commands for each option and then specify the config to jest using the --config CLI option. But then I ran into this roadblock which happens on both v20 and v21 and is showing no progress. Basically, people are reporting issues with reading config files using --config and the fallback is to use the config package.json. Unfortunately, you can only specify a single config file.

Has anyone managed to get this to work and if so can you describe your setup (the version of jest, etc). I'm wondering if I need to downgrade to v19. Alternatively, am I just taking the wrong approach?

like image 405
eremzeit Avatar asked Nov 09 '17 00:11

eremzeit


1 Answers

Apparently the up and coming feature of jest is the --projects option. You can do something like this in your package.json file:

"jest": {
  "projects": [
    "src/client/jest.config.js",
    "src/server/unit.jest.config.js",
    "src/server/int.jest.config.js",
   ]
}

And then in src/client/jest.config.js you can define a specific configuration:

module.exports = {
  name: 'client',
  displayName: 'client',

  // NOTE: if you don't set this correctly then when you reference
  // it later in a path string you'll get a confusing error message.
  // It says something like' Module <rootDir>/config/polyfills.js in
  // the setupFiles option was not found.'
  rootDir: './../../',

  testMatch: [
    "<rootDir>/src/server/**/__tests__/*.unit.{js,jsx}",
    "<rootDir>/src/server/**/__tests__/unit/*.{js,jsx}"
  ],

  // etc...
};

Here's the post describing the feature: https://facebook.github.io/jest/blog/2017/05/06/jest-20-delightful-testing-multi-project-runner.html

like image 97
eremzeit Avatar answered Oct 06 '22 16:10

eremzeit