diff --git a/services/azure-devops/azure-devops-tests.tester.js b/services/azure-devops/azure-devops-tests.tester.js index 4c3ddc49065e7b38c5ccf5e1955678ffdeedae85..48cee13a09bae3f38ea6a01fb7dbf8687f039b7a 100644 --- a/services/azure-devops/azure-devops-tests.tester.js +++ b/services/azure-devops/azure-devops-tests.tester.js @@ -15,7 +15,8 @@ const mockBadgeUriPath = `${uriPrefix}/${definitionId}` const mockBadgeUri = `${mockBadgeUriPath}.json` const mockBranchBadgeUri = `${mockBadgeUriPath}/master.json` 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 latestBuildResponse = { count: 1, @@ -27,6 +28,39 @@ const mockEmptyTestResultSummaryResponse = { 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) { return isCompact ? `(?:${label} [0-9]*)` : `(?:[0-9]* ${label})` @@ -45,11 +79,12 @@ function isAzureDevOpsTestTotals( const regexStrings = [ `^${passedRegex}$`, `^${failedRegex}$`, - `^${skippedRegex}`, + `^${skippedRegex}$`, `^${passedRegex}${separator}${failedRegex}$`, `^${failedRegex}${separator}${skippedRegex}$`, `^${passedRegex}${separator}${skippedRegex}$`, - `^${passedRegex}${separator}${failedRegex}${separator}${skippedLabel}$`, + `^${passedRegex}${separator}${failedRegex}${separator}${skippedRegex}$`, + `^no tests$`, ] return Joi.alternatives().try( @@ -59,8 +94,8 @@ function isAzureDevOpsTestTotals( const isDefaultAzureDevOpsTestTotals = isAzureDevOpsTestTotals( 'passed', - 'skipped', - 'failed' + 'failed', + 'skipped' ) const isCompactAzureDevOpsTestTotals = isAzureDevOpsTestTotals( '✔', @@ -100,7 +135,7 @@ t.create('no build response') .get(`${uriPrefix}/${nonExistentDefinitionId}.json`) .intercept(nock => nock(azureDevOpsApiBaseUri) - .get(mockNonExistendBuildApiUriPath) + .get(mockNonExistentBuildApiUriPath) .reply(200, { count: 0, value: [], @@ -146,14 +181,22 @@ t.create('no tests in test result summary response') t.create('test status') .get(mockBadgeUri) + .intercept(mockTestResultSummarySetup) .expectJSONTypes( - Joi.object().keys({ name: 'tests', value: isDefaultAzureDevOpsTestTotals }) + Joi.object().keys({ + name: 'tests', + value: expectedDefaultAzureDevOpsTestTotals, + }) ) t.create('test status on branch') .get(mockBranchBadgeUri) + .intercept(mockBranchTestResultSummarySetup) .expectJSONTypes( - Joi.object().keys({ name: 'tests', value: isDefaultAzureDevOpsTestTotals }) + Joi.object().keys({ + name: 'tests', + value: expectedDefaultAzureDevOpsTestTotals, + }) ) t.create('test status with compact message') @@ -162,8 +205,12 @@ t.create('test status with compact message') compact_message: null, }, }) + .intercept(mockTestResultSummarySetup) .expectJSONTypes( - Joi.object().keys({ name: 'tests', value: isCompactAzureDevOpsTestTotals }) + Joi.object().keys({ + name: 'tests', + value: expectedCompactAzureDevOpsTestTotals, + }) ) t.create('test status with custom labels') @@ -174,11 +221,66 @@ t.create('test status with custom labels') skipped_label: 'n/a', }, }) + .intercept(mockTestResultSummarySetup) .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') + .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, { qs: { compact_message: null,