jest: 24.1.0: TypeError: Cannot read property 'cwd' of undefined
🐛 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
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 29
- Comments: 55 (13 by maintainers)
@Oliphaunte your reproduction uses
jest@23
withbabel-jest@24
- that’s not supported. Upgrading tojest@24
makes your test pass.I wonder if we should have a peer dep?
Same bug occured in my project. For the detailed info:
For any who might encounter this, make sure anything jest related is upgraded to the same major version. Just having jest-cli still on an old version caused this exact error for me.
Still broken for me on jest@24 babel-jest@24. =(
@SimenB That was a careless mistake on my part, I updated all my packages incorrectly before running those tests. It all works correctly now with the latest versions. I’ve also updated the package I shared previously in case anybody needs more reference.
Thank you @SimenB for your help!
Whoever encounters this again: babel-jest and jest have to be the same version (Major). If an old project/service using @babel/plugin-transform-runtime
additionally: This Issue helped me
All this happened when upgrading from babel 6.x to 7.x for me. I used this tool for that.
I can confirm the same issue occurring for me in a starter react-setup package I am working on, which is hopefully simple enough to allow for debugging.
https://github.com/Oliphaunte/react_redux_setup
I can also confirm the updates @SethDavenport made in
node_modules/babel-jest/build/index.js
fixed the issue for me,yarn test
runs successfully post-edits. However, it only seems to work with a .babelrc.js object configuration. It still does not work with a babel.config.js setup, not sure if that’s just me though, would love to find out any additional information on that front as well.Also seeing this issue on “babel-jest”: “24.9.0”, “jest”: “24.9.0”, but only when running npm install, npm test. Yarn works.
@maneetgoyal Could you please share your package.json? I’ve made the changes, but I still get the error.
Thanks in advance!
Same here I had to downgrade babel-jest from 24.8.0 to 23.4.2. babel-upgrade helped me to do that.
I resolved mine by removing jest.
Now all I have that is jest dependant is : “babel-jest”: “^24.7.1”, “jest-cli”: “^24.7.1”, “jest-dom”: “^3.1.3”,
On Wed, May 22, 2019 at 9:01 AM Janne Kurkinen notifications@github.com wrote:
I’ll like to add a resolution if it helps since it took me a day to find out why I was getting this issue:
-> in my home directory i had some old node_modules installed that i forgot about -> despite i was running my tests from a project where were installed the latest versions of jest & babel-jest, somehow I believe npm was mixing up the dependencies by looking at the node_modules installed in my home directory…which i resolved by removing it. All works fine now.
@SimenB actually, thanks! I think I just figured it out, I also updated the global jest install to the latest version and it worked.
In this case, the docs should help you out: https://jestjs.io/docs/en/getting-started#using-babel
I think you’ll also be seeing a peer dependency warning, not sure about that one, though!
I went through my versions on package.json and package-lock.json just to double check, and they were all pointing to v24 and above, and I still got the same error.
Does the error still occur without the
setupFiles
? @SimenB I think this might beallTests[0].context.config
from https://github.com/facebook/jest/pull/7562 again, only way I can think of that anundefined
config could get into a transformer