fix: await handlePullRequest in rerunCheck and correct asyncs in tests

- test 'check_run rerequested' added to test it
- test methods that should be async made async
- commentOnPull made true to also test the appropriate code path
- handlePullRequest in tests replaced with handlePullRequestWithRally to escape creating real rally instance, that runs its own asyncs, that are left after the tesrts are over
- setup.js with fail on unhandledRejection added to catch async warnings like the one appeared with the absent Connections._ref in rallyClient
This commit is contained in:
Alexander Vorobyev 2021-05-05 20:14:15 +03:00
Родитель 8f1cb6f448
Коммит a2eeb8ea3d
5 изменённых файлов: 73 добавлений и 12 удалений

3
jest.config.js Normal file
Просмотреть файл

@ -0,0 +1,3 @@
module.exports = {
setupFiles: ['<rootDir>/test/setup.js'],
};

Просмотреть файл

@ -789,6 +789,17 @@ class RallyValidate {
* @returns {Promise<void>}
*/
async rerunCheck (context) {
await this.rerunCheckWithRally (context, this.initializeRallyClient)
}
/**
* Process the pull request with the given initializeRallyClient
*
* @param context
* @param _initializeRallyClient
* @returns {Promise<void>}
*/
async rerunCheckWithRally(context, _initializeRallyClient) {
const prContext = context
const defaultConfig = await this.getDefaultConfig(context)
@ -819,7 +830,7 @@ class RallyValidate {
})
prContext.payload.pull_request = prResponse.data
this.handlePullRequest(prContext)
await this.handlePullRequestWithRally(prContext, _initializeRallyClient)
}
/**

Просмотреть файл

@ -5,8 +5,9 @@ const fs = require('fs')
const validPR = require('./fixtures/valid_pull_request')
const validRepo = require('./fixtures/valid_repository')
const checkRunRerequested = require('./fixtures/check_run_rerequested')
describe('JiraIssueValidate', () => {
describe('RallyIssueValidate', () => {
let robot
let handler
let context
@ -15,6 +16,7 @@ describe('JiraIssueValidate', () => {
robot = {
log: {
debug: jest.fn(),
info: jest.fn(),
error: jest.fn()
}
}
@ -22,12 +24,13 @@ describe('JiraIssueValidate', () => {
handler = new RallyValidate(robot)
const configFile = yaml.load(fs.readFileSync('./rally.yml'))
const probotConfigEncodedYaml = Buffer.from(yaml.dump(configFile)).toString('base64')
const configAllPaths = {...configFile, commentOnPull: true}
const probotConfigEncodedYaml = Buffer.from(yaml.dump(configAllPaths)).toString('base64')
context = {
config: jest.fn().mockImplementation(() => Promise.resolve(configFile)),
config: jest.fn().mockImplementation(() => Promise.resolve(configAllPaths)),
github: {
checks: {
create: jest.fn()
create: jest.fn().mockImplementation(() => Promise.resolve(configAllPaths))
},
repos: {
compareCommits: jest.fn().mockImplementation(() => Promise.resolve({
@ -48,6 +51,11 @@ describe('JiraIssueValidate', () => {
}
}))
},
pulls: {
get: jest.fn().mockImplementation(() => Promise.resolve({
data: validPR
})),
},
issues: {
createComment: jest.fn().mockImplementation(() => Promise.resolve({}))
}
@ -68,11 +76,12 @@ describe('JiraIssueValidate', () => {
Project: {
_refObjectName: 'Sample Project'
},
ScheduleState: 'Defined'
ScheduleState: 'Defined',
Connections: { _ref: "connections-ref" }
}
]
})),
update: jest.fn()
update: jest.fn().mockImplementation(() => Promise.resolve({}))
}
initializeRallyClient = jest.fn().mockImplementation(() => Promise.resolve(rallyClient)) // eslint-disable-line
})
@ -95,21 +104,21 @@ describe('JiraIssueValidate', () => {
describe('get configuration', () => {
it('requests config file from repository', async () => {
await handler.handlePullRequest(context)
await handler.handlePullRequestWithRally(context, initializeRallyClient)
expect(context.config).toHaveBeenCalled()
})
it('doesn\'t run when config is empty and ENFORCE_ALL_REPOS is false', async () => {
context.config = jest.fn().mockImplementation(() => Promise.resolve(undefined))
process.env.ENFORCE_ALL_REPOS = false
await handler.handlePullRequest(context)
process.env.ENFORCE_ALL_REPOS = 'false'
await handler.handlePullRequestWithRally(context, initializeRallyClient)
expect(context.github.checks.create).not.toHaveBeenCalled()
})
it('returns fail status when config is empty and ENFORCE_ALL_REPOS is true', async () => {
context.config = jest.fn().mockImplementation(() => Promise.resolve(undefined))
process.env.ENFORCE_ALL_REPOS = true
await handler.handlePullRequest(context)
process.env.ENFORCE_ALL_REPOS = 'true'
await handler.handlePullRequestWithRally(context, initializeRallyClient)
expect(context.github.checks.create).toHaveBeenCalledWith(expect.objectContaining({
conclusion: 'failure'
}))
@ -130,4 +139,22 @@ describe('JiraIssueValidate', () => {
expect(rallyClient.update).toHaveBeenCalled()
})
})
describe('rerequested', () => {
it('check_run rerequested', async () => {
context.payload = checkRunRerequested
context.name = 'check_run'
await handler.rerunCheckWithRally(context, initializeRallyClient)
expect(context.config).toHaveBeenCalled()
expect(context.github.checks.create.mock.calls).toEqual([
[context.repo(expect.objectContaining({
"status": "in_progress",
}))],
[context.repo(expect.objectContaining({
"conclusion": "success",
"status": "completed",
}))]
])
})
})
})

17
test/fixtures/check_run_rerequested.json поставляемый Normal file
Просмотреть файл

@ -0,0 +1,17 @@
{
"check_run": {
"name": "integrations/rally",
"check_suite": {
"pull_requests": [
{
"number": 100
}
]
}
},
"repository": {
"owner": {
"login": "acme"
}
}
}

3
test/setup.js Normal file
Просмотреть файл

@ -0,0 +1,3 @@
process.on('unhandledRejection', (err) => {
fail(err);
});