简体   繁体   中英

Mocha and ts-node gives 'Unsupported Args'

In my package.json I have defined my test script:

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

When I run npm test I get back the result:

> 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. 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.

How do I make mocha work with 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

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

My package.json file has it in 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.

Versions in 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"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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