Skip to content

Commit

Permalink
[test] Do not set exitCode in tested code
Browse files Browse the repository at this point in the history
  • Loading branch information
Drarig29 committed Nov 13, 2023
1 parent 6594b9a commit 70f1af5
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import * as path from 'path'

import {config, EMPTY_SUMMARY} from '../src/fixtures'
import run from '../src/main'
import * as resolveConfigModule from '../src/resolve-config'
import * as fs from 'fs'

const inputs = {
Expand All @@ -19,14 +18,18 @@ const inputs = {
describe('Run Github Action', () => {
beforeEach(() => {
jest.restoreAllMocks()

process.env = {
...process.env,
INPUT_API_KEY: inputs.apiKey,
INPUT_APP_KEY: inputs.appKey,
INPUT_PUBLIC_IDS: inputs.publicIds.join(', '),
}
process.stdout.write = jest.fn()

jest.spyOn(process.stdout, 'write').mockImplementation()
jest.spyOn(core, 'setFailed').mockImplementation()
})

describe('Handle input parameters', () => {
test('Github Action called with dummy parameter fails with core.setFailed', async () => {
process.env = {
Expand All @@ -35,6 +38,7 @@ describe('Run Github Action', () => {
}

const setFailedMock = jest.spyOn(core, 'setFailed')

await run()

delete process.env.INPUT_FAIL_ON_CRITICAL_ERRORS
Expand All @@ -43,7 +47,7 @@ describe('Run Github Action', () => {
})

test('Github Action core.getInput parameters are passed on to runTests', async () => {
jest.spyOn(synthetics, 'executeTests').mockImplementation(() => ({} as any))
jest.spyOn(synthetics, 'executeTests').mockImplementation()

await run()
expect(synthetics.executeTests).toHaveBeenCalledWith(expect.anything(), {
Expand All @@ -62,7 +66,7 @@ describe('Run Github Action', () => {
...process.env,
INPUT_PUBLIC_IDS: publicIds.join(', '),
}
jest.spyOn(synthetics, 'executeTests').mockImplementation(() => ({} as any))
jest.spyOn(synthetics, 'executeTests').mockImplementation()

await run()
expect(synthetics.executeTests).toHaveBeenCalledWith(expect.anything(), {
Expand All @@ -82,7 +86,7 @@ describe('Run Github Action', () => {
INPUT_VARIABLES: 'START_URL=https://example.org,MY_VARIABLE=My title',
}

jest.spyOn(synthetics, 'executeTests').mockImplementation(() => ({} as any))
jest.spyOn(synthetics, 'executeTests').mockImplementation()

await run()
expect(synthetics.executeTests).toHaveBeenCalledWith(expect.anything(), {
Expand Down Expand Up @@ -134,7 +138,7 @@ describe('Run Github Action', () => {

describe('Handle invalid input parameters', () => {
test('Use default configuration if Github Action input is not set', async () => {
jest.spyOn(synthetics, 'executeTests').mockImplementation(() => ({} as any))
jest.spyOn(synthetics, 'executeTests').mockImplementation()
await run()
expect(synthetics.executeTests).toHaveBeenCalledWith(expect.anything(), {
...config,
Expand All @@ -150,21 +154,21 @@ describe('Run Github Action', () => {

describe('Handle configuration file', () => {
test('Github Action fails if unable to parse config file', async () => {
const configPath = 'foobar'
const setFailedMock = jest.spyOn(core, 'setFailed')
process.env = {
...process.env,
INPUT_CONFIG_PATH: configPath,
INPUT_CONFIG_PATH: 'foo',
}
await expect(run()).rejects.toThrowError('Config file not found')
expect(process.exitCode).toBe(1)
await expect(run()).rejects.toThrow('Config file not found')
expect(setFailedMock).toHaveBeenCalled()
process.env = {}
})
})

describe('Handle Synthetics test results', () => {
beforeEach(() => {
// renderResults() does side effects on the summary: mocking it for easier testing.
jest.spyOn(synthetics.utils, 'renderResults').mockImplementation(() => ({} as any))
jest.spyOn(synthetics.utils, 'renderResults').mockImplementation()
})

test('Github Action fails if Synthetics tests fail', async () => {
Expand Down

0 comments on commit 70f1af5

Please sign in to comment.