c8 icon indicating copy to clipboard operation
c8 copied to clipboard

By default coverage shows 0% if tests are written with the .mts extension. (Edited)

Open SmashingQuasar opened this issue 3 years ago • 3 comments

  • 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 avatar Nov 13 '22 15:11 SmashingQuasar

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

blephy avatar Nov 21 '22 18:11 blephy

@SmashingQuasar have you been able to test @blephy's recommendation?

bcoe avatar Feb 14 '23 18:02 bcoe

@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!

SmashingQuasar avatar Feb 24 '23 17:02 SmashingQuasar