Skip to content

Commit

Permalink
fix: await handlePullRequest in rerunCheck and correct asyncs in tests
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
vorobievalex committed May 5, 2021
1 parent 7bd7f6b commit 8a04259
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 deletions.
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
setupFiles: ['<rootDir>/test/setup.js'],
};
13 changes: 12 additions & 1 deletion lib/RallyValidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -819,7 +830,7 @@ class RallyValidate {
})
prContext.payload.pull_request = prResponse.data

this.handlePullRequest(prContext)
await this.handlePullRequestWithRally(prContext, _initializeRallyClient)
}

/**
Expand Down
49 changes: 38 additions & 11 deletions test/RallyValidate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,19 +16,21 @@ describe('JiraIssueValidate', () => {
robot = {
log: {
debug: jest.fn(),
info: jest.fn(),
error: jest.fn()
}
}

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({
Expand All @@ -48,6 +51,11 @@ describe('JiraIssueValidate', () => {
}
}))
},
pulls: {
get: jest.fn().mockImplementation(() => Promise.resolve({
data: validPR
})),
},
issues: {
createComment: jest.fn().mockImplementation(() => Promise.resolve({}))
}
Expand All @@ -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
})
Expand All @@ -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'
}))
Expand All @@ -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 changes: 17 additions & 0 deletions test/fixtures/check_run_rerequested.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"check_run": {
"name": "integrations/rally",
"check_suite": {
"pull_requests": [
{
"number": 100
}
]
}
},
"repository": {
"owner": {
"login": "acme"
}
}
}
3 changes: 3 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
process.on('unhandledRejection', (err) => {
fail(err);
});

0 comments on commit 8a04259

Please sign in to comment.