question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'close' of undefined

See original GitHub issue

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"
  },

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
th317erdcommented, 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 chalk@2.4.2 ansi-styles@3.2.1

6reactions
mjesuncommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SOLVED: TypeError: Cannot read property 'close' of undefined
Hi Everyone, Hope you are enjoying the course! If anyone is experiencing the below issue like this: TypeError: Cannot read property 'close' ......
Read more >
Uncaught TypeError: Cannot read property 'close' of undefined
You have to click Open "myWindow" button before clicking the other one, because that is when the variable myWindow gets initial value.
Read more >
Uncaught TypeError: Cannot read property 'close' of undefined
Hi,. I am using the package v19.1.0.58 with <script src="https://cdn.syncfusion.com/ej2/dist/ej2.min.js"></script>. I have two new issues using Diagram (see ...
Read more >
Cannot read property 'close' of undefined · Issue #536 · doczjs ...
I've noticed that the problem appears when using docz with react-scripts and the reason is a different version of 'ansi-styles'. Currently, you can...
Read more >
Cannot Read Property of Undefined in JavaScript - Rollbar
The TypeError: Cannot read property of undefined is one of the most common type errors in JavaScript. It occurs when a property is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found