Skip to content

Commit 9a9d261

Browse files
committed
test: cleanup and centralize test utils
Signed-off-by: Sam Gammon <[email protected]>
1 parent ebdb7ac commit 9a9d261

File tree

6 files changed

+100
-34
lines changed

6 files changed

+100
-34
lines changed

__tests__/command.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import * as command from '../src/command'
2-
import { withTestBinary } from './utils'
2+
import { setupCoreMocks, withTestBinary } from './utils'
3+
4+
const { clearMocks, resetState } = setupCoreMocks()
35

46
describe('action transport tools', () => {
57
beforeEach(() => {
68
jest.clearAllMocks()
79
})
10+
afterEach(() => {
11+
clearMocks()
12+
})
13+
afterAll(async () => {
14+
await resetState()
15+
})
816

917
it('can obtain the current buildless cli version', async () => {
1018
withTestBinary(async () => {

__tests__/index.test.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
/**
2-
* Unit tests for the action's entrypoint, src/index.ts
3-
*/
4-
51
import * as main from '../src/main'
2+
import { setupCoreMocks } from './utils'
63

7-
const runMock = jest.spyOn(main, 'entry').mockImplementation()
8-
const cleanupMock = jest.spyOn(main, 'cleanup').mockImplementation()
4+
const { clearMocks, resetState } = setupCoreMocks()
5+
const runMock = jest.spyOn(main, 'entry').mockImplementation(() => Promise.resolve())
6+
const cleanupMock = jest.spyOn(main, 'cleanup').mockImplementation(() => Promise.resolve())
97

108
describe('index', () => {
9+
afterEach(() => {
10+
clearMocks()
11+
})
12+
afterAll(async () => {
13+
await resetState()
14+
})
15+
1116
it('calls entrypoint when imported', async () => {
1217
// eslint-disable-next-line @typescript-eslint/no-require-imports
1318
require('../src/index')
14-
1519
expect(runMock).toHaveBeenCalled()
1620
})
1721
})
@@ -20,7 +24,6 @@ describe('cleanup', () => {
2024
it('calls entrypoint when imported', async () => {
2125
// eslint-disable-next-line @typescript-eslint/no-require-imports
2226
require('../src/cleanup')
23-
2427
expect(cleanupMock).toHaveBeenCalled()
2528
})
2629
})

__tests__/main.test.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
import * as core from '@actions/core'
1+
import { setupCoreMocks } from './utils'
22
import * as main from '../src/main'
33
import { ActionOutputName } from '../src/outputs'
44

5+
const { setupMocks, clearMocks, resetState, errorMock, getInputMock, setFailedMock, setOutputMock } = setupCoreMocks()
56
const runMock = jest.spyOn(main, 'entry')
67

7-
let debugMock: jest.SpyInstance
8-
let errorMock: jest.SpyInstance
9-
let getInputMock: jest.SpyInstance
10-
let setFailedMock: jest.SpyInstance
11-
let setOutputMock: jest.SpyInstance
12-
138
describe('action entry', () => {
149
beforeEach(() => {
1510
jest.clearAllMocks()
16-
17-
debugMock = jest.spyOn(core, 'debug').mockImplementation()
18-
errorMock = jest.spyOn(core, 'error').mockImplementation()
19-
getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
20-
setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
21-
setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation()
11+
setupMocks()
12+
})
13+
afterEach(() => {
14+
clearMocks()
15+
})
16+
afterAll(async () => {
17+
await resetState()
2218
})
2319

2420
it('sets the path and version outputs', async () => {
@@ -41,7 +37,7 @@ describe('action entry', () => {
4137
ActionOutputName.VERSION,
4238
expect.anything()
4339
)
44-
}, 30000)
40+
})
4541

4642
it('sets a failed status', async () => {
4743
// Set the action's inputs as return values from core.getInput()

__tests__/releases.test.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
import * as core from '@actions/core'
2-
import * as github from '@actions/github'
31
import * as releases from '../src/releases'
2+
import { setupCoreMocks } from './utils'
43

4+
const { resetState, setupMocks } = setupCoreMocks()
55
const resolveLatestMock = jest.spyOn(releases, 'resolveLatestVersion')
66

7-
let debugMock: jest.SpyInstance
8-
let errorMock: jest.SpyInstance
9-
let setFailedMock: jest.SpyInstance
10-
let getOctokitMock: jest.SpyInstance
11-
127
describe('release utilities', () => {
138
beforeEach(() => {
149
jest.clearAllMocks()
15-
debugMock = jest.spyOn(core, 'debug').mockImplementation()
16-
errorMock = jest.spyOn(core, 'error').mockImplementation()
17-
setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
18-
getOctokitMock = jest.spyOn(github, 'getOctokit').mockImplementation()
10+
setupMocks()
11+
})
12+
afterAll(async () => {
13+
await resetState()
1914
})
15+
2016
it('can resolve the latest buildless release', async () => {
2117
const result = await releases.resolveLatestVersion()
2218
expect(result).not.toBeNull()

__tests__/transport.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import * as transport from '../src/transport'
22
import { RpcTransport } from '../src/config'
3+
import { setupCoreMocks } from './utils'
4+
5+
const { resetState } = setupCoreMocks()
36

47
describe('action transport tools', () => {
58
beforeEach(() => {
69
jest.clearAllMocks()
710
})
11+
afterAll(async () => {
12+
await resetState()
13+
})
814

915
it('can provide a configured rpc transport', async () => {
1016
const engine = transport.obtainTransport()

__tests__/utils.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import path from 'node:path'
22
import fs from 'node:fs'
3+
import * as core from '@actions/core'
4+
import * as exec from '@actions/exec'
5+
import * as github from '@actions/github'
36
import { opendir, access } from 'node:fs/promises'
47
import { setBinpath } from '../src/command'
8+
import { onExit } from '../src/diagnostics'
9+
import { httpClient } from '../src/config'
510
import { install as installBuildless } from '../src/main'
611

712
/**
@@ -64,3 +69,55 @@ export async function withTestBinary<R>(
6469
setBinpath(binpath)
6570
return await fn(binpath)
6671
}
72+
73+
export function resetState() {
74+
onExit(false)
75+
}
76+
77+
export function setupCoreMocks(): {
78+
debugMock: jest.SpyInstance,
79+
errorMock: jest.SpyInstance,
80+
getInputMock: jest.SpyInstance,
81+
setFailedMock: jest.SpyInstance,
82+
setOutputMock: jest.SpyInstance,
83+
getJsonMock: jest.SpyInstance,
84+
getOctokitMock: jest.SpyInstance,
85+
execMock: jest.SpyInstance,
86+
clearMocks: () => void,
87+
setupMocks: () => void,
88+
resetState: () => void,
89+
} {
90+
let debugMock: jest.SpyInstance = jest.spyOn(core, 'debug')
91+
let errorMock: jest.SpyInstance = jest.spyOn(core, 'error')
92+
let getInputMock: jest.SpyInstance = jest.spyOn(core, 'getInput')
93+
let setFailedMock: jest.SpyInstance = jest.spyOn(core, 'setFailed')
94+
let setOutputMock: jest.SpyInstance = jest.spyOn(core, 'setOutput')
95+
let execMock: jest.SpyInstance = jest.spyOn(exec, 'exec')
96+
let getOctokitMock: jest.SpyInstance = jest.spyOn(github, 'getOctokit').mockImplementation()
97+
let getJsonMock: jest.SpyInstance = jest.spyOn(httpClient, 'getJson').mockImplementation()
98+
99+
return {
100+
debugMock,
101+
errorMock,
102+
getInputMock,
103+
setFailedMock,
104+
setOutputMock,
105+
getJsonMock,
106+
getOctokitMock,
107+
execMock,
108+
resetState: () => {
109+
resetState()
110+
},
111+
clearMocks: () => {
112+
// Nothing at this time.
113+
},
114+
setupMocks: () => {
115+
execMock = jest.spyOn(exec, 'exec').mockImplementation()
116+
debugMock = jest.spyOn(core, 'debug').mockImplementation()
117+
errorMock = jest.spyOn(core, 'error').mockImplementation()
118+
getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
119+
setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
120+
setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation()
121+
},
122+
}
123+
}

0 commit comments

Comments
 (0)