question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

babel-jest issues with monorepo and jest multi project runner

See original GitHub issue

🐛 Bug Report

We are moving to a monorepo structure, and we’d like to use jest multi-project-runner structure. We are using yarn workspaces and lerna to manage dependencies. Here is a repro https://github.com/entria/entria-fullstack/pull/12, where you can reproduce some of the issues.

To Reproduce

Neither of this transforms works:

'^.+\\.(js|ts|tsx)?$': 'babel-jest',
^.+\\.(js|ts|tsx)?$': '<rootDir>/node_modules/babel-jest',

I think the problem here is that babel-jest resolution strategy is failing to find the correct babel.config.js file

Only creating a custom babel transformer the transform works, like the below:

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

Steps to reproduce the behavior: Run jest on root of this project https://github.com/entria/entria-fullstack/pull/12

modify transform config from custom transformer to babel-jest

Another problem is that when using only projects option on jest.config.js root, it won’t use a different jest config per project, it is looks like all the config should be on root jest.config.js instead of jest.config.js inside each project

Expected behavior

  • it should find correct babel.config.js

  • it should transpile all files inside packages/*

  • it should use the jest.config.js for each project (transform options and so on)

Link to repl or repo (highly encouraged)

https://github.com/entria/entria-fullstack/pull/12

Run npx envinfo --preset jest

Paste the results here:

System:
    OS: macOS 10.14.1
    CPU: (4) x64 Intel(R) Core(TM) i7-5557U CPU @ 3.10GHz
  Binaries:
    Node: 10.12.0 - ~/.nvm/versions/node/v10.12.0/bin/node
    Yarn: 1.12.3 - /usr/local/bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v10.12.0/bin/npm
  npmPackages:
    jest: ^23.6.0 => 23.6.0 

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:37
  • Comments:22 (4 by maintainers)

github_iconTop GitHub Comments

34reactions
ofhousecommented, Aug 23, 2019

I am using jest multi project runner with babel-jest and it works just fine. Each package in the monorepo has it’s own local .babelrc.js and no .babelrc.js in the root is required.

Each of my packages has a jest.config.js with the following settings to make this work:

module.exports = {
  ...
  transform: {
    '\\.js$': ['babel-jest', { cwd: __dirname }],
  },
  ...
};

I made an example repo which contains two packages with different babel-configs, feel free to check it out: https://github.com/ofhouse/jest-project-babel-transformer

Edit: You need Jest >= 24.9.0 to pass options to transformers

10reactions
bobbybobbycommented, Mar 11, 2019

@bradfordlemley said

What if users want top-level babel config?: If individual projects want to include top-level babel config, it can be achieved by configuring babel to rootMode upward or whatever is desired. Currently, that configuration would need to be set in a custom jest transform module, but #7288 adds the ability to configure babel-jest within jest’s config, so that will hopefully become easier. This way, the top-level babel.config.js would apply if the project is run individually or via multi-project.

Here’s how I configured it in my monorepo project (that uses yarn workspaces for dependency management, and lerna for running commands on sub packages).

Project structure :

/monorepo
  babel.config.js
    /packages/
      /pkg1
        jest.config.js
        jestBabelTransform.js
        ...

/babel.config.js :

module.exports = {
  presets: [ "@babel/env", "@babel/react" ],
  babelrcRoots: ["./packages/*"]
};

/packages/pkg1/jest.config.js :

const path = require('path');

module.exports = {
  transform: {
    "^.+\\.js$": path.resolve(__dirname, "./jestBabelTransform.js")
  },
};

/packages/pkg1/jestBabelTransform.js :

const babelJest = require('babel-jest');

module.exports = babelJest.createTransformer({
  rootMode: 'upward'
});

I’m already using the rootMode: upward option with my build process, this way I get the same behaviour running jest. I can run jest from the pkg1 directory with yarn test or from the monorepo root with lerna run test --scope pkg1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Monorepo testing using jest projects - Orlando Bayo Adeyemi
This post is about my experience with the implementation. The Problem. Monorepos are a source control pattern where all of the source code...
Read more >
Jest in a Monorepo doesn't work if running from a specific ...
This seems to be related to how babel is configured in your project. The way I did it is add a root babel...
Read more >
Configuring Jest
A global setup module configured in a project (using multi-project runner) will be triggered only when you run at least one test from...
Read more >
Configuring Jest compiled - w3resource
In this tutorial we will focus on configuring Jest. Jest's configuration can be defined inside the package.json file of your project, ...
Read more >
covcg/vscode-jest-runner - Visual Studio Marketplace
Extension for Visual Studio Code - Simple way to run or debug a single (or multiple) tests from context-menu.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found