Skip to content

24.1.0: TypeError: Cannot read property 'cwd' of undefined #7868

@SethDavenport

Description

@SethDavenport

🐛 Bug Report

I've updated one of my projects from babel 6 to babel 7. As part of this, I've also

  • updated from Jest/Babel-Jest 23 to Jest/Babel-Jest 24.
  • updated my custom JS transformer to use babel 7 stuff.

When I run my tests, I get the following error:

  ● Test suite failed to run

    TypeError: Cannot read property 'cwd' of undefined
      at Object.getCacheKey (node_modules/babel-jest/build/index.js:155:51)

I can "fix" this error and get my tests running again if I hack node_modules/babel-jest/build/index.js as follows:

    getCacheKey(
      fileData,
      filename,
      configString,
      {config, instrument, rootDir}
    ) {
      // ORIGINAL:
      // const babelOptions = loadBabelConfig(config.cwd, filename);
      // HACK:
      const babelOptions = loadBabelConfig(config ? config.cwd : rootDir, filename);

      // ...

With this, my tests run fine, however I don't really understand under what circumstances config could be undefined.

To Reproduce

Here are my configs:

package.json:

"jest": {
    "transform": {
      "^.+\\.js$": "<rootDir>/jest/js-loader.js",
      "^.+\\.html$": "<rootDir>/jest/html-loader.js"
    },
    "transformIgnorePatterns": [
      "/node_modules/(?!@wealthsimple)/"
    ],
    "globals": {
      "jasmine": {}
    },
    "setupFiles": [
      "<rootDir>/jest/setup.js"
    ],
    "testMatch": [
      "**/?(*.)+(jest).js?(x)"
    ],
    "modulePathIgnorePatterns": [
      "<rootDir>/.yarn-cache/"
    ],
    "collectCoverageFrom": [
      "src/**/*.js"
    ],
  },

jest/js-loader.js:

const config = {
  presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          node: 'current',
        },
      },
    ],
    '@babel/react',
  ],
  plugins: [
    '@babel/plugin-transform-flow-strip-types',
    'dynamic-import-node',
    'styled-components',
    '@babel/plugin-transform-object-assign',
    '@babel/plugin-proposal-class-properties',
    '@babel/plugin-proposal-object-rest-spread',
    '@babel/plugin-transform-async-to-generator',
  ],
  sourceMaps: 'inline',
};

module.exports = require('babel-jest').createTransformer(config);

Expected behavior

Tests to run as they did with babel 6/babel-jest 23.

Run npx envinfo --preset jest

Paste the results here:

npx: installed 1 in 2.548s

  System:
    OS: macOS 10.14.1
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
  Binaries:
    Node: 10.12.0 - ~/.config/fnm/bin/node
    Yarn: 1.12.3 - /usr/local/bin/yarn
    npm: 6.4.1 - /usr/local/bin/npm
  npmPackages:
    jest: ^24.1.0 => 24.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions