Skip to content
Snippets Groups Projects
Commit 54569e96 authored by Mehmet Seçkin's avatar Mehmet Seçkin Committed by Paul Melnikow
Browse files

[AzureDevOpsTests] Refactor unit tests to improve stability (#2454)

* Correct regex generation logic

* Refactor variable name

* Mock Azure DevOps test result summary API

* Add live tests

Updated mocked tests to use expected values for assertion. Added live
tests to test the API. Added `no tests` as an acceptable result for live
tests.

* Declare common nock setup functions to avoid repetition
parent 16c798a6
Branches
Tags
No related merge requests found
...@@ -15,7 +15,8 @@ const mockBadgeUriPath = `${uriPrefix}/${definitionId}` ...@@ -15,7 +15,8 @@ const mockBadgeUriPath = `${uriPrefix}/${definitionId}`
const mockBadgeUri = `${mockBadgeUriPath}.json` const mockBadgeUri = `${mockBadgeUriPath}.json`
const mockBranchBadgeUri = `${mockBadgeUriPath}/master.json` const mockBranchBadgeUri = `${mockBadgeUriPath}/master.json`
const mockLatestBuildApiUriPath = `/build/builds?definitions=${definitionId}&%24top=1&api-version=5.0-preview.4` const mockLatestBuildApiUriPath = `/build/builds?definitions=${definitionId}&%24top=1&api-version=5.0-preview.4`
const mockNonExistendBuildApiUriPath = `/build/builds?definitions=${nonExistentDefinitionId}&%24top=1&api-version=5.0-preview.4` const mockLatestBranchBuildApiUriPath = `/build/builds?definitions=${definitionId}&%24top=1&api-version=5.0-preview.4&branch=master`
const mockNonExistentBuildApiUriPath = `/build/builds?definitions=${nonExistentDefinitionId}&%24top=1&api-version=5.0-preview.4`
const mockTestResultSummaryApiUriPath = `/test/ResultSummaryByBuild?buildId=${buildId}` const mockTestResultSummaryApiUriPath = `/test/ResultSummaryByBuild?buildId=${buildId}`
const latestBuildResponse = { const latestBuildResponse = {
count: 1, count: 1,
...@@ -27,6 +28,39 @@ const mockEmptyTestResultSummaryResponse = { ...@@ -27,6 +28,39 @@ const mockEmptyTestResultSummaryResponse = {
resultsByOutcome: {}, resultsByOutcome: {},
}, },
} }
const mockTestResultSummaryResponse = {
aggregatedResultsAnalysis: {
totalTests: 3,
resultsByOutcome: {
Passed: {
count: 1,
},
Failed: {
count: 1,
},
Skipped: {
count: 1,
},
},
},
}
const mockTestResultSummarySetup = nock =>
nock(azureDevOpsApiBaseUri)
.get(mockLatestBuildApiUriPath)
.reply(200, latestBuildResponse)
.get(mockTestResultSummaryApiUriPath)
.reply(200, mockTestResultSummaryResponse)
const mockBranchTestResultSummarySetup = nock =>
nock(azureDevOpsApiBaseUri)
.get(mockLatestBranchBuildApiUriPath)
.reply(200, latestBuildResponse)
.get(mockTestResultSummaryApiUriPath)
.reply(200, mockTestResultSummaryResponse)
const expectedDefaultAzureDevOpsTestTotals = '1 passed, 1 failed, 1 skipped'
const expectedCompactAzureDevOpsTestTotals = '✔ 1 | ✘ 1 | ➟ 1'
const expectedCustomAzureDevOpsTestTotals = '1 good, 1 bad, 1 n/a'
const expectedCompactCustomAzureDevOpsTestTotals = '💃 1 | 🤦‍♀️ 1 | 🤷 1'
function getLabelRegex(label, isCompact) { function getLabelRegex(label, isCompact) {
return isCompact ? `(?:${label} [0-9]*)` : `(?:[0-9]* ${label})` return isCompact ? `(?:${label} [0-9]*)` : `(?:[0-9]* ${label})`
...@@ -45,11 +79,12 @@ function isAzureDevOpsTestTotals( ...@@ -45,11 +79,12 @@ function isAzureDevOpsTestTotals(
const regexStrings = [ const regexStrings = [
`^${passedRegex}$`, `^${passedRegex}$`,
`^${failedRegex}$`, `^${failedRegex}$`,
`^${skippedRegex}`, `^${skippedRegex}$`,
`^${passedRegex}${separator}${failedRegex}$`, `^${passedRegex}${separator}${failedRegex}$`,
`^${failedRegex}${separator}${skippedRegex}$`, `^${failedRegex}${separator}${skippedRegex}$`,
`^${passedRegex}${separator}${skippedRegex}$`, `^${passedRegex}${separator}${skippedRegex}$`,
`^${passedRegex}${separator}${failedRegex}${separator}${skippedLabel}$`, `^${passedRegex}${separator}${failedRegex}${separator}${skippedRegex}$`,
`^no tests$`,
] ]
return Joi.alternatives().try( return Joi.alternatives().try(
...@@ -59,8 +94,8 @@ function isAzureDevOpsTestTotals( ...@@ -59,8 +94,8 @@ function isAzureDevOpsTestTotals(
const isDefaultAzureDevOpsTestTotals = isAzureDevOpsTestTotals( const isDefaultAzureDevOpsTestTotals = isAzureDevOpsTestTotals(
'passed', 'passed',
'skipped', 'failed',
'failed' 'skipped'
) )
const isCompactAzureDevOpsTestTotals = isAzureDevOpsTestTotals( const isCompactAzureDevOpsTestTotals = isAzureDevOpsTestTotals(
'', '',
...@@ -100,7 +135,7 @@ t.create('no build response') ...@@ -100,7 +135,7 @@ t.create('no build response')
.get(`${uriPrefix}/${nonExistentDefinitionId}.json`) .get(`${uriPrefix}/${nonExistentDefinitionId}.json`)
.intercept(nock => .intercept(nock =>
nock(azureDevOpsApiBaseUri) nock(azureDevOpsApiBaseUri)
.get(mockNonExistendBuildApiUriPath) .get(mockNonExistentBuildApiUriPath)
.reply(200, { .reply(200, {
count: 0, count: 0,
value: [], value: [],
...@@ -146,14 +181,22 @@ t.create('no tests in test result summary response') ...@@ -146,14 +181,22 @@ t.create('no tests in test result summary response')
t.create('test status') t.create('test status')
.get(mockBadgeUri) .get(mockBadgeUri)
.intercept(mockTestResultSummarySetup)
.expectJSONTypes( .expectJSONTypes(
Joi.object().keys({ name: 'tests', value: isDefaultAzureDevOpsTestTotals }) Joi.object().keys({
name: 'tests',
value: expectedDefaultAzureDevOpsTestTotals,
})
) )
t.create('test status on branch') t.create('test status on branch')
.get(mockBranchBadgeUri) .get(mockBranchBadgeUri)
.intercept(mockBranchTestResultSummarySetup)
.expectJSONTypes( .expectJSONTypes(
Joi.object().keys({ name: 'tests', value: isDefaultAzureDevOpsTestTotals }) Joi.object().keys({
name: 'tests',
value: expectedDefaultAzureDevOpsTestTotals,
})
) )
t.create('test status with compact message') t.create('test status with compact message')
...@@ -162,8 +205,12 @@ t.create('test status with compact message') ...@@ -162,8 +205,12 @@ t.create('test status with compact message')
compact_message: null, compact_message: null,
}, },
}) })
.intercept(mockTestResultSummarySetup)
.expectJSONTypes( .expectJSONTypes(
Joi.object().keys({ name: 'tests', value: isCompactAzureDevOpsTestTotals }) Joi.object().keys({
name: 'tests',
value: expectedCompactAzureDevOpsTestTotals,
})
) )
t.create('test status with custom labels') t.create('test status with custom labels')
...@@ -174,11 +221,66 @@ t.create('test status with custom labels') ...@@ -174,11 +221,66 @@ t.create('test status with custom labels')
skipped_label: 'n/a', skipped_label: 'n/a',
}, },
}) })
.intercept(mockTestResultSummarySetup)
.expectJSONTypes( .expectJSONTypes(
Joi.object().keys({ name: 'tests', value: isCustomAzureDevOpsTestTotals }) Joi.object().keys({
name: 'tests',
value: expectedCustomAzureDevOpsTestTotals,
})
) )
t.create('test status with compact message and custom labels') t.create('test status with compact message and custom labels')
.get(mockBadgeUri, {
qs: {
compact_message: null,
passed_label: '💃',
failed_label: '🤦‍♀️',
skipped_label: '🤷',
},
})
.intercept(mockTestResultSummarySetup)
.expectJSONTypes(
Joi.object().keys({
name: 'tests',
value: expectedCompactCustomAzureDevOpsTestTotals,
})
)
t.create('live test status')
.get(mockBadgeUri)
.expectJSONTypes(
Joi.object().keys({ name: 'tests', value: isDefaultAzureDevOpsTestTotals })
)
t.create('live test status on branch')
.get(mockBranchBadgeUri)
.expectJSONTypes(
Joi.object().keys({ name: 'tests', value: isDefaultAzureDevOpsTestTotals })
)
t.create('live test status with compact message')
.get(mockBadgeUri, {
qs: {
compact_message: null,
},
})
.expectJSONTypes(
Joi.object().keys({ name: 'tests', value: isCompactAzureDevOpsTestTotals })
)
t.create('live test status with custom labels')
.get(mockBadgeUri, {
qs: {
passed_label: 'good',
failed_label: 'bad',
skipped_label: 'n/a',
},
})
.expectJSONTypes(
Joi.object().keys({ name: 'tests', value: isCustomAzureDevOpsTestTotals })
)
t.create('live test status with compact message and custom labels')
.get(mockBadgeUri, { .get(mockBadgeUri, {
qs: { qs: {
compact_message: null, compact_message: null,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment