温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/node/test/default/TestContext/plan
点击访问原文链接

TestContext.plan method | Node.js node:test module | Bun

TestContext.plan method | Node.js node:test module | BunBuildDocsReferenceGuidesBlogDiscord/node:test/default/TestContext/planMplan

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/node:test/default/TestContext/planMplan

method

test.default.TestContext.plan Note: To make sure assertions are tracked, `t.assert` must be used instead of `assert` directly. ```js test('top level test', (t) => { t.plan(2); t.assert.ok('some relevant assertion here'); t.test('subtest', () => {}); }); ``` When working with asynchronous code, the `plan` function can be used to ensure that the correct number of assertions are run: ```js 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(); }); }); ``` When using the `wait` option, you can control how long the test will wait for the expected assertions. 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." data-algolia-static="false" data-algolia-merged="false" data-type="Method">plan(count: number,options?: TestContextPlanOptions): void;

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 expected count, the test will fail.

Note: To make sure assertions are tracked, t.assert must be used instead of assert directly.

test('top level test', (t) => {
t.plan(2);
t.assert.ok('some relevant assertion here');
t.test('subtest', () => {});
});

When working with asynchronous code, the plan function can be used to ensure that the correct number of assertions are run:

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();
});
});

When using the wait option, you can control how long the test will wait for the expected assertions. For example, setting a maximum wait time ensures that the test will wait for asynchronous assertions to complete within the specified timeframe:

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.

Resources

ReferenceDocsGuidesDiscordMerch StoreGitHubBlog 

Toolkit

RuntimePackage managerTest runnerBundlerPackage runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicense

Baked with ❤️ in San Francisco

We're hiring →

TestContext.plan method | Node.js node:test module | Bun,AI智能索引,全网链接索引,智能导航,网页索引

    API documentation for method node:test.default.TestContext.plan | Bun