-
Notifications
You must be signed in to change notification settings - Fork 5k
node:test: pass a done() callback to (t, done) => {} style tests #32682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| const { test } = require("node:test"); | ||
| const assert = require("node:assert"); | ||
|
|
||
| let callCount = 0; | ||
|
|
||
| // Sync callback-style test: invokes done() to signal completion. | ||
| test("callback test resolves with done()", (t, done) => { | ||
| assert.equal(typeof done, "function"); | ||
| callCount++; | ||
| done(); | ||
| }); | ||
|
|
||
| // Async-but-callback-style test: schedules done() in a setTimeout. | ||
| test("callback test resolves with done() asynchronously", (t, done) => { | ||
| setTimeout(() => { | ||
| callCount++; | ||
| done(); | ||
| }, 5); | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win Make the async callback case observable instead of timer-based. This fixture still passes if As per coding guidelines, "Do not use 🤖 Prompt for AI AgentsSource: Coding guidelines
Comment on lines
+19
to
+28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win Replace Line 22 uses ♻️ Proposed fix return Promise.resolve().then(() => {
- setTimeout(() => {
+ setImmediate(() => {
callCount++;
sawDone = true;
done();
- }, 5);
+ });
});🤖 Prompt for AI AgentsSource: Path instructions |
||
|
|
||
| // Callback-style test that signals failure via done(err) must still report | ||
| // failure, but we cannot easily inspect that from inside the same suite. | ||
| // The shape is exercised by the next two tests instead. | ||
|
|
||
| // Existing (t) => {...} sync-style signature must keep working. | ||
| test("sync test without done() still passes", t => { | ||
| assert.equal(typeof t.name, "string"); | ||
| callCount++; | ||
| }); | ||
|
|
||
| // Existing async-function signature must keep working. | ||
| test("async test without done() still passes", async t => { | ||
| await Promise.resolve(); | ||
| callCount++; | ||
| }); | ||
|
|
||
| process.on("exit", () => { | ||
| assert.equal(callCount, 4, `expected 4 test invocations, saw ${callCount}`); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,14 @@ describe("node:test", () => { | |
| stderr: expect.stringContaining("0 fail"), | ||
| }); | ||
| }); | ||
|
|
||
| test("should pass a done() callback to tests whose function takes two parameters", async () => { | ||
| const { exitCode, stderr } = await runTests(["06-callback-tests.js"]); | ||
| expect({ exitCode, stderr }).toMatchObject({ | ||
| exitCode: 0, | ||
| stderr: expect.stringContaining("0 fail"), | ||
| }); | ||
|
Comment on lines
+56
to
+61
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win Add end-to-end coverage for the new callback error/idempotency paths. This only checks the all-pass case. The runtime change also introduced new callback-specific behavior for 🤖 Prompt for AI Agents |
||
| }); | ||
| }); | ||
|
|
||
| async function runTests(filenames: string[]) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.