Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guide to use Jest with Lerna #3112

Closed
ferrannp opened this issue Mar 9, 2017 · 10 comments
Closed

Guide to use Jest with Lerna #3112

ferrannp opened this issue Mar 9, 2017 · 10 comments

Comments

@ferrannp
Copy link
Contributor

ferrannp commented Mar 9, 2017

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 babel-jest transforming dependencies #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.
@cpojer
Copy link
Member

cpojer commented Mar 9, 2017

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.

@casesandberg
Copy link

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.

@hpeikari
Copy link

hpeikari commented Jun 8, 2018

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?

@nolazybits
Copy link

nolazybits commented Jun 19, 2018

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

@Itrulia
Copy link

Itrulia commented Jul 2, 2018

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

@stephanschubert
Copy link

stephanschubert commented Aug 8, 2018

@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`],
};

@Itrulia
Copy link

Itrulia commented Aug 8, 2018

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

@youngjuning
Copy link

Maybe you need to set babel.config.js:

module.exports = {
  ...require('../../babel.config.js')
};

@ghost
Copy link

ghost commented Jan 26, 2021

I am not successful in settings things up based on comments here - jest won't detects test files when running lerna run test. However I am fine, for the moment, with this functional one-liner:

lerna exec npx jest

robertohuertasm pushed a commit to vscode-icons/vscode-icons that referenced this issue Mar 14, 2021
Jest common/shared configuration file.

*.base.* and *.common.* pattern.

See jestjs/jest#3112 for more example.
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants