Skip to content

Commit 7a6f376

Browse files
fix: example with streams and when over planning
1 parent c875b18 commit 7a6f376

File tree

3 files changed

+68
-11
lines changed

3 files changed

+68
-11
lines changed

doc/api/test.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,6 +2992,26 @@ test('top level test', (t) => {
29922992
});
29932993
```
29942994

2995+
When working with asynchronous code, the `plan` function can be used to ensure that the
2996+
correct number of assertions are run:
2997+
2998+
```js
2999+
test('planning with streams', async (t) => {
3000+
function* generate() {
3001+
yield 'a';
3002+
yield 'b';
3003+
yield 'c';
3004+
}
3005+
const expected = ['a', 'b', 'c'];
3006+
t.plan(expected.length);
3007+
const stream = Readable.from(generate());
3008+
stream.on('data', (chunk) => {
3009+
t.assert.strictEqual(chunk, expected.shift());
3010+
});
3011+
await once(stream, 'end');
3012+
})
3013+
```
3014+
29953015
### `context.runOnly(shouldRunOnlyTests)`
29963016

29973017
<!-- YAML

test/fixtures/test-runner/output/test-runner-plan.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22
const { test } = require('node:test');
3+
const { Readable } = require('node:stream');
4+
const { once } = require('node:events');
35

46
test('test planning basic', (t) => {
57
t.plan(2);
@@ -19,7 +21,7 @@ test('more assertions than planned', (t) => {
1921

2022
test('subtesting', (t) => {
2123
t.plan(1);
22-
t.test('subtest', () => {});
24+
t.test('subtest', () => { });
2325
});
2426

2527
test('subtesting correctly', (t) => {
@@ -32,18 +34,18 @@ test('subtesting correctly', (t) => {
3234
});
3335

3436
test('correctly ignoring subtesting plan', (t) => {
35-
t.plan(1);
36-
t.test('subtest', (st) => {
37-
st.plan(1);
38-
st.assert.ok(true);
39-
});
37+
t.plan(1);
38+
t.test('subtest', (st) => {
39+
st.plan(1);
40+
st.assert.ok(true);
41+
});
4042
});
4143

4244
test('failing planning by options', { plan: 1 }, () => {
4345
});
4446

4547
test('not failing planning by options', { plan: 1 }, (t) => {
46-
t.assert.ok(true);
48+
t.assert.ok(true);
4749
});
4850

4951
test('subtest planning by options', (t) => {
@@ -52,3 +54,24 @@ test('subtest planning by options', (t) => {
5254
});
5355
});
5456

57+
test('failing more assertions than planned', (t) => {
58+
t.plan(2);
59+
t.assert.ok(true);
60+
t.assert.ok(true);
61+
t.assert.ok(true);
62+
});
63+
64+
test('planning with streams', async (t) => {
65+
function* generate() {
66+
yield 'a';
67+
yield 'b';
68+
yield 'c';
69+
}
70+
const expected = ['a', 'b', 'c'];
71+
t.plan(expected.length);
72+
const stream = Readable.from(generate());
73+
stream.on('data', (chunk) => {
74+
t.assert.strictEqual(chunk, expected.shift());
75+
});
76+
await once(stream, 'end');
77+
})

test/fixtures/test-runner/output/test-runner-plan.snapshot

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,25 @@ ok 9 - subtest planning by options
8080
---
8181
duration_ms: *
8282
...
83-
1..9
84-
# tests 13
83+
# Subtest: failing more assertions than planned
84+
not ok 10 - failing more assertions than planned
85+
---
86+
duration_ms: *
87+
location: '/test/fixtures/test-runner/output/test-runner-plan.js:(LINE):1'
88+
failureType: 'testCodeFailure'
89+
error: 'plan expected 2 assertions but received 3'
90+
code: 'ERR_TEST_FAILURE'
91+
...
92+
# Subtest: planning with streams
93+
ok 11 - planning with streams
94+
---
95+
duration_ms: *
96+
...
97+
1..11
98+
# tests 15
8599
# suites 0
86-
# pass 10
87-
# fail 3
100+
# pass 11
101+
# fail 4
88102
# cancelled 0
89103
# skipped 0
90104
# todo 0

0 commit comments

Comments
 (0)