From 59a6af0516158725679857c04e04949bdb595c50 Mon Sep 17 00:00:00 2001 From: Mike Penz Date: Fri, 27 Aug 2021 11:59:12 +0200 Subject: [PATCH] - restructure transform.test.ts - format test files --- __tests__/main.test.ts | 4 +- __tests__/releaseNotes.test.ts | 16 +- __tests__/releaseNotesBuilder.test.ts | 6 +- __tests__/transform.test.ts | 401 +++++++++++++++----------- 4 files changed, 240 insertions(+), 187 deletions(-) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 92e5162..9f3ce7f 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -54,9 +54,9 @@ test('should write result to file', () => { // should succeed expect(result).toBeDefined() - const readOutput = fs.readFileSync("test.md") + const readOutput = fs.readFileSync('test.md') - fs.unlinkSync("test.md") + fs.unlinkSync('test.md') expect(readOutput.toString()).not.toBe('') }) diff --git a/__tests__/releaseNotes.test.ts b/__tests__/releaseNotes.test.ts index ab515b4..ee464a3 100644 --- a/__tests__/releaseNotes.test.ts +++ b/__tests__/releaseNotes.test.ts @@ -18,7 +18,7 @@ it('Should have empty changelog (tags)', async () => { toTag: 'v0.0.2', failOnError: false, commitMode: false, - configuration: configuration + configuration }) const changeLog = await releaseNotes.pull() @@ -35,7 +35,7 @@ it('Should match generated changelog (tags)', async () => { toTag: 'v0.0.3', failOnError: false, commitMode: false, - configuration: configuration + configuration }) const changeLog = await releaseNotes.pull() @@ -60,7 +60,7 @@ it('Should match generated changelog (refs)', async () => { toTag: 'fa3788c8c4b3373ef8424ce3eb008a5cd07cc5aa', failOnError: false, commitMode: false, - configuration: configuration + configuration }) const changeLog = await releaseNotes.pull() @@ -93,7 +93,7 @@ it('Should match generated changelog and replace all occurrences (refs)', async toTag: 'fa3788c8c4b3373ef8424ce3eb008a5cd07cc5aa', failOnError: false, commitMode: false, - configuration: configuration + configuration }) const changeLog = await releaseNotes.pull() @@ -128,7 +128,7 @@ it('Should match ordered ASC', async () => { toTag: 'v0.5.0', failOnError: false, commitMode: false, - configuration: configuration + configuration }) const changeLog = await releaseNotes.pull() @@ -150,7 +150,7 @@ it('Should match ordered DESC', async () => { toTag: 'v0.5.0', failOnError: false, commitMode: false, - configuration: configuration + configuration }) const changeLog = await releaseNotes.pull() @@ -172,7 +172,7 @@ it('Should ignore PRs not merged into develop branch', async () => { toTag: 'v1.4.0', failOnError: false, commitMode: false, - configuration: configuration + configuration }) const changeLog = await releaseNotes.pull() @@ -192,7 +192,7 @@ it('Should ignore PRs not merged into main branch', async () => { toTag: 'v1.4.0', failOnError: false, commitMode: false, - configuration: configuration + configuration }) const changeLog = await releaseNotes.pull() diff --git a/__tests__/releaseNotesBuilder.test.ts b/__tests__/releaseNotesBuilder.test.ts index 7cefffd..8baf259 100644 --- a/__tests__/releaseNotesBuilder.test.ts +++ b/__tests__/releaseNotesBuilder.test.ts @@ -45,7 +45,9 @@ it('Should match generated changelog (unspecified tags)', async () => { const changeLog = await releaseNotesBuilder.build() console.log(changeLog) - expect(changeLog).toStrictEqual(`## ๐Ÿ› Fixes\n\n- Stacktrace Data can be an array\n - PR: #39\n\n`) + expect(changeLog).toStrictEqual( + `## ๐Ÿ› Fixes\n\n- Stacktrace Data can be an array\n - PR: #39\n\n` + ) }) it('Should use empty placeholder', async () => { @@ -168,7 +170,6 @@ it('Uncategorized category', async () => { ) }) - it('Verify commit based changelog', async () => { const configuration = resolveConfiguration( '', @@ -194,7 +195,6 @@ it('Verify commit based changelog', async () => { ) }) - it('Verify commit based changelog, with emoji categorisation', async () => { const configuration = resolveConfiguration( '', diff --git a/__tests__/transform.test.ts b/__tests__/transform.test.ts index f2d7cca..c1b6685 100644 --- a/__tests__/transform.test.ts +++ b/__tests__/transform.test.ts @@ -1,240 +1,293 @@ import {buildChangelog} from '../src/transform' -import { PullRequestInfo } from '../src/pullRequests' +import {PullRequestInfo} from '../src/pullRequests' import moment from 'moment' -import { DefaultConfiguration } from '../src/configuration'; +import {DefaultConfiguration} from '../src/configuration' jest.setTimeout(180000) -let configuration = DefaultConfiguration +const configuration = DefaultConfiguration configuration.categories = [ { - "title": "## ๐Ÿš€ Features", - "labels": ["[Feature]"] + title: '## ๐Ÿš€ Features', + labels: ['[Feature]'] }, { - "title": "## ๐Ÿ› Fixes", - "labels": ["[Bug]", "[Issue]"] + title: '## ๐Ÿ› Fixes', + labels: ['[Bug]', '[Issue]'] }, { - "title": "## ๐Ÿงช Tests", - "labels": ["[Test]"] + title: '## ๐Ÿงช Tests', + labels: ['[Test]'] } ] -let mergedPullRequests: PullRequestInfo[] = [] -mergedPullRequests.push({ - number: 1, - title: "[Feature][AB-1234] - this is a PR 1 title message", - htmlURL: "", - baseBranch: "", - mergedAt: moment(), - mergeCommitSha: "sha1", - author: "Mike", - repoName: "test-repo", - labels: new Set(), - milestone: "", - body: "no magic body for this matter", - assignees: [], - requestedReviewers: [] -}, { - number: 2, - title: "[Issue][AB-4321] - this is a PR 2 title message", - htmlURL: "", - baseBranch: "", - mergedAt: moment(), - mergeCommitSha: "sha1", - author: "Mike", - repoName: "test-repo", - labels: new Set(), - milestone: "", - body: "no magic body for this matter", - assignees: [], - requestedReviewers: [] -}, { - number: 3, - title: "[Issue][Feature][AB-1234321] - this is a PR 3 title message", - htmlURL: "", - baseBranch: "", - mergedAt: moment(), - mergeCommitSha: "sha1", - author: "Mike", - repoName: "test-repo", - labels: new Set(), - milestone: "", - body: "no magic body for this matter", - assignees: [], - requestedReviewers: [] -}, { - number: 4, - title: "[AB-404] - not found label", - htmlURL: "", - baseBranch: "", - mergedAt: moment(), - mergeCommitSha: "sha1", - author: "Mike", - repoName: "test-repo", - labels: new Set(), - milestone: "", - body: "no magic body for this matter", - assignees: [], - requestedReviewers: [] -}) +// list of PRs without labels assigned (extract from title) +const mergedPullRequests: PullRequestInfo[] = [] +mergedPullRequests.push( + { + number: 1, + title: '[Feature][AB-1234] - this is a PR 1 title message', + htmlURL: '', + baseBranch: '', + mergedAt: moment(), + mergeCommitSha: 'sha1', + author: 'Mike', + repoName: 'test-repo', + labels: new Set(), + milestone: '', + body: 'no magic body for this matter', + assignees: [], + requestedReviewers: [] + }, + { + number: 2, + title: '[Issue][AB-4321] - this is a PR 2 title message', + htmlURL: '', + baseBranch: '', + mergedAt: moment(), + mergeCommitSha: 'sha1', + author: 'Mike', + repoName: 'test-repo', + labels: new Set(), + milestone: '', + body: 'no magic body for this matter', + assignees: [], + requestedReviewers: [] + }, + { + number: 3, + title: '[Issue][Feature][AB-1234321] - this is a PR 3 title message', + htmlURL: '', + baseBranch: '', + mergedAt: moment(), + mergeCommitSha: 'sha1', + author: 'Mike', + repoName: 'test-repo', + labels: new Set(), + milestone: '', + body: 'no magic body for this matter', + assignees: [], + requestedReviewers: [] + }, + { + number: 4, + title: '[AB-404] - not found label', + htmlURL: '', + baseBranch: '', + mergedAt: moment(), + mergeCommitSha: 'sha1', + author: 'Mike', + repoName: 'test-repo', + labels: new Set(), + milestone: '', + body: 'no magic body for this matter', + assignees: [], + requestedReviewers: [] + } +) it('Extract label from title, combined regex', async () => { configuration.label_extractor = [ { - "pattern": ".*(\\[Feature\\]|\\[Issue\\]).*", - "target": "$1", - "on_property": "title" + pattern: '.*(\\[Feature\\]|\\[Issue\\]).*', + target: '$1', + on_property: 'title' } ] - const resultChangelog = buildChangelog( - mergedPullRequests, - { - owner: "mikepenz", - repo: "test-repo", - fromTag: "1.0.0", - toTag: "2.0.0", - failOnError: false, - commitMode: false, - configuration: configuration - } - ) + const resultChangelog = buildChangelog(mergedPullRequests, { + owner: 'mikepenz', + repo: 'test-repo', + fromTag: '1.0.0', + toTag: '2.0.0', + failOnError: false, + commitMode: false, + configuration + }) - expect(resultChangelog).toStrictEqual(`## ๐Ÿš€ Features\n\n- [Feature][AB-1234] - this is a PR 1 title message\n - PR: #1\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n## ๐Ÿ› Fixes\n\n- [Issue][AB-4321] - this is a PR 2 title message\n - PR: #2\n\n`) + expect(resultChangelog).toStrictEqual( + `## ๐Ÿš€ Features\n\n- [Feature][AB-1234] - this is a PR 1 title message\n - PR: #1\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n## ๐Ÿ› Fixes\n\n- [Issue][AB-4321] - this is a PR 2 title message\n - PR: #2\n\n` + ) }) it('Extract label from title, split regex', async () => { configuration.label_extractor = [ { - "pattern": ".*(\\[Feature\\]).*", - "target": "$1", - "on_property": "title" + pattern: '.*(\\[Feature\\]).*', + target: '$1', + on_property: 'title' }, { - "pattern": ".*(\\[Issue\\]).*", - "target": "$1", - "on_property": "title" + pattern: '.*(\\[Issue\\]).*', + target: '$1', + on_property: 'title' } ] - const resultChangelog = buildChangelog( - mergedPullRequests, - { - owner: "mikepenz", - repo: "test-repo", - fromTag: "1.0.0", - toTag: "2.0.0", - failOnError: false, - commitMode: false, - configuration: configuration - } - ) + const resultChangelog = buildChangelog(mergedPullRequests, { + owner: 'mikepenz', + repo: 'test-repo', + fromTag: '1.0.0', + toTag: '2.0.0', + failOnError: false, + commitMode: false, + configuration + }) - expect(resultChangelog).toStrictEqual(`## ๐Ÿš€ Features\n\n- [Feature][AB-1234] - this is a PR 1 title message\n - PR: #1\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n## ๐Ÿ› Fixes\n\n- [Issue][AB-4321] - this is a PR 2 title message\n - PR: #2\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n`) + expect(resultChangelog).toStrictEqual( + `## ๐Ÿš€ Features\n\n- [Feature][AB-1234] - this is a PR 1 title message\n - PR: #1\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n## ๐Ÿ› Fixes\n\n- [Issue][AB-4321] - this is a PR 2 title message\n - PR: #2\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n` + ) }) it('Extract label from title, match', async () => { configuration.label_extractor = [ { - "pattern": "\\[Feature\\]", - "on_property": "title", - "method": "match" + pattern: '\\[Feature\\]', + on_property: 'title', + method: 'match' }, { - "pattern": "\\[Issue\\]", - "on_property": "title", - "method": "match" + pattern: '\\[Issue\\]', + on_property: 'title', + method: 'match' } ] - const resultChangelog = buildChangelog( - mergedPullRequests, - { - owner: "mikepenz", - repo: "test-repo", - fromTag: "1.0.0", - toTag: "2.0.0", - failOnError: false, - commitMode: false, - configuration: configuration - } - ) + const resultChangelog = buildChangelog(mergedPullRequests, { + owner: 'mikepenz', + repo: 'test-repo', + fromTag: '1.0.0', + toTag: '2.0.0', + failOnError: false, + commitMode: false, + configuration + }) - expect(resultChangelog).toStrictEqual(`## ๐Ÿš€ Features\n\n- [Feature][AB-1234] - this is a PR 1 title message\n - PR: #1\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n## ๐Ÿ› Fixes\n\n- [Issue][AB-4321] - this is a PR 2 title message\n - PR: #2\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n`) + expect(resultChangelog).toStrictEqual( + `## ๐Ÿš€ Features\n\n- [Feature][AB-1234] - this is a PR 1 title message\n - PR: #1\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n## ๐Ÿ› Fixes\n\n- [Issue][AB-4321] - this is a PR 2 title message\n - PR: #2\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n` + ) }) it('Extract label from title, match multiple', async () => { configuration.label_extractor = [ { - "pattern": "\\[Feature\\]|\\[Issue\\]", - "on_property": "title", - "method": "match" + pattern: '\\[Feature\\]|\\[Issue\\]', + on_property: 'title', + method: 'match' } ] - const resultChangelog = buildChangelog( - mergedPullRequests, - { - owner: "mikepenz", - repo: "test-repo", - fromTag: "1.0.0", - toTag: "2.0.0", - failOnError: false, - commitMode: false, - configuration: configuration - } - ) + const resultChangelog = buildChangelog(mergedPullRequests, { + owner: 'mikepenz', + repo: 'test-repo', + fromTag: '1.0.0', + toTag: '2.0.0', + failOnError: false, + commitMode: false, + configuration + }) - expect(resultChangelog).toStrictEqual(`## ๐Ÿš€ Features\n\n- [Feature][AB-1234] - this is a PR 1 title message\n - PR: #1\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n## ๐Ÿ› Fixes\n\n- [Issue][AB-4321] - this is a PR 2 title message\n - PR: #2\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n`) + expect(resultChangelog).toStrictEqual( + `## ๐Ÿš€ Features\n\n- [Feature][AB-1234] - this is a PR 1 title message\n - PR: #1\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n## ๐Ÿ› Fixes\n\n- [Issue][AB-4321] - this is a PR 2 title message\n - PR: #2\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n` + ) }) -it('Extract label from title, match multiple exhaustive', async () => { - let customConfig = configuration +// test set of PRs with lables predefined +const pullRequestsWithLabels: PullRequestInfo[] = [] +pullRequestsWithLabels.push( + { + number: 1, + title: '[AB-1234] - this is a PR 1 title message', + htmlURL: '', + baseBranch: '', + mergedAt: moment(), + mergeCommitSha: 'sha1', + author: 'Mike', + repoName: 'test-repo', + labels: new Set().add('Feature'), + milestone: '', + body: 'no magic body for this matter', + assignees: [], + requestedReviewers: [] + }, + { + number: 2, + title: '[AB-4321] - this is a PR 2 title message', + htmlURL: '', + baseBranch: '', + mergedAt: moment(), + mergeCommitSha: 'sha1', + author: 'Mike', + repoName: 'test-repo', + labels: new Set().add('Issue'), + milestone: '', + body: 'no magic body for this matter', + assignees: [], + requestedReviewers: [] + }, + { + number: 3, + title: '[AB-1234321] - this is a PR 3 title message', + htmlURL: '', + baseBranch: '', + mergedAt: moment(), + mergeCommitSha: 'sha1', + author: 'Mike', + repoName: 'test-repo', + labels: new Set().add('Issue').add('Feature'), + milestone: '', + body: 'no magic body for this matter', + assignees: [], + requestedReviewers: [] + }, + { + number: 4, + title: '[AB-404] - not found label', + htmlURL: '', + baseBranch: '', + mergedAt: moment(), + mergeCommitSha: 'sha1', + author: 'Mike', + repoName: 'test-repo', + labels: new Set().add(''), + milestone: '', + body: 'no magic body for this matter', + assignees: [], + requestedReviewers: [] + } +) + +it('Match multiple labels exhaustive for category', async () => { + const customConfig = DefaultConfiguration customConfig.categories = [ { - "title": "## ๐Ÿš€ Features and ๐Ÿ› Issues", - "labels": ["[Feature]", "[Issue]"], - "exhaustive": true + title: '## ๐Ÿš€ Features and ๐Ÿ› Issues', + labels: ['[Feature]', '[Issue]'], + exhaustive: true }, { - "title": "## ๐Ÿš€ Features", - "labels": ["[Feature]", "[Feature2]"], - "exhaustive": true + title: '## ๐Ÿš€ Features', + labels: ['[Feature]', '[Feature2]'], + exhaustive: true }, { - "title": "## ๐Ÿ› Fixes", - "labels": ["[Issue]", "[Issue2]"], - "exhaustive": true - } - ] - - customConfig.label_extractor = [ - { - "pattern": "\\[Feature\\]", - "on_property": "title", - "method": "match" - }, - { - "pattern": "\\[Issue\\]", - "on_property": "title", - "method": "match" + title: '## ๐Ÿ› Fixes', + labels: ['[Issue]', '[Issue2]'], + exhaustive: true } ] - const resultChangelog = buildChangelog( - mergedPullRequests, - { - owner: "mikepenz", - repo: "test-repo", - fromTag: "1.0.0", - toTag: "2.0.0", - failOnError: false, - commitMode: false, - configuration: customConfig - } + const resultChangelog = buildChangelog(mergedPullRequests, { + owner: 'mikepenz', + repo: 'test-repo', + fromTag: '1.0.0', + toTag: '2.0.0', + failOnError: false, + commitMode: false, + configuration: customConfig + }) + + expect(resultChangelog).toStrictEqual( + `## ๐Ÿš€ Features and ๐Ÿ› Issues\n\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n` ) - - expect(resultChangelog).toStrictEqual(`## ๐Ÿš€ Features and ๐Ÿ› Issues\n\n- [Issue][Feature][AB-1234321] - this is a PR 3 title message\n - PR: #3\n\n`) })