From a0ac08c487fe1adf66755c140228a6332e090c90 Mon Sep 17 00:00:00 2001 From: Bob Evans Date: Tue, 26 Nov 2024 13:46:41 -0500 Subject: [PATCH] test: Migrated bin scripts tests to `node:test` --- bin/test/conventional-changelog.test.js | 140 ++++++++++++------------ bin/test/create-docs-pr.test.js | 92 +++++++--------- bin/test/prepare-release.test.js | 131 +++++++++++----------- bin/test/update-snyk-pr.test.js | 109 +++++++++--------- 4 files changed, 234 insertions(+), 238 deletions(-) diff --git a/bin/test/conventional-changelog.test.js b/bin/test/conventional-changelog.test.js index 338849462c..d46484e26b 100644 --- a/bin/test/conventional-changelog.test.js +++ b/bin/test/conventional-changelog.test.js @@ -4,8 +4,8 @@ */ 'use strict' - -const tap = require('tap') +const test = require('node:test') +const assert = require('node:assert') const proxyquire = require('proxyquire') const sinon = require('sinon') const stream = require('node:stream') @@ -63,35 +63,35 @@ const exampleMarkdown = `### v1.0.0 (2020-04-03) * Thing no longer mutates provided inputs, but instead clones inputs before performing modifications. Thing will now always return an entirely new output ` -tap.test('Conventional Changelog Class', (testHarness) => { - testHarness.autoend() - - let clock - let MockGithubSdk - let mockGetPrByCommit - let mockGitLog - let ConventionalChangelog - - testHarness.beforeEach(() => { - clock = sinon.useFakeTimers(new Date('2020-04-03')) - mockGetPrByCommit = sinon.stub() - MockGithubSdk = sinon.stub().returns({ +test('Conventional Changelog Class', async (t) => { + t.beforeEach((ctx) => { + const clock = sinon.useFakeTimers(new Date('2020-04-03')) + const mockGetPrByCommit = sinon.stub() + const MockGithubSdk = sinon.stub().returns({ getPullRequestByCommit: mockGetPrByCommit }) - mockGitLog = new stream.Readable({ objectMode: true }) + const mockGitLog = new stream.Readable({ objectMode: true }) - ConventionalChangelog = proxyquire(CHANGELOG_PATH, { + const ConventionalChangelog = proxyquire(CHANGELOG_PATH, { './github': MockGithubSdk, 'git-raw-commits': sinon.stub().returns(mockGitLog) }) + ctx.nr = { + clock, + mockGetPrByCommit, + mockGitLog, + MockGithubSdk, + ConventionalChangelog + } }) - testHarness.afterEach(() => { - clock.restore() + t.afterEach((ctx) => { + ctx.nr.clock.restore() }) - testHarness.test('rankedGroupSort - should order a list of groupings based on rank', (t) => { + await t.test('rankedGroupSort - should order a list of groupings based on rank', (t) => { + const { ConventionalChangelog } = t.nr const changelog = new ConventionalChangelog({ newVersion: '1.0.0', previousVersion: '0.9.0' }) const groupedCommits = [ @@ -111,23 +111,22 @@ tap.test('Conventional Changelog Class', (testHarness) => { groupedCommits.sort(changelog.rankedGroupSort) - t.equal(groupedCommits[0].title, 'Features') - t.equal(groupedCommits[1].title, 'Bug fixes') - t.equal(groupedCommits[2].title, 'Security improvements') - t.equal(groupedCommits[3].title, 'Performance improvements') - t.equal(groupedCommits[4].title, 'Code refactoring') - t.equal(groupedCommits[5].title, 'Reverts') - t.equal(groupedCommits[6].title, 'Documentation') - t.equal(groupedCommits[7].title, 'Miscellaneous chores') - t.equal(groupedCommits[8].title, 'Styles') - t.equal(groupedCommits[9].title, 'Tests') - t.equal(groupedCommits[10].title, 'Continuous integration') - t.equal(groupedCommits[11].title, 'Build system') - - t.end() + assert.equal(groupedCommits[0].title, 'Features') + assert.equal(groupedCommits[1].title, 'Bug fixes') + assert.equal(groupedCommits[2].title, 'Security improvements') + assert.equal(groupedCommits[3].title, 'Performance improvements') + assert.equal(groupedCommits[4].title, 'Code refactoring') + assert.equal(groupedCommits[5].title, 'Reverts') + assert.equal(groupedCommits[6].title, 'Documentation') + assert.equal(groupedCommits[7].title, 'Miscellaneous chores') + assert.equal(groupedCommits[8].title, 'Styles') + assert.equal(groupedCommits[9].title, 'Tests') + assert.equal(groupedCommits[10].title, 'Continuous integration') + assert.equal(groupedCommits[11].title, 'Build system') }) - testHarness.test('getFormattedCommits - should get a list of commits', async (t) => { + await t.test('getFormattedCommits - should get a list of commits', async (t) => { + const { ConventionalChangelog, mockGetPrByCommit, mockGitLog } = t.nr mockGetPrByCommit.resolves({ html_url: 'https://github.com/newrelic/node-newrelic/pull/123', number: 123 @@ -145,15 +144,14 @@ tap.test('Conventional Changelog Class', (testHarness) => { const changelog = new ConventionalChangelog({ newVersion: '1.0.0', previousVersion: '0.9.0' }) const commits = await changelog.getFormattedCommits() - t.equal(commits.length, 1) + assert.equal(commits.length, 1) const commit = commits[0] - t.same(commit, exampleCommit) - - t.end() + assert.deepEqual(commit, exampleCommit) }) - testHarness.test('addPullRequestMetadata - should add pr info if available', async (t) => { + await t.test('addPullRequestMetadata - should add pr info if available', async (t) => { + const { mockGetPrByCommit, ConventionalChangelog } = t.nr mockGetPrByCommit .onCall(0) .resolves({ @@ -174,16 +172,15 @@ tap.test('Conventional Changelog Class', (testHarness) => { const changelog = new ConventionalChangelog({ newVersion: '1.0.0', previousVersion: '0.9.0' }) await changelog.addPullRequestMetadata(commits) - t.same(commits[0].pr, { + assert.deepEqual(commits[0].pr, { url: 'https://github.com/newrelic/node-newrelic/pull/345', id: 345 }) - t.notOk(commits[1].pr) - - t.end() + assert.ok(!commits[1].pr) }) - testHarness.test('generateJsonChangelog - should create the new JSON changelog entry', (t) => { + await t.test('generateJsonChangelog - should create the new JSON changelog entry', (t) => { + const { ConventionalChangelog } = t.nr const commits = [ { type: 'fix', subject: 'Fixed issue one' }, { type: 'fix', subject: 'Fixed issue two' }, @@ -193,27 +190,27 @@ tap.test('Conventional Changelog Class', (testHarness) => { const changelog = new ConventionalChangelog({ newVersion: '1.0.0', previousVersion: '0.9.0' }) const jsonEntry = changelog.generateJsonChangelog(commits) - t.same(jsonEntry, exampleJson) - t.end() + assert.deepEqual(jsonEntry, exampleJson) }) - testHarness.test( + await t.test( 'generateMarkdownChangelog - should create the new Markdown changelog entry', async (t) => { + const { ConventionalChangelog } = t.nr const changelog = new ConventionalChangelog({ newVersion: '1.0.0', previousVersion: '0.9.0' }) const markdown = await changelog.generateMarkdownChangelog([exampleCommit]) - t.equal(markdown, exampleMarkdown) - t.end() + assert.equal(markdown, exampleMarkdown) } ) - testHarness.test( + await t.test( 'writeMarkdownChangelog - should not update the markdown file if notes already exist', async (t) => { + const { MockGithubSdk, mockGitLog } = t.nr const mockReadFile = sinon.stub().resolves('### v1.0.0') const mockWriteFile = sinon.stub() - ConventionalChangelog = proxyquire(CHANGELOG_PATH, { + const ConventionalChangelog = proxyquire(CHANGELOG_PATH, { './github': MockGithubSdk, 'git-raw-commits': sinon.stub().returns(mockGitLog), 'node:fs/promises': { @@ -224,16 +221,16 @@ tap.test('Conventional Changelog Class', (testHarness) => { const changelog = new ConventionalChangelog({ newVersion: '1.0.0', previousVersion: '0.9.0' }) await changelog.writeMarkdownChangelog(exampleMarkdown) - t.equal(mockWriteFile.callCount, 0) - t.end() + assert.equal(mockWriteFile.callCount, 0) } ) - testHarness.test('writeMarkdownChangelog - should update the markdown file', async (t) => { + await t.test('writeMarkdownChangelog - should update the markdown file', async (t) => { + const { MockGithubSdk, mockGitLog } = t.nr const mockReadFile = sinon.stub().resolves('### v0.9.0') const mockWriteFile = sinon.stub().resolves() - ConventionalChangelog = proxyquire(CHANGELOG_PATH, { + const ConventionalChangelog = proxyquire(CHANGELOG_PATH, { './github': MockGithubSdk, 'git-raw-commits': sinon.stub().returns(mockGitLog), 'node:fs/promises': { @@ -244,22 +241,22 @@ tap.test('Conventional Changelog Class', (testHarness) => { const changelog = new ConventionalChangelog({ newVersion: '1.0.0', previousVersion: '0.9.0' }) await changelog.writeMarkdownChangelog(exampleMarkdown) - t.equal(mockWriteFile.callCount, 1) - t.match(mockWriteFile.args[0][0], 'NEWS.md') - t.equal(mockWriteFile.args[0][1], `${exampleMarkdown}\n### v0.9.0`) - t.equal(mockWriteFile.args[0][2], 'utf-8') - t.end() + assert.equal(mockWriteFile.callCount, 1) + assert.match(mockWriteFile.args[0][0], /NEWS\.md/) + assert.equal(mockWriteFile.args[0][1], `${exampleMarkdown}\n### v0.9.0`) + assert.equal(mockWriteFile.args[0][2], 'utf-8') }) - testHarness.test( + await t.test( 'writeJsonChangelog - should not update the json file if notes already exist', async (t) => { + const { MockGithubSdk, mockGitLog } = t.nr const mockReadFile = sinon .stub() .resolves(JSON.stringify({ entries: [{ version: '1.0.0' }] })) const mockWriteFile = sinon.stub() - ConventionalChangelog = proxyquire(CHANGELOG_PATH, { + const ConventionalChangelog = proxyquire(CHANGELOG_PATH, { './github': MockGithubSdk, 'git-raw-commits': sinon.stub().returns(mockGitLog), 'node:fs/promises': { @@ -270,16 +267,16 @@ tap.test('Conventional Changelog Class', (testHarness) => { const changelog = new ConventionalChangelog({ newVersion: '1.0.0', previousVersion: '0.9.0' }) await changelog.writeJsonChangelog(exampleJson) - t.equal(mockWriteFile.callCount, 0) - t.end() + assert.equal(mockWriteFile.callCount, 0) } ) - testHarness.test('writeJsonChangelog - should update the json file', async (t) => { + await t.test('writeJsonChangelog - should update the json file', async (t) => { + const { MockGithubSdk, mockGitLog } = t.nr const mockReadFile = sinon.stub().resolves(JSON.stringify({ entries: [{ version: '0.9.0' }] })) const mockWriteFile = sinon.stub().resolves() - ConventionalChangelog = proxyquire(CHANGELOG_PATH, { + const ConventionalChangelog = proxyquire(CHANGELOG_PATH, { './github': MockGithubSdk, 'git-raw-commits': sinon.stub().returns(mockGitLog), 'node:fs/promises': { @@ -290,13 +287,12 @@ tap.test('Conventional Changelog Class', (testHarness) => { const changelog = new ConventionalChangelog({ newVersion: '1.0.0', previousVersion: '0.9.0' }) await changelog.writeJsonChangelog(exampleJson) - t.equal(mockWriteFile.callCount, 1) - t.match(mockWriteFile.args[0][0], 'changelog.json') - t.equal( + assert.equal(mockWriteFile.callCount, 1) + assert.match(mockWriteFile.args[0][0], /changelog\.json/) + assert.equal( mockWriteFile.args[0][1], JSON.stringify({ entries: [{ ...exampleJson }, { version: '0.9.0' }] }, null, 2) ) - t.equal(mockWriteFile.args[0][2], 'utf-8') - t.end() + assert.equal(mockWriteFile.args[0][2], 'utf-8') }) }) diff --git a/bin/test/create-docs-pr.test.js b/bin/test/create-docs-pr.test.js index 9f1e652c79..0f9745266c 100644 --- a/bin/test/create-docs-pr.test.js +++ b/bin/test/create-docs-pr.test.js @@ -4,31 +4,29 @@ */ 'use strict' - -const tap = require('tap') +const test = require('node:test') +const assert = require('node:assert') const proxyquire = require('proxyquire') const sinon = require('sinon') const SCRIPT_PATH = '../create-docs-pr' -tap.test('Create Docs PR script', (testHarness) => { - testHarness.autoend() - - testHarness.test('getReleaseNotes', (t) => { - t.autoend() - - let mockFs - let script - - t.beforeEach(() => { - mockFs = { +test('Create Docs PR script', async (t) => { + await t.test('getReleaseNotes', async (t) => { + t.beforeEach((ctx) => { + const mockFs = { readFile: sinon.stub() } - script = proxyquire(SCRIPT_PATH, { + const script = proxyquire(SCRIPT_PATH, { fs: mockFs }) + ctx.nr = { + mockFs, + script + } }) - t.test('should return our release notes', async (t) => { + await t.test('should return our release notes', async (t) => { + const { mockFs, script } = t.nr const expectedMarkdown = [ '### v1.2.3 (2020-04-03)', '', @@ -45,43 +43,40 @@ tap.test('Create Docs PR script', (testHarness) => { const result = await script.getReleaseNotes('v1.2.3', 'NEWS.md') - t.equal(result.releaseDate, '2020-04-03') + assert.equal(result.releaseDate, '2020-04-03') const body = result.body.split('\n') - t.equal(body[0], '#### Stuff heading') - t.equal(body[1], '* first commit') - t.equal(body[4], '### Support statement:') - - t.end() + assert.equal(body[0], '#### Stuff heading') + assert.equal(body[1], '* first commit') + assert.equal(body[4], '### Support statement:') }) }) - testHarness.test('getFrontMatter', (t) => { - t.autoend() - - let mockFs - let script - - t.beforeEach(() => { - mockFs = { + await t.test('getFrontMatter', async (t) => { + t.beforeEach((ctx) => { + const mockFs = { readFile: sinon.stub() } - script = proxyquire(SCRIPT_PATH, { + const script = proxyquire(SCRIPT_PATH, { fs: mockFs }) + ctx.nr = { + mockFs, + script + } }) - t.test('should throw an error if there is no frontmatter', async (t) => { + await t.test('should throw an error if there is no frontmatter', async (t) => { + const { mockFs, script } = t.nr mockFs.readFile.yields(null, JSON.stringify({ entries: [{ version: '1.2.3', changes: [] }] })) // eslint-disable-next-line sonarjs/no-duplicate-string const func = () => script.getFrontMatter('v2.0.0', 'changelog.json') - t.rejects(func, 'Unable to find 2.0.0 entry in changelog.json') - - t.end() + await assert.rejects(func, { message: 'Unable to find 2.0.0 entry in changelog.json' }) }) - t.test('should return our formatted frontmatter', async (t) => { + await t.test('should return our formatted frontmatter', async (t) => { + const { mockFs, script } = t.nr mockFs.readFile.yields( null, JSON.stringify({ @@ -100,15 +95,15 @@ tap.test('Create Docs PR script', (testHarness) => { const result = await script.getFrontMatter('v2.0.0', 'changelog.json') - t.same(result, { + assert.deepEqual(result, { security: '["one","two"]', bugfixes: '["five","six"]', features: '["three","four"]' }) - t.end() }) - t.test('should return empty arrays if missing changes', async (t) => { + await t.test('should return empty arrays if missing changes', async (t) => { + const { mockFs, script } = t.nr mockFs.readFile.yields( null, JSON.stringify({ @@ -123,25 +118,22 @@ tap.test('Create Docs PR script', (testHarness) => { const result = await script.getFrontMatter('v2.0.0', 'changelog.json') - t.same(result, { + assert.deepEqual(result, { security: '[]', bugfixes: '[]', features: '[]' }) - t.end() }) }) - testHarness.test('formatReleaseNotes', (t) => { - t.autoend() - - let script - - t.beforeEach(() => { - script = proxyquire(SCRIPT_PATH, {}) + await t.test('formatReleaseNotes', async (t) => { + t.beforeEach((ctx) => { + const script = proxyquire(SCRIPT_PATH, {}) + ctx.nr = { script } }) - t.test('should generate the release note markdown', (t) => { + await t.test('should generate the release note markdown', (t) => { + const { script } = t.nr const markdown = [ '#### Stuff', '* commit number one', @@ -175,9 +167,7 @@ tap.test('Create Docs PR script', (testHarness) => { '* commit number two' ].join('\n') - t.equal(result, expected) - - t.end() + assert.equal(result, expected) }) }) }) diff --git a/bin/test/prepare-release.test.js b/bin/test/prepare-release.test.js index 905b231c00..f999f0134a 100644 --- a/bin/test/prepare-release.test.js +++ b/bin/test/prepare-release.test.js @@ -4,46 +4,50 @@ */ 'use strict' - -const tap = require('tap') +const test = require('node:test') +const assert = require('node:assert') const proxyquire = require('proxyquire') const sinon = require('sinon') const { getReleaseDate } = require('../prepare-release') -tap.test('Prepare Release script', (testHarness) => { - testHarness.autoend() - - testHarness.test('generateConventionalReleaseNotes', (t) => { - t.autoend() - - let mockConventionalCommands - let MockConventionalChangelog - let mockGithubCommands - let MockGithubSdk - let script - - t.beforeEach(() => { - mockConventionalCommands = { +test('Prepare Release script', async (t) => { + await t.test('generateConventionalReleaseNotes', async (t) => { + t.beforeEach((ctx) => { + const mockConventionalCommands = { getFormattedCommits: sinon.stub(), generateMarkdownChangelog: sinon.stub(), generateJsonChangelog: sinon.stub(), writeMarkdownChangelog: sinon.stub(), writeJsonChangelog: sinon.stub() } - MockConventionalChangelog = sinon.stub().returns(mockConventionalCommands) + const MockConventionalChangelog = sinon.stub().returns(mockConventionalCommands) - mockGithubCommands = { + const mockGithubCommands = { getLatestRelease: sinon.stub() } - MockGithubSdk = sinon.stub().returns(mockGithubCommands) + const MockGithubSdk = sinon.stub().returns(mockGithubCommands) - script = proxyquire('../prepare-release', { + const script = proxyquire('../prepare-release', { './github': MockGithubSdk, './conventional-changelog': MockConventionalChangelog }) + ctx.nr = { + mockConventionalCommands, + MockConventionalChangelog, + mockGithubCommands, + MockGithubSdk, + script + } }) - t.test('should return the markdown and json generated notes', async (t) => { + await t.test('should return the markdown and json generated notes', async (t) => { + const { + mockGithubCommands, + MockConventionalChangelog, + MockGithubSdk, + mockConventionalCommands, + script + } = t.nr mockGithubCommands.getLatestRelease.resolves({ tag_name: 'v1.2.3' }) const expectedCommits = [{ title: 'stuff: commit number one' }] mockConventionalCommands.getFormattedCommits.resolves(expectedCommits) @@ -72,11 +76,11 @@ tap.test('Prepare Release script', (testHarness) => { generateJsonChangelog: true }) - t.same(json, expectedJson) - t.equal(markdown, expectedMarkdown) + assert.deepEqual(json, expectedJson) + assert.equal(markdown, expectedMarkdown) - t.ok(MockGithubSdk.calledWith('org', 'repo')) - t.ok( + assert.ok(MockGithubSdk.calledWith('org', 'repo')) + assert.ok( MockConventionalChangelog.calledWith({ org: 'org', repo: 'repo', @@ -85,18 +89,21 @@ tap.test('Prepare Release script', (testHarness) => { }) ) - t.equal(mockConventionalCommands.getFormattedCommits.callCount, 1) + assert.equal(mockConventionalCommands.getFormattedCommits.callCount, 1) - t.ok(mockConventionalCommands.generateMarkdownChangelog.calledWith(expectedCommits)) - t.ok(mockConventionalCommands.generateJsonChangelog.calledWith(expectedCommits)) + assert.ok(mockConventionalCommands.generateMarkdownChangelog.calledWith(expectedCommits)) + assert.ok(mockConventionalCommands.generateJsonChangelog.calledWith(expectedCommits)) - t.ok(mockConventionalCommands.writeMarkdownChangelog.calledWith(expectedMarkdown, 'NEWS.md')) - t.ok(mockConventionalCommands.writeJsonChangelog.calledWith(expectedJson)) + assert.ok( + mockConventionalCommands.writeMarkdownChangelog.calledWith(expectedMarkdown, 'NEWS.md') + ) + assert.ok(mockConventionalCommands.writeJsonChangelog.calledWith(expectedJson)) }) - t.test( + await t.test( 'should not generate json file updates when generateJsonChangelog is false', async (t) => { + const { mockGithubCommands, mockConventionalCommands, script } = t.nr mockGithubCommands.getLatestRelease.resolves({ tag_name: 'v1.2.3' }) const expectedCommits = [{ title: 'stuff: commit number one' }] mockConventionalCommands.getFormattedCommits.resolves(expectedCommits) @@ -124,56 +131,56 @@ tap.test('Prepare Release script', (testHarness) => { markdownChangelog: 'NEWS.md' }) - t.same(json, undefined) - t.equal(markdown, expectedMarkdown) + assert.deepEqual(json, undefined) + assert.equal(markdown, expectedMarkdown) - t.ok(mockConventionalCommands.generateMarkdownChangelog.calledWith(expectedCommits)) - t.ok( + assert.ok(mockConventionalCommands.generateMarkdownChangelog.calledWith(expectedCommits)) + assert.ok( mockConventionalCommands.writeMarkdownChangelog.calledWith(expectedMarkdown, 'NEWS.md') ) - t.equal(mockConventionalCommands.generateJsonChangelog.callCount, 0) - t.equal(mockConventionalCommands.writeJsonChangelog.callCount, 0) + assert.equal(mockConventionalCommands.generateJsonChangelog.callCount, 0) + assert.equal(mockConventionalCommands.writeJsonChangelog.callCount, 0) } ) }) - testHarness.test('isValid', (t) => { - t.autoend() - - let mockGitCommands - let script - - t.beforeEach(() => { - mockGitCommands = { + await t.test('isValid', async (t) => { + t.beforeEach((ctx) => { + const mockGitCommands = { getPushRemotes: sinon.stub(), getLocalChanges: sinon.stub(), getCurrentBranch: sinon.stub() } - script = proxyquire('../prepare-release', { + const script = proxyquire('../prepare-release', { './git-commands': mockGitCommands }) + ctx.nr = { + mockGitCommands, + script + } }) - t.test('should return true if force mode enabled', async (t) => { + await t.test('should return true if force mode enabled', async (t) => { + const { script } = t.nr const result = await script.isValid({ force: true }) - t.equal(result, true) - t.end() + assert.equal(result, true) }) - t.test('should return true when all checks pass', async (t) => { + await t.test('should return true when all checks pass', async (t) => { + const { mockGitCommands, script } = t.nr mockGitCommands.getPushRemotes.resolves({ origin: 'stuff' }) mockGitCommands.getLocalChanges.resolves([]) mockGitCommands.getCurrentBranch.resolves('test-branch') const result = await script.isValid({ force: false, branch: 'test-branch', remote: 'origin' }) - t.equal(result, true) - t.end() + assert.equal(result, true) }) - t.test('should return false if one check fails', async (t) => { + await t.test('should return false if one check fails', async (t) => { + const { mockGitCommands, script } = t.nr mockGitCommands.getPushRemotes.resolves({ origin: 'stuff' }) mockGitCommands.getLocalChanges.resolves(['stuff']) mockGitCommands.getCurrentBranch.resolves('another-branch') @@ -184,27 +191,27 @@ tap.test('Prepare Release script', (testHarness) => { remote: 'origin' }) - t.equal(result, false) - t.end() + assert.equal(result, false) }) }) }) -tap.test('getReleaseDate', async (t) => { - t.beforeEach(async (t) => { - t.context.now = Date.now +test('getReleaseDate', async (t) => { + t.beforeEach(async (ctx) => { + const now = Date.now Date.now = function now() { return new Date('2023-11-08T22:45:00.000-05:00').getTime() } + ctx.nr = { now } }) - t.afterEach(async (t) => { - Date.now = t.context.now + t.afterEach(async (ctx) => { + Date.now = ctx.nr.now }) - t.test('returns the correct string', async (t) => { + await t.test('returns the correct string', async () => { const expected = '2023-11-08' const found = getReleaseDate() - t.equal(found, expected) + assert.equal(found, expected) }) }) diff --git a/bin/test/update-snyk-pr.test.js b/bin/test/update-snyk-pr.test.js index cfa0c0ef62..3b1f5b7849 100644 --- a/bin/test/update-snyk-pr.test.js +++ b/bin/test/update-snyk-pr.test.js @@ -4,62 +4,66 @@ */ 'use strict' - -const tap = require('tap') +const test = require('node:test') +const assert = require('node:assert') const proxyquire = require('proxyquire').noPreserveCache().noCallThru() const sinon = require('sinon') -tap.test('Update Snyk PR Scripting', (testHarness) => { - testHarness.autoend() - - let originalEnvVars - let originalConsoleLog - let MockGithubSdk - let getPullRequestMock - let updatePullRequestMock - let script - - testHarness.beforeEach(() => { - originalEnvVars = process.env - originalConsoleLog = console.log +test('Update Snyk PR Scripting', async (t) => { + t.beforeEach((ctx) => { + const originalEnvVars = process.env + const originalConsoleLog = console.log console.log = sinon.stub().returns() - getPullRequestMock = sinon.stub() - updatePullRequestMock = sinon.stub() + const getPullRequestMock = sinon.stub() + const updatePullRequestMock = sinon.stub() - MockGithubSdk = sinon.stub().returns({ + const MockGithubSdk = sinon.stub().returns({ getPullRequest: getPullRequestMock, updatePullRequest: updatePullRequestMock }) - script = proxyquire('../update-snyk-pr', { + const script = proxyquire('../update-snyk-pr', { './github': MockGithubSdk }) + ctx.nr = { + originalEnvVars, + originalConsoleLog, + getPullRequestMock, + updatePullRequestMock, + MockGithubSdk, + script + } }) - testHarness.afterEach(() => { - process.env = originalEnvVars - console.log = originalConsoleLog + t.afterEach((ctx) => { + process.env = ctx.nr.originalEnvVars + console.log = ctx.nr.originalConsoleLog }) - testHarness.test('should throw an error if SNYK_PR_ID is missing', async (t) => { + await t.test('should throw an error if SNYK_PR_ID is missing', async (t) => { + const { script } = t.nr delete process.env.SNYK_PR_ID - t.rejects(() => script(), new Error('SNYK_PR_ID is a required environment variable')) + await assert.rejects(() => script(), { + message: 'SNYK_PR_ID is a required environment variable' + }) }) - testHarness.test('should default the org/repo to agent', async (t) => { + await t.test('should default the org/repo to agent', async (t) => { + const { getPullRequestMock, updatePullRequestMock, script, MockGithubSdk } = t.nr getPullRequestMock.resolves({ id: '1234', title: 'oh hi, mark' }) updatePullRequestMock.resolves() process.env.SNYK_PR_ID = '1234' await script() - t.equal(MockGithubSdk.callCount, 1, 'should instantiate the Github SDK') - t.equal(MockGithubSdk.args[0][0], 'newrelic', 'should default to the newrelic org') - t.equal(MockGithubSdk.args[0][1], 'node-newrelic', 'should default to the agent repo') + assert.equal(MockGithubSdk.callCount, 1, 'should instantiate the Github SDK') + assert.equal(MockGithubSdk.args[0][0], 'newrelic', 'should default to the newrelic org') + assert.equal(MockGithubSdk.args[0][1], 'node-newrelic', 'should default to the agent repo') }) - testHarness.test('should set org/repo based on RELEASE_REPO and RELEASE_ORG', async () => { + await t.test('should set org/repo based on RELEASE_REPO and RELEASE_ORG', async (t) => { + const { getPullRequestMock, updatePullRequestMock, script, MockGithubSdk } = t.nr getPullRequestMock.resolves({ id: '1234', title: 'hello from the other side' }) updatePullRequestMock.resolves() process.env.SNYK_PR_ID = '1234' @@ -68,43 +72,42 @@ tap.test('Update Snyk PR Scripting', (testHarness) => { await script() - testHarness.equal(MockGithubSdk.callCount, 1, 'should instantiate the Github SDK') - testHarness.equal(MockGithubSdk.args[0][0], 'foo', 'should respect RELEASE_ORG') - testHarness.equal(MockGithubSdk.args[0][1], 'bar', 'should respect RELEASE_REPO') + assert.equal(MockGithubSdk.callCount, 1, 'should instantiate the Github SDK') + assert.equal(MockGithubSdk.args[0][0], 'foo', 'should respect RELEASE_ORG') + assert.equal(MockGithubSdk.args[0][1], 'bar', 'should respect RELEASE_REPO') }) - testHarness.test( + await t.test( 'should not update the PR if it already has the correct conventional commit prefix', async (t) => { + const { getPullRequestMock, updatePullRequestMock, script } = t.nr getPullRequestMock.resolves({ id: '1234', title: 'security: oh hi, mark' }) updatePullRequestMock.resolves() process.env.SNYK_PR_ID = '1234' await script() - t.equal(updatePullRequestMock.callCount, 0, 'should not have updated the PR') - t.equal(console.log.args[0][0], 'PR #1234 already has correct prefix, skipping update') + assert.equal(updatePullRequestMock.callCount, 0, 'should not have updated the PR') + assert.equal(console.log.args[0][0], 'PR #1234 already has correct prefix, skipping update') } ) - testHarness.test( - 'should update the PR with the security conventional commit prefix', - async (t) => { - getPullRequestMock.resolves({ id: '1234', title: 'hello from the other side' }) - updatePullRequestMock.resolves() - process.env.SNYK_PR_ID = '1234' + await t.test('should update the PR with the security conventional commit prefix', async (t) => { + const { getPullRequestMock, updatePullRequestMock, script } = t.nr + getPullRequestMock.resolves({ id: '1234', title: 'hello from the other side' }) + updatePullRequestMock.resolves() + process.env.SNYK_PR_ID = '1234' - await script() + await script() - t.equal(updatePullRequestMock.callCount, 1, 'should have called updatePullRequest') - t.same( - updatePullRequestMock.args[0][0], - { - id: '1234', - title: 'security: hello from the other side' - }, - 'should have prepended the security prefix' - ) - } - ) + assert.equal(updatePullRequestMock.callCount, 1, 'should have called updatePullRequest') + assert.deepEqual( + updatePullRequestMock.args[0][0], + { + id: '1234', + title: 'security: hello from the other side' + }, + 'should have prepended the security prefix' + ) + }) })