typescript mocha和ts节点未处理承诺拒绝警告:类型错误:无法将undefined或null转换为对象

r1zhe5dt  于 2023-03-04  发布在  TypeScript
关注(0)|答案(4)|浏览(106)

我试图设置ts节点与摩卡然而测试脚本总是失败。
我试过了

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"]
}
ev7lccsx

ev7lccsx1#

在看了这个https://github.com/mochajs/mocha-examples/tree/master/packages/typescript的例子之后,问题是我全局安装了typescript,这本不应该是个问题,但它确实是。

hc8w905p

hc8w905p2#

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

npm install typescript
q35jwt9p

q35jwt9p3#

我也遇到过类似的问题,我的错误是在一个不能编译的typescript测试助手中,导致ts-node很早就失败了

vjrehmav

vjrehmav4#

我最近得到了这个问题,它与摩卡咖啡有关。它看起来已经被这个变化修复了。https://github.com/mochajs/mocha/pull/4311
使用最新的摩卡版本解决了这个问题。

"mocha": "10.2.0",

相关问题