Skip to content

Commit 9759b8e

Browse files
authored
refactor(test): use Explicit Resource Management for mocks/spys (#12020)
1 parent 7518645 commit 9759b8e

25 files changed

Lines changed: 298 additions & 300 deletions

File tree

packages/docusaurus-babel/src/__tests__/babelTranslationsExtractor.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ const default => {
4545
`,
4646
});
4747

48-
const errorMock = vi.spyOn(console, 'error').mockImplementation(() => {});
48+
using error = vi.spyOn(console, 'error');
4949

5050
await expect(
5151
extractSourceCodeFileTranslations(sourceCodeFilePath, TestBabelOptions),
5252
).rejects.toThrow();
5353

54-
expect(errorMock).toHaveBeenCalledWith(
54+
expect(error).toHaveBeenCalledWith(
5555
expect.stringMatching(
5656
/Error while attempting to extract Docusaurus translations from source code file at/,
5757
),

packages/docusaurus-logger/src/__tests__/index.test.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {beforeAll, describe, expect, it, vi} from 'vitest';
8+
import {describe, expect, it, vi} from 'vitest';
99
import logger from '../index';
1010

1111
// Force chalk to ANSI level 3 in tests, so output is colored even in CI
@@ -94,54 +94,53 @@ describe('interpolate', () => {
9494
});
9595

9696
describe('info', () => {
97-
const consoleMock = vi.spyOn(console, 'info').mockImplementation(() => {});
9897
it('prints objects', () => {
98+
using info = vi.spyOn(console, 'info');
9999
logger.info({a: 1});
100100
logger.info(undefined);
101101
logger.info([1, 2, 3]);
102102
logger.info(new Date(2021, 10, 13));
103-
expect(consoleMock.mock.calls).toMatchSnapshot();
103+
expect(info.mock.calls).toMatchSnapshot();
104104
});
105105
});
106106

107107
describe('warn', () => {
108-
const consoleMock = vi.spyOn(console, 'warn').mockImplementation(() => {});
109108
it('prints objects', () => {
109+
using warn = vi.spyOn(console, 'warn');
110110
logger.warn({a: 1});
111111
logger.warn(undefined);
112112
logger.warn([1, 2, 3]);
113113
logger.warn(new Date(2021, 10, 13));
114-
expect(consoleMock.mock.calls).toMatchSnapshot();
114+
expect(warn.mock.calls).toMatchSnapshot();
115115
});
116116
});
117117

118118
describe('error', () => {
119-
const consoleMock = vi.spyOn(console, 'error').mockImplementation(() => {});
120119
it('prints objects', () => {
120+
using error = vi.spyOn(console, 'error');
121121
logger.error({a: 1});
122122
logger.error(undefined);
123123
logger.error([1, 2, 3]);
124124
logger.error(new Date(2021, 10, 13));
125-
expect(consoleMock.mock.calls).toMatchSnapshot();
125+
expect(error.mock.calls).toMatchSnapshot();
126126
});
127127
});
128128

129129
describe('success', () => {
130-
const consoleMock = vi.spyOn(console, 'log').mockImplementation(() => {});
131130
it('prints objects', () => {
131+
using log = vi.spyOn(console, 'log');
132132
logger.success({a: 1});
133133
logger.success(undefined);
134134
logger.success([1, 2, 3]);
135135
logger.success(new Date(2021, 10, 13));
136-
expect(consoleMock.mock.calls).toMatchSnapshot();
136+
expect(log.mock.calls).toMatchSnapshot();
137137
});
138138
});
139139

140140
describe('report', () => {
141-
beforeAll(() => vi.clearAllMocks());
142141
it('works with all severities', () => {
143-
const consoleLog = vi.spyOn(console, 'info').mockImplementation(() => {});
144-
const consoleWarn = vi.spyOn(console, 'warn').mockImplementation(() => {});
142+
using log = vi.spyOn(console, 'info');
143+
using warn = vi.spyOn(console, 'warn');
145144
logger.report('ignore')('hey');
146145
logger.report('log')('hey');
147146
logger.report('warn')('hey');
@@ -154,12 +153,10 @@ describe('report', () => {
154153
).toThrowErrorMatchingInlineSnapshot(
155154
`[Error: Unexpected "reportingSeverity" value: foo.]`,
156155
);
157-
expect(consoleLog).toHaveBeenCalledTimes(1);
158-
expect(consoleLog).toHaveBeenCalledWith(
159-
expect.stringMatching(/.*\[INFO\].* hey/),
160-
);
161-
expect(consoleWarn).toHaveBeenCalledTimes(1);
162-
expect(consoleWarn).toHaveBeenCalledWith(
156+
expect(log).toHaveBeenCalledTimes(1);
157+
expect(log).toHaveBeenCalledWith(expect.stringMatching(/.*\[INFO\].* hey/));
158+
expect(warn).toHaveBeenCalledTimes(1);
159+
expect(warn).toHaveBeenCalledWith(
163160
expect.stringMatching(/.*\[WARNING\].* hey/),
164161
);
165162
});

packages/docusaurus-mdx-loader/src/remark/resolveMarkdownLinks/__tests__/index.test.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {beforeEach, describe, expect, it, vi} from 'vitest';
8+
import {describe, expect, it, vi} from 'vitest';
99
import * as path from 'path';
1010
import plugin from '..';
1111
import type {PluginOptions} from '../index';
@@ -184,11 +184,6 @@ this is a code block
184184
});
185185

186186
describe('onBrokenMarkdownLinks', () => {
187-
const warnMock = vi.spyOn(console, 'warn').mockImplementation(() => {});
188-
beforeEach(() => {
189-
warnMock.mockClear();
190-
});
191-
192187
async function processResolutionErrors(
193188
content: string,
194189
onBrokenMarkdownLinks: PluginOptions['onBrokenMarkdownLinks'] = 'throw',
@@ -227,6 +222,8 @@ this is a code block
227222

228223
describe('warns', () => {
229224
it('for unresolvable md and mdx link', async () => {
225+
using warn = vi.spyOn(console, 'warn');
226+
230227
/* language=markdown */
231228
const content = `
232229
[link1](link1.mdx)
@@ -251,8 +248,8 @@ this is a code block
251248
"
252249
`);
253250

254-
expect(warnMock).toHaveBeenCalledTimes(2);
255-
expect(warnMock.mock.calls).toMatchInlineSnapshot(`
251+
expect(warn).toHaveBeenCalledTimes(2);
252+
expect(warn.mock.calls).toMatchInlineSnapshot(`
256253
[
257254
[
258255
"[WARNING] Markdown link with URL \`link1.mdx\` in source file "packages/docusaurus-mdx-loader/src/remark/resolveMarkdownLinks/__tests__/docs/myFile.mdx" (2:1) couldn't be resolved.
@@ -267,6 +264,8 @@ this is a code block
267264
});
268265

269266
it('for unresolvable md and mdx link - with recovery', async () => {
267+
using warn = vi.spyOn(console, 'warn');
268+
270269
/* language=markdown */
271270
const content = `
272271
[link1](link1.mdx)
@@ -298,8 +297,8 @@ this is a code block
298297
"
299298
`);
300299

301-
expect(warnMock).toHaveBeenCalledTimes(2);
302-
expect(warnMock.mock.calls).toMatchInlineSnapshot(`
300+
expect(warn).toHaveBeenCalledTimes(2);
301+
expect(warn.mock.calls).toMatchInlineSnapshot(`
303302
[
304303
[
305304
"onBrokenMarkdownLinks called with",

packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/index.test.ts

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {beforeEach, describe, expect, it, vi} from 'vitest';
8+
import {describe, expect, it, vi} from 'vitest';
99
import * as path from 'path';
1010
import vfile from 'to-vfile';
1111
import plugin, {type PluginOptions} from '../index';
@@ -68,10 +68,10 @@ describe('transformImage plugin', () => {
6868
});
6969

7070
it('does not choke on invalid image', async () => {
71-
const errorMock = vi.spyOn(console, 'warn').mockImplementation(() => {});
71+
using warn = vi.spyOn(console, 'warn');
7272
const result = await processContent(`![invalid image](/invalid.png)`);
7373
expect(result).toMatchSnapshot();
74-
expect(errorMock).toHaveBeenCalledTimes(1);
74+
expect(warn).toHaveBeenCalledTimes(1);
7575
});
7676

7777
describe('onBrokenMarkdownImages', () => {
@@ -121,19 +121,15 @@ describe('transformImage plugin', () => {
121121
return processContent(content, {onBrokenMarkdownImages: 'warn'});
122122
}
123123

124-
const warnMock = vi.spyOn(console, 'warn').mockImplementation(() => {});
125-
beforeEach(() => {
126-
warnMock.mockClear();
127-
});
128-
129124
it('if image absolute path does not exist', async () => {
125+
using warn = vi.spyOn(console, 'warn');
130126
const result = await processWarn(fixtures.doesNotExistAbsolute);
131127
expect(result).toMatchInlineSnapshot(`
132128
"![img](/img/doesNotExist.png)
133129
"
134130
`);
135-
expect(warnMock).toHaveBeenCalledTimes(1);
136-
expect(warnMock.mock.calls).toMatchInlineSnapshot(`
131+
expect(warn).toHaveBeenCalledTimes(1);
132+
expect(warn.mock.calls).toMatchInlineSnapshot(`
137133
[
138134
[
139135
"[WARNING] Markdown image with URL \`/img/doesNotExist.png\` in source file "packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/__fixtures__/docs/myFile.mdx" (1:1) couldn't be resolved to an existing local image file.",
@@ -143,13 +139,14 @@ describe('transformImage plugin', () => {
143139
});
144140

145141
it('if image relative path does not exist', async () => {
142+
using warn = vi.spyOn(console, 'warn');
146143
const result = await processWarn(fixtures.doesNotExistRelative);
147144
expect(result).toMatchInlineSnapshot(`
148145
"![img](./doesNotExist.png)
149146
"
150147
`);
151-
expect(warnMock).toHaveBeenCalledTimes(1);
152-
expect(warnMock.mock.calls).toMatchInlineSnapshot(`
148+
expect(warn).toHaveBeenCalledTimes(1);
149+
expect(warn.mock.calls).toMatchInlineSnapshot(`
153150
[
154151
[
155152
"[WARNING] Markdown image with URL \`./doesNotExist.png\` in source file "packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/__fixtures__/docs/myFile.mdx" (1:1) couldn't be resolved to an existing local image file.",
@@ -159,13 +156,14 @@ describe('transformImage plugin', () => {
159156
});
160157

161158
it('if image @site path does not exist', async () => {
159+
using warn = vi.spyOn(console, 'warn');
162160
const result = await processWarn(fixtures.doesNotExistSiteAlias);
163161
expect(result).toMatchInlineSnapshot(`
164162
"![img](@site/doesNotExist.png)
165163
"
166164
`);
167-
expect(warnMock).toHaveBeenCalledTimes(1);
168-
expect(warnMock.mock.calls).toMatchInlineSnapshot(`
165+
expect(warn).toHaveBeenCalledTimes(1);
166+
expect(warn.mock.calls).toMatchInlineSnapshot(`
169167
[
170168
[
171169
"[WARNING] Markdown image with URL \`@site/doesNotExist.png\` in source file "packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/__fixtures__/docs/myFile.mdx" (1:1) couldn't be resolved to an existing local image file.",
@@ -175,13 +173,14 @@ describe('transformImage plugin', () => {
175173
});
176174

177175
it('if image url empty', async () => {
176+
using warn = vi.spyOn(console, 'warn');
178177
const result = await processWarn(fixtures.urlEmpty);
179178
expect(result).toMatchInlineSnapshot(`
180179
"![img]()
181180
"
182181
`);
183-
expect(warnMock).toHaveBeenCalledTimes(1);
184-
expect(warnMock.mock.calls).toMatchInlineSnapshot(`
182+
expect(warn).toHaveBeenCalledTimes(1);
183+
expect(warn.mock.calls).toMatchInlineSnapshot(`
185184
[
186185
[
187186
"[WARNING] Markdown image with empty URL found in source file "packages/docusaurus-mdx-loader/src/remark/transformImage/__tests__/__fixtures__/docs/myFile.mdx" (1:1).",
@@ -205,19 +204,16 @@ describe('transformImage plugin', () => {
205204
});
206205
}
207206

208-
const logMock = vi.spyOn(console, 'log').mockImplementation(() => {});
209-
beforeEach(() => {
210-
logMock.mockClear();
211-
});
212-
213207
it('if image absolute path does not exist', async () => {
208+
using log = vi.spyOn(console, 'log');
209+
214210
const result = await processWarn(fixtures.doesNotExistAbsolute);
215211
expect(result).toMatchInlineSnapshot(`
216212
"![new 404 alt](/404.png)
217213
"
218214
`);
219-
expect(logMock).toHaveBeenCalledTimes(1);
220-
expect(logMock.mock.calls).toMatchInlineSnapshot(`
215+
expect(log).toHaveBeenCalledTimes(1);
216+
expect(log.mock.calls).toMatchInlineSnapshot(`
221217
[
222218
[
223219
"onBrokenMarkdownImages called for ",
@@ -249,13 +245,15 @@ describe('transformImage plugin', () => {
249245
});
250246

251247
it('if image relative path does not exist', async () => {
248+
using log = vi.spyOn(console, 'log');
249+
252250
const result = await processWarn(fixtures.doesNotExistRelative);
253251
expect(result).toMatchInlineSnapshot(`
254252
"![new 404 alt](/404.png)
255253
"
256254
`);
257-
expect(logMock).toHaveBeenCalledTimes(1);
258-
expect(logMock.mock.calls).toMatchInlineSnapshot(`
255+
expect(log).toHaveBeenCalledTimes(1);
256+
expect(log.mock.calls).toMatchInlineSnapshot(`
259257
[
260258
[
261259
"onBrokenMarkdownImages called for ",
@@ -287,13 +285,15 @@ describe('transformImage plugin', () => {
287285
});
288286

289287
it('if image @site path does not exist', async () => {
288+
using log = vi.spyOn(console, 'log');
289+
290290
const result = await processWarn(fixtures.doesNotExistSiteAlias);
291291
expect(result).toMatchInlineSnapshot(`
292292
"![new 404 alt](/404.png)
293293
"
294294
`);
295-
expect(logMock).toHaveBeenCalledTimes(1);
296-
expect(logMock.mock.calls).toMatchInlineSnapshot(`
295+
expect(log).toHaveBeenCalledTimes(1);
296+
expect(log.mock.calls).toMatchInlineSnapshot(`
297297
[
298298
[
299299
"onBrokenMarkdownImages called for ",
@@ -325,13 +325,15 @@ describe('transformImage plugin', () => {
325325
});
326326

327327
it('if image url empty', async () => {
328+
using log = vi.spyOn(console, 'log');
329+
328330
const result = await processWarn(fixtures.urlEmpty);
329331
expect(result).toMatchInlineSnapshot(`
330332
"![new 404 alt](/404.png)
331333
"
332334
`);
333-
expect(logMock).toHaveBeenCalledTimes(1);
334-
expect(logMock.mock.calls).toMatchInlineSnapshot(`
335+
expect(log).toHaveBeenCalledTimes(1);
336+
expect(log.mock.calls).toMatchInlineSnapshot(`
335337
[
336338
[
337339
"onBrokenMarkdownImages called for ",

0 commit comments

Comments
 (0)