Skip to content

Commit 8f69220

Browse files
test: refactor test mocks
1 parent 524caad commit 8f69220

2 files changed

Lines changed: 22 additions & 50 deletions

File tree

src/index.test.ts

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,23 @@
1-
import type { arch, platform } from 'node:os';
2-
3-
import type { addPath, getInput, setFailed } from '@actions/core';
4-
import type { exec } from '@actions/exec';
5-
import type {
6-
cacheFile,
7-
downloadTool,
8-
extractTar,
9-
extractZip,
10-
find,
11-
} from '@actions/tool-cache';
12-
import type { MockedFunction } from 'vitest';
1+
import os from 'node:os';
2+
3+
import * as core from '@actions/core';
4+
import * as exec from '@actions/exec';
5+
import * as tc from '@actions/tool-cache';
136
import { beforeEach, describe, expect, it, vi } from 'vitest';
147

15-
const mockedCore = {
16-
getInput: vi.fn() as MockedFunction<typeof getInput>,
17-
addPath: vi.fn() as MockedFunction<typeof addPath>,
18-
setFailed: vi.fn() as MockedFunction<typeof setFailed>,
19-
};
20-
21-
const mockedExec = {
22-
exec: vi.fn() as MockedFunction<typeof exec>,
23-
};
24-
25-
const mockedTc = {
26-
downloadTool: vi.fn() as MockedFunction<typeof downloadTool>,
27-
extractTar: vi.fn() as MockedFunction<typeof extractTar>,
28-
extractZip: vi.fn() as MockedFunction<typeof extractZip>,
29-
cacheFile: vi.fn() as MockedFunction<typeof cacheFile>,
30-
find: vi.fn() as MockedFunction<typeof find>,
31-
};
32-
33-
const mockedOs = {
34-
platform: vi.fn() as MockedFunction<typeof platform>,
35-
arch: vi.fn() as MockedFunction<typeof arch>,
36-
};
37-
38-
vi.mock('@actions/core', () => mockedCore);
39-
vi.mock('@actions/exec', () => mockedExec);
40-
vi.mock('@actions/tool-cache', () => mockedTc);
41-
vi.mock('node:os', () => mockedOs);
42-
43-
const { run } = await import('./index.js');
8+
vi.mock('@actions/core');
9+
const mockedCore = vi.mocked(core);
10+
11+
vi.mock('@actions/exec');
12+
const mockedExec = vi.mocked(exec);
13+
14+
vi.mock('@actions/tool-cache');
15+
const mockedTc = vi.mocked(tc);
16+
17+
vi.mock('node:os');
18+
const mockedOs = vi.mocked(os);
19+
20+
import { run } from './index.js';
4421

4522
beforeEach(() => {
4623
vi.resetAllMocks();

src/utils.test.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
import type { arch, platform } from 'node:os';
1+
import os from 'node:os';
22

3-
import type { MockedFunction } from 'vitest';
43
import { beforeEach, describe, expect, it, vi } from 'vitest';
54

6-
const mockedOs = {
7-
platform: vi.fn() as MockedFunction<typeof platform>,
8-
arch: vi.fn() as MockedFunction<typeof arch>,
9-
};
5+
vi.mock('node:os');
6+
const mockedOs = vi.mocked(os);
107

11-
vi.mock('node:os', () => mockedOs);
12-
13-
const { getBinaryPath, getDownloadObject } = await import('./utils.js');
8+
import { getBinaryPath, getDownloadObject } from './utils.js';
149

1510
const platforms: NodeJS.Platform[] = ['darwin', 'linux', 'win32'];
1611
const architectures = ['arm', 'x32', 'x64'] as NodeJS.Architecture[];

0 commit comments

Comments
 (0)