简体   繁体   English

Mocha 和 ts-node 给出“不支持的参数”

[英]Mocha and ts-node gives 'Unsupported Args'

In my package.json I have defined my test script:在我的package.json我定义了我的测试脚本:

"scripts": {
    "test": "mocha --require ts-node/register ./test/**/*.ts",
    "build": "npx tsc"
}

When I run npm test I get back the result:当我运行npm test我得到了结果:

> mocha --require ts-node/register ./test/**/*.ts

error:   Unsupported Args: --require ts-node/register ./test/**/*.ts

It seems to somehow be the command interaction.它似乎以某种方式是命令交互。 Even if I run it manually node .\\node_modules\\mocha\\bin\\mocha --require ts-node/register "./test/**/*.ts" it fails with the same message.即使我手动运行它node .\\node_modules\\mocha\\bin\\mocha --require ts-node/register "./test/**/*.ts"node .\\node_modules\\mocha\\bin\\mocha --require ts-node/register "./test/**/*.ts"失败并显示相同的消息。 If I remove --require ts-node/register it runs, but fails when running the test on import statements because my test files are Typescript files.如果我删除--require ts-node/register它会运行,但在对import语句运行测试时会失败,因为我的测试文件是 Typescript 文件。

How do I make mocha work with ts-node?如何让 mocha 与 ts-node 一起工作?

I was unable to find an answer to how to solve it with the command line, but I circumvented the problem by moving the options into package.json我无法找到如何使用命令行解决它的答案,但我通过将选项移动到 package.json 来规避这个问题

"scripts": {
    "test": "mocha"
},
"mocha": {
    "extension": ["ts"],
    "spec": "test/**/*.ts",
    "require": "ts-node/register"
}

My package.json file has it in scripts我的package.json文件在scripts有它

"scripts": {
        "test": "./node_modules/mocha/bin/mocha -r ts-node/register tests/test.ts"
    },

And after this I can run npm test and everything seems good.在此之后,我可以运行npm test ,一切似乎都很好。

Versions in devDependencies : devDependencies版本:

"devDependencies": {
    "@types/chai-http": "^4.2.0",
    "@types/expect": "^24.3.0",
    "@types/mocha": "^9.0.0",
    "@types/chai": "^4.2.18",
    "@types/node": "14.14.30",
    "chai": "^4.3.4",
    "mocha": "^9.1.3",
    "ts-node": "^9.1.1",
    "typescript": "^4.4.4"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM