connor4312 / nodejs-testing

VS Code integration for node:test native tests
MIT License
48 stars 7 forks source link

Test coverage, typescript and tsx #38

Open coderhammer opened 4 months ago

coderhammer commented 4 months ago

Hey, I'm trying the new code coverage feature, first of all, thank you for the work.

It doesn't work for me, I don't know if I'm missing some configuration. Currently using this configuration:

{
  "nodejs-testing.extensions": [
    {
      "extensions": ["mjs", "cjs", "js"],
      "parameters": []
    },
    {
      "extensions": ["mts", "cts", "ts"],
      "parameters": ["--import", "tsx"]
    }
  ]
}

Everything works well except test coverage, I only have this that appears:

Screenshot 2024-05-12 at 19 17 48

And everthing shows up as 100%:

Screenshot 2024-05-12 at 19 18 43

So it looks like something does not work. When I run the following command by hand:

node --test --enable-source-maps --experimental-test-coverage --import tsx $(find src -name '*.test.ts')

I obtain:

Screenshot 2024-05-12 at 19 19 47

So the node test runner seems to recognise my files.

Are you aware of such a thing? Can I help you with identifying the root cause?

Thank you for your time and have a nice day

connor4312 commented 4 months ago

We just toss everything into the c8 module and let it work, but it won't have had access to scripts (and sourcemaps) from the runtime.

I plan to rework coverage shortly with the addition of attributable coverage in VS Code (based on https://github.com/microsoft/vscode/pull/212325) which does not have this issue.

coderhammer commented 4 months ago

I admit I don't understand quite well all the mechanisms, but I guess I'll be waiting that this gets implemented! Thanks for taking the time to respond :) I'll try to take some time to explore how c8 works exactly and how it relates to this project

Lucas-Levandoski commented 1 week ago

Hey there @coderhammer, it seems to work just fine for me.

package.json

  "engines": {
    "node": "20.x"
  },
  "main": "dist/src/functions/index.js",
  "scripts": {
    "build": "tsc --project tsconfig.json && tscpaths -p tsconfig.json -s ./ -o ./dist",
    "watch": "tsc -w",
    "clean": "rimraf dist",
    "prestart": "npm run clean && npm run build",
    "start": "func start",
    "pretest": "npm run clean && npm run build",
    "test": "node ./dist/__tests__",
    "test:debug": "npm run pretest && node --inspect-brk ./dist/__tests__",
    "test:html": "nyc --reporter=html node dist/__tests__ && start coverage/index.html",
    "posttest": "nyc --reporter=lcov --reporter=text node dist/__tests__"
  },

settings.json (user settings)

  "nodejs-testing.extensions": [
    {
      "extensions": ["mjs", "cjs", "js"],
      "parameters": []
    },
    {
      "extensions": ["mts", "cts", "ts"],
      "parameters": ["--import", "tsx"]
    }
  ],
  "nodejs-testing.nodejsPath": "C:\\Program Files\\nodejs\\node.exe",

tsconfig.json

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Node 20",
  "include": ["src/**/*.ts", "__tests__/**/*.ts"],
  "exclude": ["dist", "node_modules/*"],

  "compilerOptions": {
    "lib": ["es2022"],
    "module": "commonjs",
    "target": "es6",
    "outDir": "dist",
    "rootDir": ".",
    "sourceMap": true,

    "strict": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,

    "baseUrl": "./src",
    "paths": {
      "Models": ["models/index"],
      "Container": ["container/index"],
      "Availability": ["modules/availability/index"],
      "Agenda": ["modules/agenda/index"],
      "Shared": ["modules/shared/index"],
      "Utils": ["utils/index"],
      "Errors": ["errors/index"],
      "Authentication": ["authentication/index"],
      "Middlewares": ["middlewares/index"],
      "Functions": ["functions/index"]
    }
  }
}

image