-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
test: refactor test runner run plan tests #57304
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
Merged
nodejs-github-bot
merged 7 commits into
nodejs:main
from
pmarchini:test/refactor-test-runner-plan-tests
Mar 10, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d28ef6a
test: test runner run plan
pmarchini 245ad8b
test: add timeout tests for t.plan
pmarchini e9ea918
test: add input validation
pmarchini dab9a7f
test_runner: replace test-runner-run-plan with test-runner-plan
pmarchini 48c5c4f
test_runner: remove duplicate tests
pmarchini ad45621
test: remove forceExit option in test runner plans
pmarchini 97f1057
test: update testFixtures path
pmarchini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import test from 'node:test'; | ||
|
||
test('less assertions than planned', (t) => { | ||
t.plan(2); | ||
t.assert.ok(true, 'only one assertion'); | ||
// Missing second assertion | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import test from 'node:test'; | ||
|
||
test('matching assertions', (t) => { | ||
t.plan(2); | ||
t.assert.ok(true, 'first assertion'); | ||
t.assert.ok(true, 'second assertion'); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import test from 'node:test'; | ||
|
||
test('more assertions than planned', (t) => { | ||
t.plan(1); | ||
t.assert.ok(true, 'first assertion'); | ||
t.assert.ok(true, 'extra assertion'); // This should cause failure | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import test from 'node:test'; | ||
|
||
test('deeply nested tests', async (t) => { | ||
t.plan(1); | ||
|
||
await t.test('level 1', async (t) => { | ||
t.plan(1); | ||
|
||
await t.test('level 2', (t) => { | ||
pmarchini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.plan(1); | ||
t.assert.ok(true, 'deepest assertion'); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import test from 'node:test'; | ||
|
||
test('failing planning by options', { plan: 1 }, () => { | ||
// Should fail - no assertions | ||
}); | ||
|
||
test('passing planning by options', { plan: 1 }, (t) => { | ||
t.assert.ok(true); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import test from 'node:test'; | ||
import { Readable } from 'node:stream'; | ||
|
||
test('planning with streams', (t, done) => { | ||
function* generate() { | ||
yield 'a'; | ||
yield 'b'; | ||
yield 'c'; | ||
} | ||
const expected = ['a', 'b', 'c']; | ||
t.plan(expected.length); | ||
const stream = Readable.from(generate()); | ||
stream.on('data', (chunk) => { | ||
t.assert.strictEqual(chunk, expected.shift()); | ||
}); | ||
|
||
stream.on('end', () => { | ||
done(); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import test from 'node:test'; | ||
|
||
test('parent test', async (t) => { | ||
t.plan(1); | ||
await t.test('child test', (t) => { | ||
pmarchini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.plan(1); | ||
t.assert.ok(true, 'child assertion'); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import test from 'node:test'; | ||
|
||
test('planning with wait should PASS within timeout', async (t) => { | ||
t.plan(1, { wait: 5000 }); | ||
setTimeout(() => { | ||
t.assert.ok(true); | ||
}, 250); | ||
}); | ||
|
||
test('planning with wait should FAIL within timeout', async (t) => { | ||
t.plan(1, { wait: 5000 }); | ||
setTimeout(() => { | ||
t.assert.ok(false); | ||
}, 250); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import test from 'node:test'; | ||
|
||
test('planning should FAIL when wait time expires before plan is met', (t) => { | ||
t.plan(2, { wait: 500 }); | ||
setTimeout(() => { | ||
pmarchini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.assert.ok(true); | ||
}, 30_000).unref(); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import test from 'node:test'; | ||
|
||
test('should not wait for assertions and fail immediately', async (t) => { | ||
t.plan(1, { wait: false }); | ||
|
||
// Set up an async operation that won't complete before the test finishes | ||
// Since wait:false, the test should fail immediately without waiting | ||
setTimeout(() => { | ||
t.assert.ok(true); | ||
}, 1000).unref(); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import test from 'node:test'; | ||
|
||
test('should pass when assertions are eventually met', async (t) => { | ||
t.plan(1, { wait: true }); | ||
|
||
setTimeout(() => { | ||
t.assert.ok(true); | ||
}, 250); | ||
}); | ||
atlowChemi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
test('should fail when assertions fail', async (t) => { | ||
t.plan(1, { wait: true }); | ||
|
||
setTimeout(() => { | ||
t.assert.ok(false); | ||
}, 250).unref(); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
import * as common from '../common/index.mjs'; | ||
import * as fixtures from '../common/fixtures.mjs'; | ||
import { describe, it, run } from 'node:test'; | ||
import { join } from 'node:path'; | ||
|
||
const testFixtures = fixtures.path('test-runner', 'plan'); | ||
|
||
describe('input validation', () => { | ||
it('throws if options is not an object', (t) => { | ||
t.assert.throws(() => { | ||
t.plan(1, null); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
message: /The "options" argument must be of type object/, | ||
}); | ||
}); | ||
|
||
it('throws if options.wait is not a number or a boolean', (t) => { | ||
t.assert.throws(() => { | ||
t.plan(1, { wait: 'foo' }); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
message: /The "options\.wait" property must be one of type boolean or number\. Received type string/, | ||
}); | ||
}); | ||
|
||
it('throws if count is not a number', (t) => { | ||
t.assert.throws(() => { | ||
t.plan('foo'); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
message: /The "count" argument must be of type number/, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('test planning', () => { | ||
it('should pass when assertions match plan', async () => { | ||
const stream = run({ | ||
files: [join(testFixtures, 'match.mjs')] | ||
}); | ||
|
||
stream.on('test:fail', common.mustNotCall()); | ||
stream.on('test:pass', common.mustCall(1)); | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
for await (const _ of stream); | ||
}); | ||
|
||
it('should fail when less assertions than planned', async () => { | ||
const stream = run({ | ||
files: [join(testFixtures, 'less.mjs')] | ||
}); | ||
|
||
stream.on('test:fail', common.mustCall(1)); | ||
stream.on('test:pass', common.mustNotCall()); | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
for await (const _ of stream); | ||
}); | ||
|
||
it('should fail when more assertions than planned', async () => { | ||
const stream = run({ | ||
files: [join(testFixtures, 'more.mjs')] | ||
}); | ||
|
||
stream.on('test:fail', common.mustCall(1)); | ||
stream.on('test:pass', common.mustNotCall()); | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
for await (const _ of stream); | ||
}); | ||
|
||
it('should handle plan with subtests correctly', async () => { | ||
const stream = run({ | ||
files: [join(testFixtures, 'subtest.mjs')] | ||
}); | ||
|
||
stream.on('test:fail', common.mustNotCall()); | ||
stream.on('test:pass', common.mustCall(2)); // Parent + child test | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
for await (const _ of stream); | ||
}); | ||
|
||
it('should handle plan via options', async () => { | ||
const stream = run({ | ||
files: [join(testFixtures, 'plan-via-options.mjs')] | ||
}); | ||
|
||
stream.on('test:fail', common.mustCall(1)); | ||
stream.on('test:pass', common.mustCall(1)); | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
for await (const _ of stream); | ||
}); | ||
|
||
it('should handle streaming with plan', async () => { | ||
const stream = run({ | ||
files: [join(testFixtures, 'streaming.mjs')] | ||
}); | ||
|
||
stream.on('test:fail', common.mustNotCall()); | ||
stream.on('test:pass', common.mustCall(1)); | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
for await (const _ of stream); | ||
}); | ||
|
||
it('should handle nested subtests with plan', async () => { | ||
const stream = run({ | ||
files: [join(testFixtures, 'nested-subtests.mjs')] | ||
}); | ||
|
||
stream.on('test:fail', common.mustNotCall()); | ||
stream.on('test:pass', common.mustCall(3)); // Parent + 2 levels of nesting | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
for await (const _ of stream); | ||
}); | ||
|
||
describe('with timeout', () => { | ||
it('should handle basic timeout scenarios', async () => { | ||
const stream = run({ | ||
files: [join(testFixtures, 'timeout-basic.mjs')] | ||
}); | ||
|
||
stream.on('test:fail', common.mustCall(1)); | ||
stream.on('test:pass', common.mustCall(1)); | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
for await (const _ of stream); | ||
}); | ||
|
||
it('should fail when timeout expires before plan is met', async (t) => { | ||
const stream = run({ | ||
files: [join(testFixtures, 'timeout-expired.mjs')] | ||
}); | ||
|
||
stream.on('test:fail', common.mustCall(1)); | ||
stream.on('test:pass', common.mustNotCall()); | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
for await (const _ of stream); | ||
}); | ||
|
||
it('should handle wait:true option specifically', async () => { | ||
const stream = run({ | ||
files: [join(testFixtures, 'timeout-wait-true.mjs')] | ||
}); | ||
|
||
stream.on('test:fail', common.mustCall(1)); | ||
stream.on('test:pass', common.mustCall(1)); | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
for await (const _ of stream); | ||
}); | ||
|
||
it('should handle wait:false option (should not wait)', async () => { | ||
const stream = run({ | ||
files: [join(testFixtures, 'timeout-wait-false.mjs')] | ||
}); | ||
|
||
stream.on('test:fail', common.mustCall(1)); // Fails because plan is not met immediately | ||
stream.on('test:pass', common.mustNotCall()); | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
for await (const _ of stream); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.