Bug Report
Attempting to run Jest (via yarn run test:ci) in container as part of Jenkins pipeline. Works flawlessly on local machine, but fails randomly on Jenkins. About half of identical builds are coming back with the following error.
$ jest --coverage --ci
(node:45) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'close' of undefined
at Object.<anonymous> (/var/lib/jenkins/workspace/portal-ui-gerrit-build-develop/node_modules/chalk/index.js:72:75)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Module.require (module.js:604:17)
at require (internal/module.js:11:18)
at _load_chalk (/var/lib/jenkins/workspace/portal-ui-gerrit-build-develop/node_modules/jest-cli/build/cli/index.js:67:42)
at Object.<anonymous> (/var/lib/jenkins/workspace/portal-ui-gerrit-build-develop/node_modules/jest-cli/build/cli/index.js:23:32)
(node:45) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:45) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Jest configuration
"jest": {
"preset": "jest-preset-angular",
"roots": [
"<rootDir>/src/"
],
"setupTestFrameworkScriptFile": "<rootDir>/src/setup-jest.ts",
"testResultsProcessor": "jest-junit"
},
"jest-junit": {
"suiteName": "Jest Unit Tests",
"output": "./reports/jest.xml",
"classNameTemplate": "{classname}-{title}",
"titleTemplate": "{classname}-{title}",
"ancestorSeparator": " › ",
"usePathForSuiteName": "true"
},
Without a reproduction this isn't actionable
How can I best provide the info that is needed?
Setting up a repo which exhibits the error. Even if it doesn't happen every time, having some code where it happens _sometimes_ might help us track it down.
What appears to happen is that you have left some dangling asynchronous code after marking your test as finished. This usually happens on synchronous tests (i.e. tests not returning a Promise nor taking a done argument), which leave some stuff.
There are a couple of things you can do to try to mitigate this:
23.0.0-beta.2) if it's a DOM test. We clear document to avoid later events to trigger once the test has finished.I cannot provide a repo as it is not open source code. It is an Angular project using the jest-angular-preset.
I have tried to use the 23.0.0-beta2 version but I get exactly the same issue. Fine on local, breaks with same error on Jenkins.
The stack trace suggests it's an issue with the chalk package that Jest uses, not with any of the tests that are being run.
It's not an issue with chalk, it's because you have left dangling async code on a sync test (or you haven't waited for all stuff on an async test). Line 72 of chalk/index.js contains a reference to a require result. Jest picks it and inlines the require call, for performance reasons, resulting in:
require('ansi-styles').color.closeRe = new RegExp(escapeStringRegexp(ansiStyles.color.close), 'g');
However, your test has already finished, meaning the environment has been disposed, and any further call to require will result in a failure. Possible mitigations for this are in my comment above; the fact that does not fail locally is probably a race condition.
Ah, I understand! Thanks very much for the explanation, @mjesun
I am seeing this same thing as well while running latest CRA build script. It happens on _some_ environments(CI jobs that do not run jest for us) and always resolves after manually rm -rf node_modules.
EDIT
Related: https://github.com/wallabyjs/public/issues/1477#issuecomment-359653952
I was getting a similar error, trying to report that I had no tests.
Invoking jest via:
const jestRT = require('jest');
setTimeout(() => jestRT.run(argv), 0);
allowed the loading to complete, and get the expected error (but annoying) error about no tests.
(And thus giving me confidence that once I add some code and tests, I'll get meaningful errors).
This is something jest _could_ do for itself, though I'm not sure what uses might depend on it NOT doing it.
I think the original issues was mitigated in #5888, which will, hopefully, point back to user code or at least give a more useful error.
@BobKerns seems separate, please open up a new issue following the template
I had this issue because another module was installing ansi-styles that was an older version at the root of the projects node_modules. To fix ran the following command and everything started working again for me:
npm i --save-dev [email protected] [email protected]
Most helpful comment
I had this issue because another module was installing
ansi-stylesthat was an older version at the root of the projectsnode_modules. To fix ran the following command and everything started working again for me:npm i --save-dev [email protected] [email protected]