Skip to content

Commit a58774c

Browse files
committed
test: use afterEach for mock cleanup to prevent leaks
1 parent 55dafbe commit a58774c

2 files changed

Lines changed: 6 additions & 8 deletions

File tree

src/utils/__tests__/generators.test.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from 'node:assert/strict';
2-
import { describe, it, mock } from 'node:test';
2+
import { describe, it, mock, afterEach } from 'node:test';
33

44
import {
55
groupNodesByModule,
@@ -144,6 +144,8 @@ describe('buildApiDocURL', () => {
144144
});
145145

146146
describe('createLazyGenerator', () => {
147+
afterEach(() => mock.restoreAll());
148+
147149
it('spreads metadata properties onto the returned object', () => {
148150
const metadata = { name: 'ast', version: '1.0.0', dependsOn: undefined };
149151
const gen = createLazyGenerator(metadata);
@@ -163,7 +165,6 @@ describe('createLazyGenerator', () => {
163165
const gen = createLazyGenerator({ name: 'ast' });
164166
const result = await gen.generate('hello');
165167
assert.equal(result, 'processed:hello');
166-
mock.restoreAll();
167168
});
168169

169170
it('exposes a processChunk function that delegates to the lazily loaded module', async () => {
@@ -177,6 +178,5 @@ describe('createLazyGenerator', () => {
177178
const gen = createLazyGenerator({ name: 'ast' });
178179
const result = await gen.processChunk(['a', 'b', 'c'], [0, 2]);
179180
assert.deepStrictEqual(result, ['a', 'c']);
180-
mock.restoreAll();
181181
});
182182
});

src/utils/__tests__/url.test.mjs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import assert from 'node:assert/strict';
2-
import { describe, it, mock } from 'node:test';
2+
import { describe, it, mock, afterEach } from 'node:test';
33

4-
// Mock node:fs/promises so we don't touch the real filesystem
54
let fileContent = 'hello from file';
65
mock.module('node:fs/promises', {
76
namedExports: {
87
readFile: async () => fileContent,
98
},
109
});
1110

12-
// Mock node:url passthrough (pathToFileURL is used inside importFromURL)
13-
// We load after mocking so the module picks up our stubs
1411
const { toParsedURL, loadFromURL } = await import('../url.mjs');
1512

1613
describe('toParsedURL', () => {
@@ -31,6 +28,8 @@ describe('toParsedURL', () => {
3128
});
3229

3330
describe('loadFromURL', () => {
31+
afterEach(() => mock.restoreAll());
32+
3433
it('should read content from the filesystem for a plain file path', async () => {
3534
fileContent = 'file content';
3635
const result = await loadFromURL('/some/path/file.txt');
@@ -50,6 +49,5 @@ describe('loadFromURL', () => {
5049

5150
const result = await loadFromURL('https://nodejs.org/data.txt');
5251
assert.strictEqual(result, 'fetched content');
53-
mock.restoreAll();
5452
});
5553
});

0 commit comments

Comments
 (0)