Skip to content

Commit

Permalink
fix: example with streams and when over planning
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-ippolito committed May 7, 2024
1 parent c875b18 commit 7a6f376
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
20 changes: 20 additions & 0 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -2992,6 +2992,26 @@ test('top level test', (t) => {
});
```

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', async (t) => {
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());
});
await once(stream, 'end');
})

Check failure on line 3012 in doc/api/test.md

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
```

### `context.runOnly(shouldRunOnlyTests)`

<!-- YAML
Expand Down
37 changes: 30 additions & 7 deletions test/fixtures/test-runner/output/test-runner-plan.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';
const { test } = require('node:test');
const { Readable } = require('node:stream');
const { once } = require('node:events');

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

test('subtesting', (t) => {
t.plan(1);
t.test('subtest', () => {});
t.test('subtest', () => { });
});

test('subtesting correctly', (t) => {
Expand All @@ -32,18 +34,18 @@ test('subtesting correctly', (t) => {
});

test('correctly ignoring subtesting plan', (t) => {
t.plan(1);
t.test('subtest', (st) => {
st.plan(1);
st.assert.ok(true);
});
t.plan(1);
t.test('subtest', (st) => {
st.plan(1);
st.assert.ok(true);
});
});

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

test('not failing planning by options', { plan: 1 }, (t) => {
t.assert.ok(true);
t.assert.ok(true);
});

test('subtest planning by options', (t) => {
Expand All @@ -52,3 +54,24 @@ test('subtest planning by options', (t) => {
});
});

test('failing more assertions than planned', (t) => {
t.plan(2);
t.assert.ok(true);
t.assert.ok(true);
t.assert.ok(true);
});

test('planning with streams', async (t) => {
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());
});
await once(stream, 'end');
})
22 changes: 18 additions & 4 deletions test/fixtures/test-runner/output/test-runner-plan.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,25 @@ ok 9 - subtest planning by options
---
duration_ms: *
...
1..9
# tests 13
# Subtest: failing more assertions than planned
not ok 10 - failing more assertions than planned
---
duration_ms: *
location: '/test/fixtures/test-runner/output/test-runner-plan.js:(LINE):1'
failureType: 'testCodeFailure'
error: 'plan expected 2 assertions but received 3'
code: 'ERR_TEST_FAILURE'
...
# Subtest: planning with streams
ok 11 - planning with streams
---
duration_ms: *
...
1..11
# tests 15
# suites 0
# pass 10
# fail 3
# pass 11
# fail 4
# cancelled 0
# skipped 0
# todo 0
Expand Down

0 comments on commit 7a6f376

Please sign in to comment.