Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'close' of undefined #6068

Closed
Ant59 opened this issue Apr 25, 2018 · 13 comments
Closed

TypeError: Cannot read property 'close' of undefined #6068

Ant59 opened this issue Apr 25, 2018 · 13 comments

Comments

@Ant59
Copy link

Ant59 commented Apr 25, 2018

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"
  },
@SimenB
Copy link
Member

SimenB commented Apr 25, 2018

Without a reproduction this isn't actionable

@Ant59
Copy link
Author

Ant59 commented Apr 26, 2018

How can I best provide the info that is needed?

@SimenB
Copy link
Member

SimenB commented Apr 26, 2018

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.

@mjesun
Copy link
Contributor

mjesun commented Apr 26, 2018

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:

  • Try the latest Jest beta ( 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.
  • Try using fake timers, as they will prevent them from running.

@Ant59
Copy link
Author

Ant59 commented Apr 27, 2018

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.

@mjesun
Copy link
Contributor

mjesun commented Apr 27, 2018

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.

@Ant59
Copy link
Author

Ant59 commented Apr 30, 2018

Ah, I understand! Thanks very much for the explanation, @mjesun

@nfantone
Copy link

nfantone commented Dec 2, 2018

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: wallabyjs/public#1477 (comment)

@BobKerns
Copy link

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.

@SimenB
Copy link
Member

SimenB commented Apr 19, 2019

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

@SimenB SimenB closed this as completed Apr 19, 2019
@th317erd
Copy link

th317erd commented Oct 3, 2019

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]

@kevcruzX
Copy link

@th317erd You have saved my day. I am totally grateful.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants