Skip to content

Commit a794371

Browse files
authored
test: switch to vitest (#14)
1 parent 65ddbed commit a794371

File tree

6 files changed

+2261
-3612
lines changed

6 files changed

+2261
-3612
lines changed

.github/linters/.eslintrc.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
env:
22
node: true
33
es6: true
4-
jest: true
54

65
globals:
76
Atomics: readonly
@@ -24,15 +23,15 @@ parserOptions:
2423
- './tsconfig.json'
2524

2625
plugins:
27-
- jest
26+
- vitest
2827
- '@typescript-eslint'
2928

3029
extends:
3130
- eslint:recommended
3231
- plugin:@typescript-eslint/eslint-recommended
3332
- plugin:@typescript-eslint/recommended
3433
- plugin:github/recommended
35-
- plugin:jest/recommended
34+
- plugin:vitest/legacy-recommended
3635

3736
rules:
3837
{

__tests__/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/**
22
* Unit tests for the action's entrypoint, src/index.ts
33
*/
4+
import { describe, expect, it, vi } from 'vitest';
45

56
import * as main from '../src/main';
67

78
// Mock the action's entrypoint
8-
const runMock = jest.spyOn(main, 'run').mockImplementation();
9+
const runMock = vi.spyOn(main, 'run').mockImplementation(async () => {});
910

1011
describe('index', () => {
1112
it('calls run when imported', async () => {
12-
// eslint-disable-next-line @typescript-eslint/no-require-imports
13-
require('../src/index');
13+
await import('../src/index');
1414

1515
expect(runMock).toHaveBeenCalled();
1616
});

__tests__/main.test.ts

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
2+
13
import * as fs from 'node:fs/promises';
24
import * as path from 'node:path';
35

@@ -13,40 +15,43 @@ import {
1315
mockHttpGet
1416
} from './utils';
1517

16-
jest.mock('@actions/cache');
17-
jest.mock('@actions/core');
18-
jest.mock('node:fs/promises');
18+
vi.mock('@actions/cache');
19+
vi.mock('@actions/core');
20+
vi.mock('node:fs/promises');
1921

2022
// Spy the action's entrypoint
21-
const runSpy = jest.spyOn(main, 'run');
23+
const runSpy = vi.spyOn(main, 'run');
24+
25+
let schemaContents: string;
26+
let invalidSchemaContents: string;
27+
let instanceContents: string;
2228

2329
describe('action', () => {
2430
const schema = '/foo/bar';
2531
const remoteSchema = 'https://foo.bar/schema.json';
2632
const files = ['/foo/bar/baz/**.yml'];
2733

28-
const schemaContents: string = jest
29-
.requireActual('node:fs')
30-
.readFileSync(
34+
beforeAll(async () => {
35+
// jest.mocked(core.debug).mockImplementation(console.debug);
36+
37+
const actualFs = await vi.importActual<typeof import('node:fs')>('node:fs');
38+
39+
schemaContents = actualFs.readFileSync(
3140
path.join(__dirname, 'fixtures', 'evm-config.schema.json'),
3241
'utf-8'
3342
);
34-
const invalidSchemaContents: string = jest
35-
.requireActual('node:fs')
36-
.readFileSync(
43+
invalidSchemaContents = actualFs.readFileSync(
3744
path.join(__dirname, 'fixtures', 'invalid.schema.json'),
3845
'utf-8'
3946
);
40-
const instanceContents: string = jest
41-
.requireActual('node:fs')
42-
.readFileSync(path.join(__dirname, 'fixtures', 'evm-config.yml'), 'utf-8');
43-
44-
beforeAll(() => {
45-
// jest.mocked(core.debug).mockImplementation(console.debug);
47+
instanceContents = actualFs.readFileSync(
48+
path.join(__dirname, 'fixtures', 'evm-config.yml'),
49+
'utf-8'
50+
);
4651
});
4752

4853
beforeEach(() => {
49-
jest.clearAllMocks();
54+
vi.clearAllMocks();
5055
process.exitCode = undefined;
5156
});
5257

@@ -85,7 +90,7 @@ describe('action', () => {
8590
mockGetInput({ schema });
8691
mockGetMultilineInput({ files });
8792

88-
jest.mocked(fs.readFile).mockImplementation(() => {
93+
vi.mocked(fs.readFile).mockImplementation(() => {
8994
throw new Error('File read error');
9095
});
9196

@@ -102,7 +107,7 @@ describe('action', () => {
102107
mockGetInput({ schema });
103108
mockGetMultilineInput({ files });
104109

105-
jest.mocked(fs.readFile).mockImplementation(() => {
110+
vi.mocked(fs.readFile).mockImplementation(() => {
106111
throw 42; // eslint-disable-line no-throw-literal
107112
});
108113

@@ -119,7 +124,7 @@ describe('action', () => {
119124
mockGetInput({ schema });
120125
mockGetMultilineInput({ files });
121126

122-
jest.mocked(fs.readFile).mockResolvedValueOnce(schemaContents);
127+
vi.mocked(fs.readFile).mockResolvedValueOnce(schemaContents);
123128

124129
await main.run();
125130
expect(runSpy).toHaveReturned();
@@ -133,7 +138,7 @@ describe('action', () => {
133138
mockGetInput({ schema });
134139
mockGetMultilineInput({ files });
135140

136-
jest.mocked(fs.readFile).mockResolvedValueOnce(schemaContents);
141+
vi.mocked(fs.readFile).mockResolvedValueOnce(schemaContents);
137142

138143
await main.run();
139144
expect(runSpy).toHaveReturned();
@@ -148,7 +153,7 @@ describe('action', () => {
148153
mockGetInput({ schema: remoteSchema });
149154
mockGetMultilineInput({ files });
150155

151-
jest.spyOn(cache, 'restoreCache').mockResolvedValue(undefined);
156+
vi.spyOn(cache, 'restoreCache').mockResolvedValue(undefined);
152157
const httpGetSpy = mockHttpGet(schemaContents);
153158

154159
await main.run();
@@ -181,7 +186,7 @@ describe('action', () => {
181186
mockGetInput({ schema: remoteSchema });
182187
mockGetMultilineInput({ files });
183188

184-
jest.spyOn(cache, 'restoreCache').mockResolvedValue(undefined);
189+
vi.spyOn(cache, 'restoreCache').mockResolvedValue(undefined);
185190
const httpGetSpy = mockHttpGet(schemaContents);
186191

187192
await main.run();
@@ -192,7 +197,7 @@ describe('action', () => {
192197

193198
// Confirm cache calls use the same paths and key
194199
expect(cache.restoreCache).toHaveBeenCalledTimes(1);
195-
const [paths, key] = jest.mocked(cache.restoreCache).mock.calls[0];
200+
const [paths, key] = vi.mocked(cache.restoreCache).mock.calls[0];
196201
expect(cache.saveCache).toHaveBeenCalledTimes(1);
197202
expect(cache.saveCache).toHaveBeenLastCalledWith(paths, key);
198203
});
@@ -202,7 +207,7 @@ describe('action', () => {
202207
mockGetInput({ schema: remoteSchema });
203208
mockGetMultilineInput({ files });
204209

205-
jest.spyOn(cache, 'restoreCache').mockResolvedValue('cache-key');
210+
vi.spyOn(cache, 'restoreCache').mockResolvedValue('cache-key');
206211
const httpGetSpy = mockHttpGet(schemaContents);
207212

208213
await main.run();
@@ -217,7 +222,7 @@ describe('action', () => {
217222
mockGetInput({ schema: remoteSchema });
218223
mockGetMultilineInput({ files });
219224

220-
jest.spyOn(cache, 'restoreCache').mockResolvedValue('cache-key');
225+
vi.spyOn(cache, 'restoreCache').mockResolvedValue('cache-key');
221226
mockHttpGet(schemaContents);
222227

223228
await main.run();
@@ -250,7 +255,7 @@ describe('action', () => {
250255
mockGetInput({ schema: remoteSchema });
251256
mockGetMultilineInput({ files });
252257

253-
jest.spyOn(cache, 'restoreCache').mockImplementation(async () => {
258+
vi.spyOn(cache, 'restoreCache').mockImplementation(async () => {
254259
throw error;
255260
});
256261

@@ -272,8 +277,8 @@ describe('action', () => {
272277
mockGetInput({ schema: remoteSchema });
273278
mockGetMultilineInput({ files });
274279

275-
jest.spyOn(cache, 'restoreCache').mockResolvedValue(undefined);
276-
jest.spyOn(cache, 'saveCache').mockImplementation(async () => {
280+
vi.spyOn(cache, 'restoreCache').mockResolvedValue(undefined);
281+
vi.spyOn(cache, 'saveCache').mockImplementation(async () => {
277282
throw error;
278283
});
279284

@@ -294,9 +299,9 @@ describe('action', () => {
294299
mockGetInput({ schema });
295300
mockGetMultilineInput({ files });
296301

297-
jest
298-
.mocked(fs.readFile)
299-
.mockResolvedValueOnce(schemaContents.replace('$schema', '_schema'));
302+
vi.mocked(fs.readFile).mockResolvedValueOnce(
303+
schemaContents.replace('$schema', '_schema')
304+
);
300305

301306
await main.run();
302307
expect(runSpy).toHaveReturned();
@@ -312,7 +317,7 @@ describe('action', () => {
312317
mockGetInput({ schema });
313318
mockGetMultilineInput({ files });
314319

315-
jest.mocked(fs.readFile).mockResolvedValueOnce(schemaContents);
320+
vi.mocked(fs.readFile).mockResolvedValueOnce(schemaContents);
316321

317322
await main.run();
318323
expect(runSpy).toHaveReturned();
@@ -326,8 +331,7 @@ describe('action', () => {
326331
mockGetInput({ schema });
327332
mockGetMultilineInput({ files });
328333

329-
jest
330-
.mocked(fs.readFile)
334+
vi.mocked(fs.readFile)
331335
.mockResolvedValueOnce(schemaContents)
332336
.mockResolvedValueOnce(instanceContents);
333337
mockGlobGenerator(['/foo/bar/baz/config.yml']);
@@ -345,8 +349,7 @@ describe('action', () => {
345349
mockGetInput({ schema });
346350
mockGetMultilineInput({ files });
347351

348-
jest
349-
.mocked(fs.readFile)
352+
vi.mocked(fs.readFile)
350353
.mockResolvedValueOnce(schemaContents)
351354
.mockResolvedValueOnce('invalid content')
352355
.mockResolvedValueOnce(instanceContents);
@@ -365,8 +368,7 @@ describe('action', () => {
365368
mockGetInput({ schema });
366369
mockGetMultilineInput({ files });
367370

368-
jest
369-
.mocked(fs.readFile)
371+
vi.mocked(fs.readFile)
370372
.mockResolvedValueOnce(schemaContents)
371373
.mockResolvedValueOnce('invalid content')
372374
.mockResolvedValueOnce(instanceContents);
@@ -387,8 +389,7 @@ describe('action', () => {
387389

388390
const paths = ['/foo/bar/baz/config.yml', '/foo/bar/baz/e/config.yml'];
389391

390-
jest
391-
.mocked(fs.readFile)
392+
vi.mocked(fs.readFile)
392393
.mockResolvedValueOnce(schemaContents)
393394
.mockResolvedValueOnce('invalid content')
394395
.mockResolvedValueOnce(instanceContents);
@@ -407,8 +408,7 @@ describe('action', () => {
407408
mockGetInput({ schema });
408409
mockGetMultilineInput({ files });
409410

410-
jest
411-
.mocked(fs.readFile)
411+
vi.mocked(fs.readFile)
412412
.mockResolvedValueOnce(
413413
schemaContents.replace(
414414
'http://json-schema.org/draft-07/schema#',
@@ -436,7 +436,7 @@ describe('action', () => {
436436
});
437437

438438
it('which are valid', async () => {
439-
jest.mocked(fs.readFile).mockResolvedValueOnce(schemaContents);
439+
vi.mocked(fs.readFile).mockResolvedValueOnce(schemaContents);
440440

441441
await main.run();
442442
expect(runSpy).toHaveReturned();
@@ -449,7 +449,7 @@ describe('action', () => {
449449
it('which are invalid', async () => {
450450
mockGetBooleanInput({ 'fail-on-invalid': false });
451451

452-
jest.mocked(fs.readFile).mockResolvedValueOnce(invalidSchemaContents);
452+
vi.mocked(fs.readFile).mockResolvedValueOnce(invalidSchemaContents);
453453

454454
await main.run();
455455
expect(runSpy).toHaveReturned();
@@ -460,14 +460,12 @@ describe('action', () => {
460460
});
461461

462462
it('using JSON Schema draft-04', async () => {
463-
jest
464-
.mocked(fs.readFile)
465-
.mockResolvedValueOnce(
466-
schemaContents.replace(
467-
'http://json-schema.org/draft-07/schema#',
468-
'http://json-schema.org/draft-04/schema#'
469-
)
470-
);
463+
vi.mocked(fs.readFile).mockResolvedValueOnce(
464+
schemaContents.replace(
465+
'http://json-schema.org/draft-07/schema#',
466+
'http://json-schema.org/draft-04/schema#'
467+
)
468+
);
471469

472470
await main.run();
473471
expect(runSpy).toHaveReturned();
@@ -478,9 +476,9 @@ describe('action', () => {
478476
});
479477

480478
it('but fails if $schema key is missing', async () => {
481-
jest
482-
.mocked(fs.readFile)
483-
.mockResolvedValueOnce(schemaContents.replace('$schema', '_schema'));
479+
vi.mocked(fs.readFile).mockResolvedValueOnce(
480+
schemaContents.replace('$schema', '_schema')
481+
);
484482

485483
await main.run();
486484
expect(runSpy).toHaveReturned();

0 commit comments

Comments
 (0)