Skip to content

Commit abf5a98

Browse files
feat(learn): clean up code sample & update/correct time mocking section (#8707)
* feat(learn): clean up code sample & update/correct time mocking section * fixup!: correct typo Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Jacob Smith <3012099+JakobJingleheimer@users.noreply.github.com> --------- Signed-off-by: Jacob Smith <3012099+JakobJingleheimer@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent ca965dd commit abf5a98

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

apps/site/pages/en/learn/test-runner/mocking.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -149,30 +149,27 @@ This leverages [`mock`](https://nodejs.org/api/test.html#class-mocktracker) from
149149

150150
```mjs
151151
import assert from 'node:assert/strict';
152-
import { before, describe, it, mock } from 'node:test';
152+
import { describe, it, mock } from 'node:test';
153153

154-
describe('foo', { concurrency: true }, () => {
154+
describe('foo', { concurrency: true }, async () => {
155155
const barMock = mock.fn();
156-
let foo;
157-
158-
before(async () => {
159-
const barNamedExports = await import('./bar.mjs')
160-
// discard the original default export
161-
.then(({ default: _, ...rest }) => rest);
162-
163-
// It's usually not necessary to manually call restore() after each
164-
// nor reset() after all (node does this automatically).
165-
mock.module('./bar.mjs', {
166-
defaultExport: barMock,
167-
// Keep the other exports that you don't want to mock.
168-
namedExports: barNamedExports,
169-
});
170156

171-
// This MUST be a dynamic import because that is the only way to ensure the
172-
// import starts after the mock has been set up.
173-
({ foo } = await import('./foo.mjs'));
157+
const barNamedExports = await import('./bar.mjs')
158+
// discard the original default export
159+
.then(({ default: _, ...rest }) => rest);
160+
161+
// It's usually not necessary to manually call restore() after each
162+
// nor reset() after all (node does this automatically).
163+
mock.module('./bar.mjs', {
164+
defaultExport: barMock,
165+
// Keep the other exports that you don't want to mock.
166+
namedExports: barNamedExports,
174167
});
175168

169+
// This MUST be a dynamic import because that is the only way to ensure the
170+
// import starts after the mock has been set up.
171+
const { foo } = await import('./foo.mjs');
172+
176173
it('should do the thing', () => {
177174
barMock.mock.mockImplementationOnce(function bar_mock() {
178175
/**/
@@ -258,17 +255,19 @@ Note the use of time-zone here (`Z` in the time-stamps). Neglecting to include a
258255
import assert from 'node:assert/strict';
259256
import { describe, it, mock } from 'node:test';
260257

261-
import ago from './ago.mjs';
258+
describe('whatever', { concurrency: true }, async () => {
259+
mock.timers.enable({ now: new Date('2000-01-01T00:02:02Z') });
262260

263-
describe('whatever', { concurrency: true }, () => {
264-
it('should choose "minutes" when that\'s the closet unit', () => {
265-
mock.timers.enable({ now: new Date('2000-01-01T00:02:02Z') });
261+
const { default: ago } = await import('./ago.mjs');
266262

263+
it('should choose "minutes" when that\'s the closest unit', () => {
267264
const t = ago('1999-12-01T23:59:59Z');
268265

269266
assert.equal(t, '2 minutes ago');
270267
});
271268
});
272269
```
273270

271+
`ago` **must** be imported dynamically _after_ `mock.timers` is enabled. As with all module dependency mocking, this is necessary so that the `ago` module receives the mock before the `ago` module is executed (if the mocking does not occur before, it will be too late).
272+
274273
This is especially useful when comparing against a static fixture (that is checked into a repository), such as in [snapshot testing](https://nodejs.org/api/test.html#snapshot-testing).

0 commit comments

Comments
 (0)