By default coverage shows 0% if tests are written with the .mts extension. (Edited)
- Version: v18.12.1
- Platform: Linux ---- 5.10.0-9-amd64 1 SMP Debian 5.10.70-1 (2021-09-30) x86_64 GNU/Linux
Hey!
I am using Mocha,Chai and Sinon with TypeScript 4.8 on Node 18.12.
My code is written purely in ESM using the .mts file extension.
To run my tests I am using the following command: NODE_ENV=test TS_NODE_PROJECT=./tsconfig.mocha.json c8 mocha --parallel
Here is my .mocharc.json file:
{
"extension": [
"ts"
],
"node-option": [
"experimental-specifier-resolution=node",
"loader=ts-node/esm",
"loader=esmock"
],
"spec": [
"__tests__/**/*.spec.mts"
],
"timeout": 5000,
"parallel": true,
"checkLeaks": true,
"diff": true,
"forbidPending": true
}
Here is my .c8rc.json file:
{
"src": "./src",
"exclude": [
"*Interface.mts"
],
"reporter": [
"html"
],
"report-dir": "./coverage",
"skip-full": true,
"check-coverage": true,
"all": true,
"statements": 100,
"branches": 100,
"functions": 100,
"lines": 100
}
This is the full tsconfig.mocha.json file:
{
"compilerOptions": {
"outDir": "build",
"allowJs": false,
"target": "ESNext",
"alwaysStrict": true,
"removeComments": true,
"strict": true,
"charset": "UTF-8",
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"allowUnusedLabels": false,
"module": "NodeNext",
"allowUnreachableCode": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"newLine": "LF",
"moduleResolution": "NodeNext",
"useUnknownInCatchVariables": true,
"lib": ["ESNext"],
"noUncheckedIndexedAccess": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"forceConsistentCasingInFileNames": true,
"listFiles": false,
"listEmittedFiles": true,
"noErrorTruncation": true,
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"pretty": true,
"sourceMap": true
},
"include": [".", "./**/.*", "./**/*.json"],
"ts-node": {
"files": true,
"swc": true
}
As you can see, I am using SWC to make ts-node work faster.
I have multiple test files. If I use the .spec.ts extension for a specific test file, then c8 takes it into account.
However the instant I use the .spec.mts file extension, this test file is ignored in coverage.
~Is there a way to make c8 recognize the .mts extension?~
I figured it out by adding specifically "extension": [".mts"] to my .c8rc.json file but I suggest to make a change to add the .mjs, .cjs, .mts and .cts extensions recognized by default since those are standard.
Thanks!
@SmashingQuasar hello 😆
did you try with (in c8 config)
"src": "src/**/*", // without ./
"exclude": [
"**/*Interface.mts" // without ./
],
i had already noticed that src with a relative path do not work properly.
@SmashingQuasar have you been able to test @blephy's recommendation?
@bcoe Sorry for the delay. @blephy and I were working in the same company so we worked it out via pair programming. :) I do not remember how we fixed it and I have left the company since then so I cannot look it up. Maybe blephy can tell you about it, sorry!