piotrwitek / ts-mocha

Mocha thin wrapper that allows running TypeScript tests with TypeScript runtime (ts-node) to get rid of compilation complexity
MIT License
190 stars 25 forks source link

ts-mocha vs mocha --require ts-node/register #62

Open 0-sv opened 3 years ago

0-sv commented 3 years ago

Hey, I'm having a hard time understanding what the difference is between these two commands. Basically, I've configured two tsconfig.json files and I use the --project parameter to import them separately.

The performance is much better with ts-mocha, thank you for that. But I see differences in behaviour, and I'm having a hard time wrapping my head around it...

package.json:

"test": "NODE_ENV=test mocha --timeout 25000 -r ts-node/register -r ts-custom-error src/*.test.* --exit",
"test:strict": "NODE_ENV=test ts-mocha --project ./tsconfig.strict.json --timeout 25000 -r ts-custom-error src/*.test.* --exit"

tsconfig.strict.json:

{
    "extends": "./tsconfig.json",
    "compilerOptions": {
        "strict": true
    }
}

Scenario 1

npm run test with strict: true: I get a lot of TS errors (this is expected)

Scenario 2

npm run test with strict: false: execution but worser performance than with ts-mocha

Scenario 3

npm run test:strict with strict: true: I get no TS errors and normal compilation, albeit fast performance (I expect a lot of TS errors, but don't get any)

Scenario 4

npm run test:strict with strict: false: I get no TS errors and normal compilation, albeit fast performance.

0-sv commented 3 years ago

This seems to work:

"test:strict": "TS_NODE_PROJECT='./tsconfig.strict.json' NODE_ENV=test mocha -r ts-node/register  --timeout 25000 -r ts-custom-error src/*.test.* --exit"

But it doesn't work with ts-mocha as I expect with the --project flag:

"test:strict": "NODE_ENV=test ts-mocha -p ./tsconfig.strict.json --timeout 25000 -r ts-custom-error src/*.test.* --exit"
0-sv commented 3 years ago

I also have access to some environment variables, listed here: https://github.com/TypeStrong/ts-node/tree/629525474fb548fddb7a90a48ab795a9199b1997#cli-and-programmatic-options. Does this also work for ts-mocha?