jest@28 babel-jest config file path

Viewed 27

I bumped jest from version 27 to 28 in a monorepo and figured jest@28 don't pick up the babel config anymore. I read about the breaking changes in the migration guide especially the section about the babel config changes but I am a bit confused by what this exactly means:

babel-jest now passes root: config.rootDir to Babel when resolving configuration. This improves compatibility when using projects with differing configuration, but it might mean your babel config isn't picked up in the same way anymore.

This is how my project is structured:

  • It's a standard monorepo that relies on lerna and yarn classic workspaces.
  • jest is installed as a workspace de dependency
  • babel dependencies are also installed as workspace dependencies
  • tests are written in TypeScript and transpile through babel-jest

Babel config file (babel.config.js) lives at the root of the repo, and only configures jest to transpile TypeScript tests:

module.exports = {
    presets: [
        ['@babel/preset-env', { targets: { node: 'current' } }],
        '@babel/preset-typescript'
    ]
}

Jest as a root config (root.config.js) that's passed to the npm script:


module.exports = {
    rootDir: '../..',
    projects: [
        '<rootDir>/packages/@scope/packageA',
        '<rootDir>/packages/@scope/packageB',
        '<rootDir>/packages/@scope/packageC',

    ],

    // Global mono-repo code coverage threshold.
    coverageThreshold: {
        global: {
            branches: 80,
            functions: 85,
            lines: 85,
        },
    },

};

And also has a common config (common.config.js that's imported and extended in every project:

module.exports = {
    testMatch: ['<rootDir>/**/__tests__/**/*.spec.ts'],
};

I had to update the common.config.js jest config with the following to make it work with jest 28:

module.exports = {
    testMatch: ['<rootDir>/**/__tests__/**/*.spec.ts'],
    transform: {
        '^.+\\.[jt]sx?$': ['babel-jest', { configFile: './babel.config.js' }],
    },
};

Note that the jest root and common config files are under <wkspceRoot>/scripts/jest.

I am just wondering if there's a way to ensure that babel-jest pick up my babel config file without having to explicitly pass it in and what the quoted change means in detail.

0 Answers
Related