From ed7e7b72dd4493d9e892b0e1b0c14e66c04036e0 Mon Sep 17 00:00:00 2001 From: Jacob Smith <3012099+JakobJingleheimer@users.noreply.github.com> Date: Sun, 8 Mar 2026 13:50:22 +0100 Subject: [PATCH 1/2] feat(learn): clean up code sample & update/correct time mocking section --- .../pages/en/learn/test-runner/mocking.md | 45 +++++++++---------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/apps/site/pages/en/learn/test-runner/mocking.md b/apps/site/pages/en/learn/test-runner/mocking.md index 1ce756866e546..dde3f5379bf36 100644 --- a/apps/site/pages/en/learn/test-runner/mocking.md +++ b/apps/site/pages/en/learn/test-runner/mocking.md @@ -149,30 +149,27 @@ This leverages [`mock`](https://nodejs.org/api/test.html#class-mocktracker) from ```mjs import assert from 'node:assert/strict'; -import { before, describe, it, mock } from 'node:test'; +import { describe, it, mock } from 'node:test'; -describe('foo', { concurrency: true }, () => { +describe('foo', { concurrency: true }, async () => { const barMock = mock.fn(); - let foo; - - before(async () => { - const barNamedExports = await import('./bar.mjs') - // discard the original default export - .then(({ default: _, ...rest }) => rest); - - // It's usually not necessary to manually call restore() after each - // nor reset() after all (node does this automatically). - mock.module('./bar.mjs', { - defaultExport: barMock, - // Keep the other exports that you don't want to mock. - namedExports: barNamedExports, - }); - // This MUST be a dynamic import because that is the only way to ensure the - // import starts after the mock has been set up. - ({ foo } = await import('./foo.mjs')); + const barNamedExports = await import('./bar.mjs') + // discard the original default export + .then(({ default: _, ...rest }) => rest); + + // It's usually not necessary to manually call restore() after each + // nor reset() after all (node does this automatically). + mock.module('./bar.mjs', { + defaultExport: barMock, + // Keep the other exports that you don't want to mock. + namedExports: barNamedExports, }); + // This MUST be a dynamic import because that is the only way to ensure the + // import starts after the mock has been set up. + const { foo } = await import('./foo.mjs'); + it('should do the thing', () => { barMock.mock.mockImplementationOnce(function bar_mock() { /* … */ @@ -258,12 +255,12 @@ Note the use of time-zone here (`Z` in the time-stamps). Neglecting to include a import assert from 'node:assert/strict'; import { describe, it, mock } from 'node:test'; -import ago from './ago.mjs'; +describe('whatever', { concurrency: true }, async () => { + mock.timers.enable({ now: new Date('2000-01-01T00:02:02Z') }); -describe('whatever', { concurrency: true }, () => { - it('should choose "minutes" when that\'s the closet unit', () => { - mock.timers.enable({ now: new Date('2000-01-01T00:02:02Z') }); + const { default: ago } = await import('./ago.mjs'); + it('should choose "minutes" when that\'s the closet unit', () => { const t = ago('1999-12-01T23:59:59Z'); assert.equal(t, '2 minutes ago'); @@ -271,4 +268,6 @@ describe('whatever', { concurrency: true }, () => { }); ``` +`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). + 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). From 51af87093bf1cada45b952fdca15f1b5136a895e Mon Sep 17 00:00:00 2001 From: Jacob Smith <3012099+JakobJingleheimer@users.noreply.github.com> Date: Sun, 8 Mar 2026 09:20:03 -0400 Subject: [PATCH 2/2] fixup!: correct typo Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Jacob Smith <3012099+JakobJingleheimer@users.noreply.github.com> --- apps/site/pages/en/learn/test-runner/mocking.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/site/pages/en/learn/test-runner/mocking.md b/apps/site/pages/en/learn/test-runner/mocking.md index dde3f5379bf36..4e67b826ff14e 100644 --- a/apps/site/pages/en/learn/test-runner/mocking.md +++ b/apps/site/pages/en/learn/test-runner/mocking.md @@ -260,7 +260,7 @@ describe('whatever', { concurrency: true }, async () => { const { default: ago } = await import('./ago.mjs'); - it('should choose "minutes" when that\'s the closet unit', () => { + it('should choose "minutes" when that\'s the closest unit', () => { const t = ago('1999-12-01T23:59:59Z'); assert.equal(t, '2 minutes ago');