connor4312 / nodejs-testing

VS Code integration for node:test native tests
MIT License
43 stars 6 forks source link

Test cases not found if using CommonJS instead of ES6 #42

Open Tkong1 opened 3 weeks ago

Tkong1 commented 3 weeks ago

I am new to node:test and wanted to use the extension to execute my first node unit tests. I am usually using CommonJS module style for my node.js projects. But i was only able to see the tests on the Testing window in VSCode if i use the ES6 module style.

Setup: OS: Ubuntu 22.04 VS-Code: v1.90.1 Node.js: v22.3.0 node:test runner: v1.5.1

Test file (ES6): /test/test.js

import { test } from "node:test";

test("will pass", () => {
  console.log("hello world");
});

test("will fail", () => {
  throw new Error("fail");
});

2024-06-17 21-00-55

Test file (CommonJS): test/test.js

const test = require('node:test');

test("will pass", () => {
  console.log("hello world");
});

test("will fail", () => {
  throw new Error("fail");
});

2024-06-17 20-59-01

When i run the node:test's with the CLI it works with CommonJS module style.

2024-06-17 21-05-48

connor4312 commented 3 weeks ago

You want to change

const test = require('node:test');

to

const { test } = require('node:test');
Tkong1 commented 3 weeks ago

Thanks a lot for this tip, this works. Even if i don't really know what the issue with const test = require('node:test'); is.

connor4312 commented 3 weeks ago

We don't handle that case, I didn't know they default-export the test function. I'll add support for that.