-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
test_runner: add timeout support to test plan #56765
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 9 commits
e9a7a7f
9735384
c942eb9
927ffdb
0665597
295bd0f
53fcd1e
20cfb75
33993c8
90e566f
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 |
|---|---|---|
|
|
@@ -3381,13 +3381,17 @@ added: | |
|
|
||
| The name of the test. | ||
|
|
||
| ### `context.plan(count)` | ||
| ### `context.plan(count[,options])` | ||
|
|
||
| <!-- YAML | ||
| added: | ||
| - v22.2.0 | ||
| - v20.15.0 | ||
| changes: | ||
| - version: | ||
| - REPLACEME | ||
| pr-url: https://github.com/nodejs/node/pull/56765 | ||
| description: Add the `options` parameter. | ||
| - version: | ||
| - v23.4.0 | ||
| - v22.13.0 | ||
|
|
@@ -3396,6 +3400,16 @@ changes: | |
| --> | ||
|
|
||
| * `count` {number} The number of assertions and subtests that are expected to run. | ||
| * `options` {Object} Additional options for the plan. | ||
| * `wait` {boolean|number} The wait time for the plan: | ||
| * If `true`, the plan waits indefinitely for all assertions and subtests to run. | ||
| * If `false`, the plan performs an immediate check after the test function completes, | ||
| without waiting for any pending assertions or subtests. | ||
| Any assertions or subtests that complete after this check will not be counted towards the plan. | ||
| * If a number, it specifies the maximum wait time in milliseconds | ||
| before timing out while waiting for expected assertions and subtests to be matched. | ||
| If the timeout is reached, the test will fail. | ||
|
Comment on lines
+3409
to
+3411
Member
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. Should the maximal value be pointed out here?
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. Are you suggesting that there should be a maximum value possible for the wait?
Member
Author
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.
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. I don't see a need for it. If someone wants to set the timeout to
Member
Author
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. IMHO, validating the maximum value is a good way to inform the user of misuse.
Member
Author
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. @atlowChemi @jsumners If you are okay with this, I would avoid documenting this implementation detail!
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. I am good with the PR as it is.
Member
Author
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. I'll just address the last comment related to tests and restart the CIs
Member
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. @pmarchini I left this comment while approving the PR - so this is non-blocking IMHO 🙂 |
||
| **Default:** `false`. | ||
|
|
||
| This function is used to set the number of assertions and subtests that are expected to run | ||
| within the test. If the number of assertions and subtests that run does not match the | ||
|
|
@@ -3434,6 +3448,26 @@ test('planning with streams', (t, done) => { | |
| }); | ||
| ``` | ||
|
|
||
| When using the `wait` option, you can control how long the test will wait for the expected assertions. | ||
pmarchini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| For example, setting a maximum wait time ensures that the test will wait for asynchronous assertions | ||
| to complete within the specified timeframe: | ||
|
|
||
| ```js | ||
| test('plan with wait: 2000 waits for async assertions', (t) => { | ||
| t.plan(1, { wait: 2000 }); // Waits for up to 2 seconds for the assertion to complete. | ||
|
|
||
| const asyncActivity = () => { | ||
| setTimeout(() => { | ||
| t.assert.ok(true, 'Async assertion completed within the wait time'); | ||
| }, 1000); // Completes after 1 second, within the 2-second wait time. | ||
| }; | ||
|
|
||
| asyncActivity(); // The test will pass because the assertion is completed in time. | ||
| }); | ||
| ``` | ||
|
|
||
| Note: If a `wait` timeout is specified, it begins counting down only after the test function finishes executing. | ||
|
|
||
| ### `context.runOnly(shouldRunOnlyTests)` | ||
|
|
||
| <!-- YAML | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| 'use strict'; | ||
| const { describe, it } = require('node:test'); | ||
| const { platformTimeout } = require('../../../common'); | ||
|
|
||
| describe('planning with wait', () => { | ||
| it('planning with wait and passing', async (t) => { | ||
| t.plan(1, { wait: platformTimeout(5000) }); | ||
|
|
||
| const asyncActivity = () => { | ||
| setTimeout(() => { | ||
| t.assert.ok(true); | ||
| }, platformTimeout(250)); | ||
| }; | ||
|
|
||
| asyncActivity(); | ||
| }); | ||
|
|
||
| it('planning with wait and failing', async (t) => { | ||
| t.plan(1, { wait: platformTimeout(5000) }); | ||
|
|
||
| const asyncActivity = () => { | ||
| setTimeout(() => { | ||
| t.assert.ok(false); | ||
| }, platformTimeout(250)); | ||
| }; | ||
|
|
||
| asyncActivity(); | ||
| }); | ||
|
|
||
| it('planning wait time expires before plan is met', async (t) => { | ||
| t.plan(2, { wait: platformTimeout(500) }); | ||
|
|
||
| const asyncActivity = () => { | ||
| setTimeout(() => { | ||
| t.assert.ok(true); | ||
| }, platformTimeout(50_000_000)); | ||
| }; | ||
|
|
||
| asyncActivity(); | ||
| }); | ||
|
|
||
| it(`planning with wait "options.wait : true" and passing`, async (t) => { | ||
| t.plan(1, { wait: true }); | ||
|
|
||
| const asyncActivity = () => { | ||
| setTimeout(() => { | ||
| t.assert.ok(true); | ||
| }, platformTimeout(250)); | ||
| }; | ||
|
|
||
| asyncActivity(); | ||
| }); | ||
|
|
||
| it(`planning with wait "options.wait : true" and failing`, async (t) => { | ||
| t.plan(1, { wait: true }); | ||
|
|
||
| const asyncActivity = () => { | ||
| setTimeout(() => { | ||
| t.assert.ok(false); | ||
| }, platformTimeout(250)); | ||
| }; | ||
|
|
||
| asyncActivity(); | ||
| }); | ||
|
|
||
| it(`planning with wait "options.wait : false" should not wait`, async (t) => { | ||
| t.plan(1, { wait: false }); | ||
|
|
||
| const asyncActivity = () => { | ||
| setTimeout(() => { | ||
| t.assert.ok(true); | ||
| }, platformTimeout(500_000)); | ||
| }; | ||
|
|
||
| asyncActivity(); | ||
| }) | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.