【问题标题】:mocha and ts-node UnhandledPromiseRejectionWarning:TypeError: Cannot convert undefined or null to objectmocha 和 ts-node UnhandledPromiseRejectionWarning:TypeError: Cannot convert undefined or null to object
【发布时间】:2020-09-16 14:44:16
【问题描述】:

我正在尝试使用 mocha 设置 ts-node,但是测试脚本总是失败。

我试过了

mocha --require ts-node/register --extensions ts,tsx --watch --watch-files src 'src/**/*.spec.{ts,tsx}'

mocha --require ts-node/register src/**/*.spec.ts

我也尝试在本地和全局安装 ts-node,但始终是输出

> project@1.0.0 test /home/moamen/foo/baz
> mocha --require ts-node/register src/**/*.spec.ts

(node:48106) UnhandledPromiseRejectionWarning: TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at Object.help (/home/moamen/foo/baz/node_modules/yargs/lib/usage.js:240:22)
    at Object.self.showHelp (/home/moamen/foo/baz/node_modules/yargs/lib/usage.js:432:15)
    at Array.<anonymous> (/home/moamen/foo/baz/node_modules/mocha/lib/cli/cli.js:53:13)
    at Object.fail (/home/moamen/foo/baz/node_modules/yargs/lib/usage.js:41:17)
    at /home/moamen/foo/baz/node_modules/yargs/lib/command.js:246:36
(Use `node --trace-warnings ...` to show where the warning was created)
(node:48106) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:48106) [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.

这是我的tsconfig.json

{
    "compilerOptions": {
        "target": "es6" ,
        "module": "commonjs",
        "sourceMap": true ,
        "outDir": "dist" ,
        "strict": true ,
        "moduleResolution": "node" ,
        "esModuleInterop": true 
        },
    "lib": ["es2015"]
}

【问题讨论】:

    标签: typescript mocha.js ts-node


    【解决方案1】:

    看到这个例子后https://github.com/mochajs/mocha-examples/tree/master/packages/typescript。问题是我在全球范围内安装了打字稿。这不应该是一个问题,但它确实是。

    【讨论】:

      【解决方案2】:

      将 typescript 模块安装为项目的依赖项为我解决了这个问题。运行命令

      npm install typescript
      

      【讨论】:

        【解决方案3】:

        我有一个非常相似的问题。我的错误是在一个打字稿测试助手中,它没有编译并导致 ts-node 很早就失败了

        【讨论】:

        • 我在 ts-node repo 上发布了一个问题,但我意识到它与 ts-node 无关,它与 mocha 有关。
        猜你喜欢
        • 1970-01-01
        • 2021-06-13
        • 1970-01-01
        • 1970-01-01
        • 2017-01-05
        • 2021-06-02
        • 1970-01-01
        • 2013-02-11
        • 2020-06-01
        相关资源
        最近更新 更多