jest: TypeError: Cannot read property 'Object.' of null
Trying to run Jest tests after upgrading.
/Users/justintulk/Sites/myProject/node_modules/jest-runtime/build/index.js:518
(_script_transformer || _load_script_transformer()).default.EVAL_RESULT_VARIABLE];
TypeError: Cannot read property 'Object.<anonymous>' of null
Do you want to request a feature or report a bug?
Report a bug.
What is the current behavior?
yarn test
fails at running scripts/test.js
(included). Throws primarily when trying to run all the tests, running yarn watch
and only testing changed files has not reproduced the errors.
/Users/justintulk/Sites/myProject/node_modules/jest-runtime/build/index.js:518
(_script_transformer || _load_script_transformer()).default.EVAL_RESULT_VARIABLE];
^
TypeError: Cannot read property 'Object.<anonymous>' of null
at Runtime._execModule (/Users/justintulk/Sites/myProject/node_modules/jest-runtime/build/index.js:518:64)
at Runtime.requireModule (/Users/justintulk/Sites/myProject/node_modules/jest-runtime/build/index.js:332:14)
at Runtime.requireModuleOrMock (/Users/justintulk/Sites/myProject/node_modules/jest-runtime/build/index.js:408:19)
at Object.getVendors (/Users/justintulk/Sites/myProject/node_modules/newrelic/lib/utilization/index.js:6:10)
at fetchSystemInfo (/Users/justintulk/Sites/myProject/node_modules/newrelic/lib/system-info.js:93:15)
at facts (/Users/justintulk/Sites/myProject/node_modules/newrelic/lib/collector/facts.js:9:3)
at _getFacts (/Users/justintulk/Sites/myProject/node_modules/newrelic/lib/collector/api.js:162:5)
at redirectCb (/Users/justintulk/Sites/myProject/node_modules/newrelic/lib/collector/api.js:158:5)
at cb_nextTick (/Users/justintulk/Sites/myProject/node_modules/newrelic/lib/collector/parse-response.js:113:7)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install
and yarn test
.
What is the expected behavior?
Should run tests without crashing.
Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
EDIT ~Works fine~ fails w/ Jest 20.0.4 Fails on 21.2.1
scripts/test.js
// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = "test";
process.env.NODE_ENV = "test";
process.env.PUBLIC_URL = "";
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on("unhandledRejection", err => {
throw err;
});
const ijest = require("jest");
const argv = process.argv.slice(2);
// Watch unless on CI or in coverage mode
// if (!process.env.CI && argv.indexOf("--coverage") < 0) {
// argv.push("--watch");
// }
ijest.run(argv);
Package.json
"test": "node scripts/test.js",
"jest": {
"collectCoverageFrom": ["src/**/*.{js,jsx}"],
"testMatch": [
"<rootDir>/controllers/**/__tests__/**/*.js?(x)",
"<rootDir>/controllers/**/?(*.)(spec|test).js?(x)"
],
"testEnvironment": "node",
"testURL": "http://localhost",
"testResultsProcessor": "./node_modules/jest-junit"
}
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 15
- Comments: 31 (8 by maintainers)
For the Animated problem, Can add this to your test file
For others that find this issue, it can be caused by running an
Animated
animation in React Native. Not running that animation when jest is detected is the best solution I found for now.@SimenB that error message helps a lot - thanks! for anyone stumbling across this issue with Animated, put the following line into the jest setup script:
import 'react-native/Libraries/Animated/src/bezier'
This will fail in Jest 22 because we clean up the contexts more aggressively. This error normally happens when you are scheduling async work after the test run for a file has already been completed.
If I switch my test environment from node to the default jsdom I get the following error instead
I’ve found similar errors in other places: https://www.google.com/search?q=jest+Cannot+read+property+‘Object.<anonymous>’+of+null&rlz=1C5CHFA_enUS703US703&oq=jest+Cannot+read+property+‘Object.<anonymous>’+of+null&aqs=chrome..69i57j69i60l3j0l2.931j0j4&sourceid=chrome&ie=UTF-8 but have not found any posted resolution. If anyone is aware of a resolution please link and I’ll close.
None of the above fixes worked for me. While certainly not ideal, a simple workaround is adding
setImmediate(done);
to the end of the test.I’ve opened up a PR trying to give a better error in this situation: #5888
I am getting the same error on 20.0.4 and can’t find a way to shake the error. The tests were passing before I did a yarn install.
Using
js-ipfs
seems to cause this error but I can’t find out why this happens actuallyIt’s been broken consistently in my circle CI (jest running inside docker container with node:8.9.0 image, also tested with node:8.9.1 same error).
The test run without a hitch in my local machine (even with the same setup as circle CI i.e. run in docker and node:8.9.0).
But since it’s always errored in the CI, I think I can test something in case anyone has any suggestion.
Here’s the log from jest:
Seems like coming from webpack there. ~But why it’s only error on the CI is a mystery~.
update:
After some tests I figure out that I can replicate the error in my local machine by running jest with
-w 1
this explains why it’s only error at circle CI, they seems to gave only 1 vcpu for each build env.I only have
__tests__
folders under these directories:and jest only error if it’s running all tests at once, it wont error when running with any combination of two folders, or if it’s only one of them e.g.
jest src/app
For workaround I separate the tests into two jest calls:
jest src/app
andjest src/contexts src/layouts
.