Closed SethDavenport closed 5 years ago
Does the error still occur without the setupFiles
?
@SimenB I think this might be allTests[0].context.config
from https://github.com/facebook/jest/pull/7562 again, only way I can think of that an undefined
config could get into a transformer
Might be. This needs a proper reproduction, though. @SethDavenport mind putting together a more complete project we can pull down and test? As minimal as possible, although it should be fairly easy to debug bigger projects here as well 🙂
If it's hard to isolate, you can change this to just be return lines;
which should give a full stack trace including internals: https://github.com/facebook/jest/blob/9ba62ada66f731880229073eb193e8cb60863ae3/packages/jest-message-util/src/index.ts#L156
@jeysal it still happens without the setupfile, yes.
@SimenB it might be tricky to extract. I'll take a shot this evening. In the meantime the change you suggested to jest-message-util
results in this:
FAIL src/app/common/services/account-summary/account-summary.service.jest.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)
at ScriptTransformer._getCacheKey (node_modules/jest-runtime/build/script_transformer.js:159:23)
at ScriptTransformer._getFileCachePath (node_modules/jest-runtime/build/script_transformer.js:185:27)
at ScriptTransformer.transformSource (node_modules/jest-runtime/build/script_transformer.js:279:32)
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:388:40)
at ScriptTransformer.transform (node_modules/jest-runtime/build/script_transformer.js:448:19)
at Runtime._execModule (node_modules/jest-runtime/build/index.js:640:53)
at Runtime.requireModule (node_modules/jest-runtime/build/index.js:376:14)
at new Runtime (node_modules/jest-runtime/build/index.js:219:14)
at node_modules/jest-runner/build/run_test.js:98:15
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.
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)
TypeError: Cannot read property 'cwd' of undefined
at Object.getCacheKey (node_modules/babel-jest/build/index.js:155:51)
import React from "react";
import renderer from "react-test-renderer";
import Header from "./header";
describe("Header", () => {
it("renders correctly", () => {
const tree = renderer
.create(<Header siteTitle="Gatsby Jest Test" />)
.toJSON()
expect(tree).toMatchSnapshot()
})
})
Any ideas what could be wrong?
Thanks for making a repro repo @Oliphaunte - oddly my attempt to do so didn't reproduce the issue so I'm no closer to understanding what about my environment is triggering it.
I did come up with a slightly better workaround than editing my node_modules:
const c = {
presets: [
'@babel/preset-env',
'@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',
};
const defaultCwd = process.cwd();
const transformer = require('babel-jest').createTransformer(c);
// Monkey patch around https://github.com/facebook/jest/issues/7868
const oldGetCacheKey = transformer.getCacheKey;
transformer.getCacheKey = (
fileData,
filename,
configString,
{ config, instrument, rootDir },
) =>
oldGetCacheKey(
fileData,
filename,
configString, {
config: config || { cwd: defaultCwd },
instrument,
rootDir,
},
);
module.exports = transformer;
@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?
@SethDavenport Can you verify the version of jest-runtime
in your repo?
@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!
@SimenB I am not explicitly pulling in jest-runtime
in my package.json. Should I be?
My yarn.lock has both 23.6.0 and 24.1.0 of jest-runtime, likely due to a downstream dependency using jest 23.
No, you shouldn't have to. What does your package.json look like?
Although version 24 of jest
is not tested with ts-jest
which suggest to downgrade (if experiencing issues)
ts-jest has issues with jest 24 if you also use babel, from my understanding. So ts-jest and babel-jest@24 does not play well together
Still broken for me on jest@24 babel-jest@24. =(
Reproduction? Just saying it's broken doesn't really help us track it down
I was able to reproduce it, i was trying to fix it for my teammate, i didn't have it initially but after i deleted my node modules i was affected by this issue too
Sorry I've been slammed this week and didn't get a chance to circle back until now. My best guess at what's going on is as follows:
The top-level repo uses Jest 24:
...
"@babel/core": "^7.2.2",
"@babel/plugin-proposal-class-properties": "^7.3.0",
"@babel/plugin-proposal-object-rest-spread": "^7.3.2",
"@babel/plugin-transform-async-to-generator": "^7.2.0",
"@babel/plugin-transform-flow-strip-types": "^7.2.3",
"@babel/plugin-transform-object-assign": "^7.2.0",
"@babel/preset-env": "^7.3.1",
"@babel/preset-react": "^7.0.0",
"babel-jest": "^24.1.0",
"jest": "^24.1.0",
However one of its other dependencies uses babel 6/jest 23 (I'm in the middle of porting our internal stack over to Babel 7 and I haven't done our downstream internal packages yet). Crucially this dependency is declaring jest as a dependency
, not a devDependency
which is an error I will fix.
I think this is enough for my yarn.lock to have both 23- and 24-series of the jest stack in it, which probably means my runtime env isn't sane.
I removed bad internal dependency and now I can run the tests without the error.
AFAIC you can close the bug, I just need to finish porting over our internal packages.
Cool, thanks! Too bad the wrong versions are hoisted... Possibly an npm/yarn bug?
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.
I'm getting this error too. I went through the whole package.json and package-lock.json and everything appears to be at v24 for anything related to Jest.
This is the error I'm getting:
Test suite failed to run
TypeError: Cannot read property 'cwd' of undefined
at Object.getCacheKey (node_modules/babel-jest/build/index.js:155:51)
This is my package.json
{
"name": "foobar",
"version": "1.0.0",
"description": "foo",
"main": "index.js",
"scripts": {
"test": "jest; exit 0",
"start": "webpack-dev-server --open",
"lint": "eslint src/** src/**/**; exit 0",
"lint-fix": "eslint src/** src/**/** --fix; exit 0",
"win-lint": "eslint src/** src/**/**",
"win-lint-fix": "eslint src/** src/**/** --fix"
},
"author": "Charley McGowan",
"license": "MIT",
"dependencies": {
"prop-types": "^15.5.10",
"react": "^16.8.3",
"react-dom": "^16.8.3",
"react-redux": "^6.0.1",
"react-router-dom": "^4.0.0",
"redux": "^4.0.1",
"styled-jsx": "^3.2.1"
},
"devDependencies": {
"babel-core": "^6.24.1",
"babel-jest": "^24.1.0",
"babel-loader": "^7.0.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"eslint": "^4.19.1",
"eslint-loader": "^2.1.2",
"eslint-plugin-react": "^7.12.4",
"file-loader": "^1.1.6",
"html-webpack-plugin": "^2.29.0",
"jest": "^24.1.0",
"react-hot-loader": "^3.0.0-beta.7",
"url-loader": "^0.6.2",
"webpack": "^3.4.0",
"webpack-dev-server": "^2.5.0"
}
}
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.
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.
Just to update this. I was able to find a workaround by moving jest and babel-jest to versions 20.0.4. I don't see this as a solution per se to the current versions issue, but it at least allowed me to continue my work for the time being.
If you can put together a reproducible demo I'd be happy to take a look :)
If you can put together a reproducible demo I'd be happy to take a look :)
let me look for the repo that was giving me the issue.
@SimenB ok I found it, so it's This repository: https://github.com/glitchwizard/SettleIt-Capstone
and at this commit I added jest at 24.1.0, and babel-jest at 24.1.0:
at this later commit I switched it over to jest 20.0.4, and babel-jest 20.0.3, and all was well after that.
Hope this helps. The whole repo is two separate projects I'm working on , a C# back end a react front end, I think you'd only need to boot up the react front end to see this error at that commit point. I believe it was showing up for all commits between the two I've pointed you two here.
@glitchwizard thanks! Your repo uses Babel 6, so you need to use babel-jest@23
(or upgrade to babel 7). babel-jest@23
should work with jest@24
, but babel-jest@24
won't work with jest@23
@SimenB ok great! I'll be working on that repo today, so I'll upgrade everything and report back. Thanks for the pointer! For my own sake, I'm kind of new to this coding stuff, where would I find the documentation on what dependencies are needed for Jest so I don't run into this again?
I'm digging through this but with 4000+ dependencies it seems a bit difficult to track all those. Is there a best practices way for checking dependencies?
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!
@SimenB ok so when I run "npm run test" of which the script is ""test": "jest && exit 0" in my package.json, the tests are able to run, but I get an error at the end of it.
Test Suites: 1 failed, 1 passed, 2 total
Tests: 2 failed, 3 passed, 5 total
Snapshots: 0 total
Time: 4.801s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! SettleIt@1.0.0 test: `jest && exit 0`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the SettleIt@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\C M\AppData\Roaming\npm-cache\_logs\2019-03-08T16_45_26_562Z-debug.log
If I run simply 'jest' at the command line I get the following again:
PS C:\Users\C M\source\repos\SettleIt-Capstone\SettleItFrontEnd> jest
FAIL __tests__\reducers\display-settle-sheet-start-reducer.test.js
● Test suite failed to run
TypeError: Cannot read property 'cwd' of undefined
at Object.getCacheKey (node_modules/babel-jest/build/index.js:153:51)
FAIL __tests__\reducers\settle-sheet-reducer.test.js
● Test suite failed to run
TypeError: Cannot read property 'cwd' of undefined
at Object.getCacheKey (node_modules/babel-jest/build/index.js:153:51)
Test Suites: 2 failed, 2 total
Tests: 0 total
Snapshots: 0 total
Time: 2.324s
Ran all test suites.
PS C:\Users\C M\source\repos\SettleIt-Capstone\SettleItFrontEnd>
I think I have everything set up correctly in my package.json, .babelrc.
package.json:
{
"name": "foo",
"version": "1.0.0",
"description": "bar",
"main": "index.js",
"scripts": {
"test": "jest && exit 0",
"build": "webpack --mode production",
"start": "webpack-dev-server --open",
"lint": "eslint src/** src/**/**; exit 0",
"lint-fix": "eslint src/** src/**/** --fix; exit 0",
"win-lint": "eslint src/** src/**/**",
"win-lint-fix": "eslint src/** src/**/** --fix"
},
"author": "Charley McGowan",
"license": "",
"dependencies": {
"prop-types": "^15.5.10",
"react": "^16.8.3",
"react-dom": "^16.8.3",
"react-redux": "^6.0.1",
"react-router-dom": "^4.0.0",
"redux": "^4.0.1",
"styled-jsx": "^3.2.1",
"uuid": "^3.3.2"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.3.4",
"@babel/preset-env": "^7.3.4",
"@babel/preset-react": "^7.0.0",
"babel-jest": "^24.3.1",
"babel-loader": "^8.0.5",
"eslint": "^4.19.1",
"eslint-loader": "^2.1.2",
"eslint-plugin-react": "^7.12.4",
"file-loader": "^1.1.6",
"html-webpack-plugin": "^3.2.0",
"jest": "^24.3.1",
"react-hot-loader": "^3.0.0-beta.7",
"url-loader": "^0.6.2",
"webpack": "^3.4.0",
"webpack-dev-server": "^2.5.0"
}
}
and .babelrc
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
]
}
Am I missing something? Why would "npm run test" work, but not simply 'jest'?
@SimenB actually, thanks! I think I just figured it out, I also updated the global jest install to the latest version and it worked.
This problem just recently popped up for me 5 days ago in my jenkins pipeline thus breaking my build, although tests are working on my local machine. Any ideas?
You don't have a lockfile? As mentioned above, this is dues to version mismatches between Jest dependencies. Without a reproduction, there's not much else we can do to help
Thanks yes I thought the lockfile prevented these issues from occuring that was the whole idea. Will see if anything has happened along the way.
But it seems to just randomly appear I'm clueless. As the test where working just fine and then there was a commit in a branch that had nothing to do with the lockfile nor any dependencies. After that all tests just stopped working in any branches.. could it be that the pipeline is storing some dependencies somewhere? What could possibly have happened to the jenkins pipeline along the way?
I have never used Jenkins, so I am zero help there. However, this is not the issue to discuss it. StackOverflow might be of help?
Okey so here is how I fixed the issue on the failing tests in my jenkins pipeline (and working locally):
On a retrospective I believe the issue that occurred in my particular case was that there was some new dependency to babel-jest that crashed as I was using the ^ prefix for my dependencies. Changing to a particular version should ensure that this does not happen again and I can choose to update each package as I feel comfortable.
It is also worth mentioning I am using a non ejected create-react-app with react-app-rewired conf and prior to the crash I did not have babel-jest in my dev dependencies so not sure what kind of version cra was using under the hood. My conclusion is that something auto-magically broke my dependencies due to this fact that something was updated under the hood.
Anyway I'm glad to get this working after just 1.5 days of trial and error. It could have been much worse.
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.
If you are using node version manager, be sure to look in ~/.nvm
What fixed it for me was removing nvm, and installing jest-cli to get the right version of jest https://github.com/facebook/jest/issues/3391
I'm having this issue in 24.7.1.
ERROR: TypeError: Cannot read property 'cwd' of undefined
STACK: TypeError: Cannot read property 'cwd' of undefined
at Object.getCacheKey (/Users/bram/source/applydigital/zo-data-layer/node_modules/babel-jest/build/index.js:153:51)
Looking at that directory I can confirm not #3391, I definitely have 24.7.1 installed. (EDIT: sorry, 24.7.1)
Same here with ts-jest@24.0.2 @jest/core@24.8.0
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:
Same here with ts-jest@24.0.2 @jest/core@24.8.0
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/facebook/jest/issues/7868?email_source=notifications&email_token=AAS5DVRKL6HMUTJKIJ2U2N3PWT4WVA5CNFSM4GWWRZTKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODV6HUJA#issuecomment-494696996, or mute the thread https://github.com/notifications/unsubscribe-auth/AAS5DVVNUJZ7R55INLWNU3LPWT4WVANCNFSM4GWWRZTA .
Fixed this issue by bumping the following packages:
jest@24.8.0
babel-jest@24.8.0
@types/jest@24.0.13
The only thing that helped me with this issue was downgrading babel-jest
to 23.2
.
I've found the answer here https://stackoverflow.com/questions/54707877/typeerror-cannot-read-property-cwd-of-undefined. Thank you, Cesare Fischetti!
Same here I had to downgrade babel-jest from 24.8.0 to 23.4.2. babel-upgrade helped me to do that.
Solve my issue by removing jest-cli from my package.json and did a yarn
@bbouabou @allomov Yep, that fixed the issue for me also. Downgraded babel-jest to 23.4.2.
@glitchwizard thanks! Your repo uses Babel 6, so you need to use
babel-jest@23
(or upgrade to babel 7).babel-jest@23
should work withjest@24
, butbabel-jest@24
won't work withjest@23
This I feel should be included in the documentation. This solved so many mysterious bugs I got after upgrading Babel to 7 and babel-jest
to 24. Who woulda thought you need to upgradejest
too?!
@SethDavenport's fix (https://github.com/facebook/jest/issues/7868#issuecomment-463630822) worked for me. Using:
"@babel/core": "^7.6.2",
"@types/jest": "^24.0.18",
"jest": "^24.9.0",
"babel-jest": "^24.9.0",
Without that patch, I still get the error: "TypeError: Cannot read property 'cwd' of undefined". And I'm using yarn
.
🐛 Bug Report
I've updated one of my projects from babel 6 to babel 7. As part of this, I've also
When I run my tests, I get the following error:
I can "fix" this error and get my tests running again if I hack
node_modules/babel-jest/build/index.js
as follows: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/js-loader.js
:Expected behavior
Tests to run as they did with babel 6/babel-jest 23.
Run
npx envinfo --preset jest
Paste the results here: