Skip to content

Commit

Permalink
Expand test coverage for required status scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
jnewland committed May 24, 2024
1 parent 486fc05 commit 61f32a0
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 2 deletions.
134 changes: 134 additions & 0 deletions __tests__/required.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { required } from '../src/required'
import { GitHub } from '@actions/github/lib/utils'
import { Context } from '@actions/github/lib/context'
import { WorkflowRunCompletedEvent } from '@octokit/webhooks-types'

Check failure on line 4 in __tests__/required.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Unable to resolve path to module '@octokit/webhooks-types'

describe('required', () => {
let mockOctokit: any

Check failure on line 7 in __tests__/required.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Unexpected any. Specify a different type
let context: Context
let event: WorkflowRunCompletedEvent

beforeEach(() => {
mockOctokit = {
rest: {
repos: {
createCommitStatus: jest.fn()
},
actions: {
getWorkflowRun: jest.fn(),
listWorkflowRunsForRepo: jest.fn()
}
}
}
context = new Context()
event = {
workflow_run: {
id: 123,
name: 'Test Workflow',
head_sha: 'abc123',
conclusion: 'success',
html_url: 'http://example.com'
}
} as unknown as WorkflowRunCompletedEvent
})

it('should handle all required statuses completing successfully', async () => {
// Mock setup to simulate all required statuses completing successfully
mockOctokit.rest.actions.listWorkflowRunsForRepo.mockResolvedValue({
data: {
workflow_runs: [
{ name: 'Test Workflow', conclusion: 'success' },
{ name: 'Another Workflow', conclusion: 'success' }
]
}
})

await required({
octokit: mockOctokit as unknown as InstanceType<typeof GitHub>,
context,
event,
workflows: ['Test Workflow', 'Another Workflow'],
statusName: 'Required'
})

expect(mockOctokit.rest.repos.createCommitStatus).toHaveBeenCalledWith(expect.objectContaining({

Check failure on line 54 in __tests__/required.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Insert `⏎······`
state: 'success',

Check failure on line 55 in __tests__/required.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Insert `··`
description: 'All required workflows have succeeded.'

Check failure on line 56 in __tests__/required.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Insert `··`
}))

Check failure on line 57 in __tests__/required.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Replace `})` with `··})⏎····`
})

it('should handle only one of the required statuses reporting success', async () => {
// Mock setup to simulate only one of the required statuses reporting success
mockOctokit.rest.actions.listWorkflowRunsForRepo.mockResolvedValue({
data: {
workflow_runs: [
{ name: 'Test Workflow', conclusion: 'success' },
{ name: 'Another Workflow', conclusion: null }
]
}
})

await required({
octokit: mockOctokit as unknown as InstanceType<typeof GitHub>,
context,
event,
workflows: ['Test Workflow', 'Another Workflow'],
statusName: 'Required'
})

expect(mockOctokit.rest.repos.createCommitStatus).toHaveBeenCalledWith(expect.objectContaining({

Check failure on line 79 in __tests__/required.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Insert `⏎······`
state: 'pending',

Check failure on line 80 in __tests__/required.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Replace `······` with `········`
description: expect.stringContaining('required workflows are still pending...')

Check failure on line 81 in __tests__/required.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Replace `······description:·expect.stringContaining('required·workflows·are·still·pending...'` with `········description:·expect.stringContaining(⏎··········'required·workflows·are·still·pending...'⏎········`
}))

Check failure on line 82 in __tests__/required.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Replace `····})` with `······})⏎····`
})

it('should handle one of the required statuses reporting failure', async () => {
// Mock setup to simulate one of the required statuses reporting failure
mockOctokit.rest.actions.listWorkflowRunsForRepo.mockResolvedValue({
data: {
workflow_runs: [
{ name: 'Test Workflow', conclusion: 'success' },
{ name: 'Another Workflow', conclusion: 'failure' }
]
}
})

await required({
octokit: mockOctokit as unknown as InstanceType<typeof GitHub>,
context,
event,
workflows: ['Test Workflow', 'Another Workflow'],
statusName: 'Required'
})

expect(mockOctokit.rest.repos.createCommitStatus).toHaveBeenCalledWith(expect.objectContaining({
state: 'failure',
description: expect.stringContaining('required workflows were not successful.')
}))
})

it('should handle replication lag scenario', async () => {
// Mock setup to simulate replication lag scenario
mockOctokit.rest.actions.listWorkflowRunsForRepo.mockResolvedValue({
data: {
workflow_runs: [
{ name: 'Test Workflow', conclusion: null },
{ name: 'Another Workflow', conclusion: 'success' }
]
}
})

await required({
octokit: mockOctokit as unknown as InstanceType<typeof GitHub>,
context,
event,
workflows: ['Test Workflow', 'Another Workflow'],
statusName: 'Required'
})

expect(mockOctokit.rest.repos.createCommitStatus).toHaveBeenCalledWith(expect.objectContaining({
state: 'pending',
description: expect.stringContaining('Waiting for conclusion to be reported for Test Workflow...')
}))
})
})
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
},
"scripts": {
"bundle": "npm run format:write && npm run package",
"ci-test": "true",
"ci-test": "jest",
"coverage": "npx make-coverage-badge --output-path ./badges/coverage.svg",
"format:write": "npx prettier --write .",
"format:check": "npx prettier --check .",
"lint": "npx eslint . -c ./.github/linters/.eslintrc.yml",
"package": "npx ncc build src/index.ts -o dist --source-map --license licenses.txt",
"package:watch": "npm run package -- --watch",
"test": "true",
"test": "jest --coverage",
"all": "npm run format:write && npm run lint && npm run test && npm run coverage && npm run package"
},
"license": "MIT",
Expand Down

0 comments on commit 61f32a0

Please sign in to comment.