Skip to content

Commit e07e7af

Browse files
authored
mock core.<method> loggers for actions
1 parent 394221a commit e07e7af

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

__tests__/helpers.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as fs from 'fs/promises'
2+
import * as core from '@actions/core'
23
import path from 'path'
34

45
export const testFixturesDirectory = path.join(__dirname, 'fixtures')
@@ -19,7 +20,7 @@ module github.com/robherley/go-test-example
1920
go 1.18
2021
`
2122

22-
export const setupActionsInputs = async () => {
23+
export const setupActionsInputs = () => {
2324
process.env['INPUT_MODULEDIRECTORY'] = testModuleDirectory
2425
process.env['INPUT_TESTARGUMENTS'] = testArguments
2526
}
@@ -46,3 +47,20 @@ export const getTestStdout = async (): Promise<string> => {
4647
)
4748
return buf.toString()
4849
}
50+
51+
export const mockActionsCoreLogging = () => {
52+
type LogFuncs = 'debug' | 'error' | 'warning' | 'notice' | 'info'
53+
const logMethods: LogFuncs[] = ['debug', 'error', 'warning', 'notice', 'info']
54+
logMethods.forEach(method => {
55+
jest
56+
.spyOn(core, method)
57+
.mockImplementation(
58+
(msg: string | Error, props?: core.AnnotationProperties) => {
59+
console.log(
60+
`[mock: core.${method}(${props ? JSON.stringify(props) : ''})]:`,
61+
msg
62+
)
63+
}
64+
)
65+
})
66+
}

__tests__/renderer.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import { getTestStdout } from './helpers'
1+
import { getTestStdout, mockActionsCoreLogging } from './helpers'
22
import Renderer from '../src/renderer'
33

44
describe('Renderer', () => {
5+
beforeEach(async () => {
6+
mockActionsCoreLogging()
7+
})
8+
59
it('correctly parses test2json output', async () => {
610
const stdout = await getTestStdout()
711

__tests__/tester.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
testModuleDirectory,
99
mockProcessExit,
1010
createFakeGoModule,
11+
mockActionsCoreLogging,
1112
} from './helpers'
1213
import Tester from '../src/tester'
1314
import Renderer from '../src/renderer'
@@ -18,7 +19,8 @@ describe('Tester', () => {
1819
})
1920

2021
beforeEach(async () => {
21-
await setupActionsInputs()
22+
mockActionsCoreLogging()
23+
setupActionsInputs()
2224
})
2325

2426
it("sets defaults if inputs aren't set", async () => {

jest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const config: Config.InitialOptions = {
55
testEnvironment: 'node',
66
testPathIgnorePatterns: ['helpers.ts'],
77
reporters: ['default', 'github-actions'],
8+
clearMocks: true,
89
}
910

1011
export default config

0 commit comments

Comments
 (0)