Jest: Guide to use Jest with Lerna

Created on 9 Mar 2017  路  7Comments  路  Source: facebook/jest

Do you want to request a feature or report a bug?

docs :)


Basically, I'd like to know what would be the best set up for using Jest and Lerna together. We'd like to have the parent dir with the Jest dependencies and be responsible to run tests.

  1. What will happen if some packages have .babelrc? Will Jest use that one or the parent one? Will it have conflicts? Maybe one package needs a plugin that other does not.
  2. I also ran into this issue https://github.com/facebook/jest/issues/2034. So, is the recommended way to create a build/index.js for all packages first? Any alternatives?
  3. Can I override jest config in each package.json of each package? So I can have a different jest set up depending on the package? For example, one package might use setupFiles but others will not need it.
Documentation

Most helpful comment

on the root of the repo I have
a jest.config.js like

const base = require("./jest.config.base.js");

module.exports = {
    ...base,
    projects:
    [
        "<rootDir>/packages/*/jest.config.js"
    ],
    coverageDirectory: "<rootDir>/coverage/"
};

using the projects this will look for all the jest.config.js in the packages

a jest.config.base.js that will be used as a base for all the packages

module.exports = {
    roots: [
        "<rootDir>/src",
        "<rootDir>/tests"
    ],
    transform: {
        "^.+\\.ts$": "ts-jest"
    },
    testRegex: "(/tests/.*.(test|spec)).(jsx?|tsx?)$",
    moduleFileExtensions: [
        "ts",
        "tsx",
        "js",
        "jsx",
        "json",
        "node"
    ],
    collectCoverage: true,
    coveragePathIgnorePatterns: [
        "(tests/.*.mock).(jsx?|tsx?)$"
    ],
    verbose: true
};

the src and tests folders are folder in my packages.

Now in each packages I have a jest.config.js with

// Jest configuration for api
const base = require("../../jest.config.base.js");

module.exports = {
    ...base,
    name: YourModuleName,
    displayName: YourModuleName
};

You could get your module name from the package.json for instance

So now you can run jest either from the packages itself or from the root and it should work.
$ lerna run test will execute the tests in each packages or $ yarn test which will run jest on the root.

Hope it helps

All 7 comments

You could look at the Jest repo itself for a start! You cannot overwrite the Jest config for now, for that we may build the multi-config runner #2970.

Is there a way use babel-jest as a transform in lerna repos such that I don't need to have a built file in every package? I am running into issues with code in my node_modules not getting transformed due to testPathIgnorePatterns.

I have the same issue as @casesandberg. The Jest unit tests in Lerna packages are failing.
One issue I have is "import" being undefined token (testPathIgnorePatterns fixes that), but then the Lerna packages are not transpiled and hence cannot be tested.

Any help?

on the root of the repo I have
a jest.config.js like

const base = require("./jest.config.base.js");

module.exports = {
    ...base,
    projects:
    [
        "<rootDir>/packages/*/jest.config.js"
    ],
    coverageDirectory: "<rootDir>/coverage/"
};

using the projects this will look for all the jest.config.js in the packages

a jest.config.base.js that will be used as a base for all the packages

module.exports = {
    roots: [
        "<rootDir>/src",
        "<rootDir>/tests"
    ],
    transform: {
        "^.+\\.ts$": "ts-jest"
    },
    testRegex: "(/tests/.*.(test|spec)).(jsx?|tsx?)$",
    moduleFileExtensions: [
        "ts",
        "tsx",
        "js",
        "jsx",
        "json",
        "node"
    ],
    collectCoverage: true,
    coveragePathIgnorePatterns: [
        "(tests/.*.mock).(jsx?|tsx?)$"
    ],
    verbose: true
};

the src and tests folders are folder in my packages.

Now in each packages I have a jest.config.js with

// Jest configuration for api
const base = require("../../jest.config.base.js");

module.exports = {
    ...base,
    name: YourModuleName,
    displayName: YourModuleName
};

You could get your module name from the package.json for instance

So now you can run jest either from the packages itself or from the root and it should work.
$ lerna run test will execute the tests in each packages or $ yarn test which will run jest on the root.

Hope it helps

@nolazybits are you sure your projects are even used? When I follow what you did, it just executes everything with the base config.

@Itrulia Here's what I did in my packages/*/jest.config.js files to fix this:

const base = require("../../jest.config.base.js");
const pack = require("./package");

module.exports = {
  ...base,
  displayName: pack.name,
  name: pack.name,
  rootDir: "../..",
  testMatch: [`<rootDir>/packages/${pack.name}/**/*.spec.js`],
};

I also fixed it already, I had to add "<rootDir" to projects and then it worked.

Was this page helpful?
0 / 5 - 0 ratings