【问题标题】:UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'close' of undefinedUnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“关闭”
【发布时间】:2019-08-23 06:54:58
【问题描述】:

我正在设置 nodejs 以便使用 jest javascript 测试框架测试我的 javascript 代码。我做错了什么?

package.json文件

{
  "name": "institute-jest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-jest": "^23.4.0",
    "jest": "^23.4.1",
    "regenerator-runtime": "^0.12.0"
  }
}

在终端中运行npm test 后收到以下错误消息

(node:5088) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'close' of undefined
    at Object.<anonymous> (C:\Users\Olakunle-PC\Desktop\nodejs\HomeStudyLabs\node_modules\jest\node_modules\chalk\index.js:72:75)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at _load_chalk (C:\Users\Olakunle-PC\Desktop\nodejs\HomeStudyLabs\node_modules\jest\node_modules\jest-cli\build\cli\index.js:67:43)
    at Object.<anonymous> (C:\Users\Olakunle-PC\Desktop\nodejs\HomeStudyLabs\node_modules\jest\node_modules\jest-cli\build\cli\index.js:29:32)
(node:5088) 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:5088) [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.

【问题讨论】:

  • 你能显示你的 index.js 文件吗?我认为问题出在那个文件中

标签: javascript node.js npm jestjs


【解决方案1】:

我执行了以下操作来解决问题

  • 已删除node_modules 目录。
  • 已删除package-lock.json 文件。
  • 运行npm clear cache --force
  • 并运行npm install

【讨论】:

    【解决方案2】:

    如果你得到未定义的错误,你需要确保哪个变量抛出未定义的错误,被分配一个值给它。

    更多详情查看链接detail 以下代码可能有助于调试:

    function test(t) {     
      if (t === undefined) { 
            console.log(t.tt)
      }
      return t;    
    }
    

    【讨论】:

    • 提问者没有询问如何管理未定义或空值。他的问题是关于 jest 框架没有通过 npm test 启动。
    猜你喜欢
    • 2021-05-20
    • 2021-04-18
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 2021-05-18
    • 2021-01-15
    • 2021-03-03
    相关资源
    最近更新 更多