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.

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

See original GitHub issue

🐛 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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:29
  • Comments:55

github_iconTop GitHub Comments

54reactions
SimenBcommented, Feb 14, 2019

@Oliphaunte your reproduction uses jest@23 with babel-jest@24 - that’s not supported. Upgrading to jest@24 makes your test pass.

I wonder if we should have a peer dep?

32reactions
debuggycommented, Feb 14, 2019

Same bug occured in my project. For the detailed info:

 FAIL  tests/schema-validator.test.js
  ● Test suite failed to run

    TypeError: Cannot read property 'cwd' of undefined

      at Object.getCacheKey (node_modules/babel-jest/build/index.js:155:51)
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Cannot read property 'cwd' of undefined
Probably the problem is the version of the "babel-jest" module. Try to allineate it with the "jest" module. These are my dependencies in...
Read more >
24.1.0: TypeError: Cannot read property 'cwd' of undefined
When I run my tests, I get the following error: ○ Test suite failed to run TypeError: Cannot read property 'cwd' of undefined...
Read more >
TypeError: Cannot read property 'cwd' of undefined-Reactjs
[Solved]-TypeError: Cannot read property 'cwd' of undefined-Reactjs. Probably the problem is the version of the "babel-jest" module. Try to allineate it with ...
Read more >
babel-jest - npm
Jest plugin to use babel for transformation.. Latest version: 29.3.1, last published: 2 months ago. Start using babel-jest in your project ...
Read more >
cannot read property 'pathname' of undefined jest - You.com
TypeError : Cannot read property 'location' of undefined. error image. my component code is like this. import React from "react"; import { useHistory...
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