10

We recently migrated two different repos into a monorepo. Each uses jest with its own custom configurations, defined in their own package.json files.

I'd like to use the --projects flag to run Jest across both projects from the root of the monorepo. I've added a jest.config.js file to the root of the monorepo:

module.exports = {
    projects: ['<rootDir>/projectA', '<rootDir>/projectB']
}; 

The runner successfully picks up the tests for both projects, but it doesn't appear to be using each project's custom configuration. For example, in "projectA", I'm using babel-plugin-module-resolver. When I run jest in that project alone, babel-jest successfully picks up that plugin and it works fine, but when I run it from the root in multi-project mode, I get "Cannot find module..." errors that indicate the plugin isn't being used.

Similarly, in "projectB" I'm using a custom setupTestFrameworkScriptFile. Running jest in this project runs that file just fine, but it's ignored when running from the root.

My understanding of the multi-project mode was that each individual project should keep its own settings/configs intact. Did I miss something? Do I need to configure these in the root as well?

Kenrick
  • 492
  • 8
  • 17
kgoggin
  • 169
  • 1
  • 12
  • Looking at how React does it, looks like they pointed the "projects" straight to the project's config file: https://github.com/facebook/react/pull/10214/files#diff-b9cfc7f2cdf78a7f4b91a753d10865a2R123 – Kenrick Nov 21 '17 at 04:05
  • React is not using projects anymore – Sibelius Seraphini Nov 15 '18 at 01:10

1 Answers1

0

I think there are some bugs with jest multi project runner, we need to provide some failing examples so jest can fix it. There are almost no docs about it

I made this work providing custom babel-transformer instead of using babel-jest directly.

Check this link https://twitter.com/sseraphini/status/1061779382669316098

Use this for your transformer inside packages

const config = require('../shared/babel.config.js');

 const { createTransformer } = require('babel-jest');
 module.exports = createTransformer({
  ...config,
});

and use this for your root transfomer

const { join, resolve } = require('path');
 const { createTransformer } = require('babel-jest');
 const packagePath = resolve('../');

const packageGlob = join(packagePath, '*');
 module.exports = createTransformer({
  babelrcRoots: packageGlob,
});

use like this on jest.config.js

transform: {
    '^.+\\.(js|ts|tsx)?$': '<rootDir>/test/babel-transformer',
},
Sibelius Seraphini
  • 5,303
  • 9
  • 34
  • 55