diff --git a/package-lock.json b/package-lock.json index a02062af..e7046d06 100644 --- a/package-lock.json +++ b/package-lock.json @@ -449,12 +449,6 @@ "@types/retry": "*" } }, - "@types/chai": { - "version": "4.2.15", - "resolved": "https://registry.npmjs.org/@types/chai/-/chai-4.2.15.tgz", - "integrity": "sha512-rYff6FI+ZTKAPkJUoyz7Udq3GaoDZnxYDEvdEdFZASiA7PoErltHezDishqQiSDWrGxvxmplH304jyzQmjp0AQ==", - "dev": true - }, "@types/diff": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/@types/diff/-/diff-5.0.0.tgz", @@ -1030,12 +1024,6 @@ "integrity": "sha1-iYUI2iIm84DfkEcoRWhJwVAaSw0=", "dev": true }, - "assertion-error": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz", - "integrity": "sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==", - "dev": true - }, "astral-regex": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/astral-regex/-/astral-regex-2.0.0.tgz", @@ -1263,20 +1251,6 @@ "lodash": "^4.17.14" } }, - "chai": { - "version": "4.3.4", - "resolved": "https://registry.npmjs.org/chai/-/chai-4.3.4.tgz", - "integrity": "sha512-yS5H68VYOCtN1cjfwumDSuzn/9c+yza4f3reKXlE5rUg7SFcCEy90gJvydNgOYtblyf4Zi6jIWRnXOgErta0KA==", - "dev": true, - "requires": { - "assertion-error": "^1.1.0", - "check-error": "^1.0.2", - "deep-eql": "^3.0.1", - "get-func-name": "^2.0.0", - "pathval": "^1.1.1", - "type-detect": "^4.0.5" - } - }, "chalk": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.0.tgz", @@ -1293,12 +1267,6 @@ "integrity": "sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==", "dev": true }, - "check-error": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.2.tgz", - "integrity": "sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=", - "dev": true - }, "cheerio": { "version": "1.0.0-rc.5", "resolved": "https://registry.npmjs.org/cheerio/-/cheerio-1.0.0-rc.5.tgz", @@ -1577,15 +1545,6 @@ "mimic-response": "^1.0.0" } }, - "deep-eql": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-3.0.1.tgz", - "integrity": "sha512-+QeIQyN5ZuO+3Uk5DYh6/1eKO0m0YmJFGNmFHGACpf1ClL1nmlV/p4gNgbl2pJGxgXb4faqo6UE+M5ACEMyVcw==", - "dev": true, - "requires": { - "type-detect": "^4.0.0" - } - }, "deep-extend": { "version": "0.6.0", "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz", @@ -2258,12 +2217,6 @@ "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==" }, - "get-func-name": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.0.tgz", - "integrity": "sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=", - "dev": true - }, "get-intrinsic": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.1.1.tgz", @@ -3804,12 +3757,6 @@ "integrity": "sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==", "dev": true }, - "pathval": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/pathval/-/pathval-1.1.1.tgz", - "integrity": "sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ==", - "dev": true - }, "picomatch": { "version": "2.2.2", "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.2.2.tgz", diff --git a/package.json b/package.json index cbbcfd7c..92634cd4 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,6 @@ "@microsoft/api-extractor": "^7.8.10", "@octokit/types": "^6.12.2", "@types/async-retry": "^1.4.2", - "@types/chai": "^4.2.11", "@types/diff": "^5.0.0", "@types/mocha": "^8.0.0", "@types/node": "^14.0.20", @@ -63,7 +62,6 @@ "@types/proxyquire": "^1.3.28", "@types/sinon": "^9.0.4", "c8": "^7.0.1", - "chai": "^4.2.0", "gts": "^3.0.0", "jsdoc": "^3.6.4", "jsdoc-fresh": "^1.0.1", diff --git a/src/bin/code-suggester.ts b/src/bin/code-suggester.ts index 230bd915..2809aca4 100644 --- a/src/bin/code-suggester.ts +++ b/src/bin/code-suggester.ts @@ -18,8 +18,6 @@ import * as yargs from 'yargs'; import {CREATE_PR_COMMAND, REVIEW_PR_COMMAND, main} from './workflow'; import {logger} from '../logger'; -// tslint:disable:no-unused-expression -// yargs actually is a used expression. TS-lint does not detect it. yargs .scriptName('code-suggester') .usage('$0 [args]') diff --git a/system-test/main.test.ts b/system-test/main.test.ts index 647c6695..53203268 100644 --- a/system-test/main.test.ts +++ b/system-test/main.test.ts @@ -12,11 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {assert} from 'chai'; +import * as assert from 'assert'; import {describe, it} from 'mocha'; describe('System test', () => { it('has a test case', () => { - assert.isOk(true); + assert.ok(true); }); }); diff --git a/test/branch.ts b/test/branch.ts index 398a9813..f49a8228 100644 --- a/test/branch.ts +++ b/test/branch.ts @@ -12,8 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +/* eslint-disable node/no-unsupported-features/node-builtins */ + import {describe, it, before, afterEach} from 'mocha'; -import {assert, expect} from 'chai'; +import * as assert from 'assert'; import {octokit, setup} from './util'; import * as sinon from 'sinon'; import {GetResponseTypeFromEndpointMethod} from '@octokit/types'; @@ -65,7 +67,7 @@ describe('Branch', () => { .resolves(branchResponse); // // tests const headSHA = await getBranchHead(octokit, origin, 'master'); - expect(headSHA).to.equal(branchResponse.data.commit.sha); + assert.strictEqual(headSHA, branchResponse.data.commit.sha); sandbox.assert.calledOnceWithExactly(getBranchStub, { owner: origin.owner, repo: origin.repo, @@ -114,7 +116,7 @@ describe('Branch', () => { .resolves(createRefResponse); // tests const sha = await branch(octokit, origin, upstream, branchName, 'master'); - expect(sha).to.equal(branchResponse.data.commit.sha); + assert.strictEqual(sha, branchResponse.data.commit.sha); sandbox.assert.calledOnceWithExactly(getBranchStub, { owner: upstream.owner, repo: upstream.repo, @@ -166,7 +168,7 @@ describe('Branch', () => { 'existing-branch', 'master' ); - expect(sha).to.equal(branchResponse.data.commit.sha); + assert.strictEqual(sha, branchResponse.data.commit.sha); sandbox.assert.calledOnceWithExactly(getBranchStub, { owner: upstream.owner, repo: upstream.repo, @@ -181,13 +183,12 @@ describe('Branch', () => { }); it('Branching fails when Octokit get branch fails', async () => { - sandbox.stub(octokit.repos, 'getBranch').rejects(Error(testErrorMessage)); - try { - await branch(octokit, origin, upstream, branchName, 'master'); - assert.fail(); - } catch (err) { - expect(err.message).to.equal(testErrorMessage); - } + const error = new Error(testErrorMessage); + sandbox.stub(octokit.repos, 'getBranch').rejects(error); + await assert.rejects( + branch(octokit, origin, upstream, branchName, 'master'), + error + ); }); it('Branching fails when Octokit list branch fails', async () => { const branchResponseBody = await import( @@ -199,14 +200,13 @@ describe('Branch', () => { url: 'http://fake-url.com', data: branchResponseBody, } as GetBranchResponse; + const error = new Error(testErrorMessage); sandbox.stub(octokit.repos, 'getBranch').resolves(branchResponse); - sandbox.stub(octokit.git, 'getRef').rejects(Error(testErrorMessage)); - try { - await branch(octokit, origin, upstream, branchName, 'master'); - assert.fail(); - } catch (err) { - expect(err.message).to.equal(testErrorMessage); - } + sandbox.stub(octokit.git, 'getRef').rejects(error); + await assert.rejects( + branch(octokit, origin, upstream, branchName, 'master'), + error + ); }); it('Branching fails when Octokit create ref fails', async () => { const branchResponseBody = await import( @@ -220,15 +220,14 @@ describe('Branch', () => { } as GetBranchResponse; sandbox.stub(octokit.repos, 'getBranch').resolves(branchResponse); const getRefError = Error('Not Found'); + const createRefError = Error(testErrorMessage); Object.assign(getRefError, {status: 404}); sandbox.stub(octokit.git, 'getRef').rejects(getRefError); - sandbox.stub(octokit.git, 'createRef').rejects(Error(testErrorMessage)); - try { - await branch(octokit, origin, upstream, branchName, 'master'); - assert.fail(); - } catch (err) { - expect(err.message).to.equal(testErrorMessage); - } + sandbox.stub(octokit.git, 'createRef').rejects(createRefError); + await assert.rejects( + branch(octokit, origin, upstream, branchName, 'master'), + createRefError + ); }); it('Branching fails when primary branch specified did not match any of the branches returned', async () => { const branchResponseBody = await import( @@ -241,16 +240,13 @@ describe('Branch', () => { data: branchResponseBody, } as GetBranchResponse; sandbox.stub(octokit.repos, 'getBranch').resolves(branchResponse); - try { - await branch(octokit, origin, upstream, branchName, 'non-master-branch'); - assert.fail(); - } catch (err) { - assert.isOk(true); - } + await assert.rejects( + branch(octokit, origin, upstream, branchName, 'non-master-branch') + ); }); it('the reference string parsing function correctly appends branch name to reference prefix', () => { - assert.equal(createRef('master'), 'refs/heads/master'); - assert.equal(createRef('foo/bar/baz'), 'refs/heads/foo/bar/baz'); - assert.equal(createRef('+++'), 'refs/heads/+++'); + assert.strictEqual(createRef('master'), 'refs/heads/master'); + assert.strictEqual(createRef('foo/bar/baz'), 'refs/heads/foo/bar/baz'); + assert.strictEqual(createRef('+++'), 'refs/heads/+++'); }); }); diff --git a/test/cli.ts b/test/cli.ts index 5453d2ca..4fd0c06b 100644 --- a/test/cli.ts +++ b/test/cli.ts @@ -1,5 +1,20 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/* eslint-disable node/no-unsupported-features/node-builtins */ import {describe, it, afterEach} from 'mocha'; -import {assert, expect} from 'chai'; +import * as assert from 'assert'; import {main, coerceUserCreatePullRequestOptions} from '../src/bin/workflow'; import * as sinon from 'sinon'; import * as proxyquire from 'proxyquire'; @@ -31,16 +46,12 @@ describe('main', () => { './handle-git-dir-change': stubHelperHandlers, }); stubWorkFlow.main(); - assert.isOk(true); }); it('fails when there is no env variable', async () => { - try { - sandbox.stub(process.env, 'ACCESS_TOKEN').value(undefined); - await main(); - assert.fail(); - } catch (err) { - assert.isOk(true); - } + sandbox + .stub(process, 'env') + .value({...process.env, ACCESS_TOKEN: undefined}); + await assert.rejects(main()); }); it('Fails when unrecognized command is given', async () => { @@ -61,12 +72,7 @@ describe('main', () => { const stubWorkFlow = proxyquire.noCallThru()('../src/bin/workflow', { './handle-git-dir-change': stubHelperHandlers, }); - try { - await stubWorkFlow.main(); - assert.fail(); - } catch (err) { - assert.isOk(true); - } + await assert.rejects(stubWorkFlow.main()); }); it('Passes up the error message when fetching change failed', async () => { @@ -75,12 +81,7 @@ describe('main', () => { .stub(process, 'env') .value({...process.env, ACCESS_TOKEN: '123121312'}); sandbox.stub(yargs, 'argv').value({...yargs.argv, _: ['unrecognized']}); - try { - await main(); - assert.fail(); - } catch (err) { - assert.isOk(true); - } + await assert.rejects(main()); }); }); @@ -105,9 +106,7 @@ describe('Mapping pr yargs to create PR options', () => { fork: true, labels: ['automerge'], }; - sandbox.stub(yargs, 'argv').value({_: ['pr'], ...options}); - - expect(coerceUserCreatePullRequestOptions()).to.deep.equals(options); + assert.deepStrictEqual(coerceUserCreatePullRequestOptions(), options); }); }); diff --git a/test/commit-and-push.ts b/test/commit-and-push.ts index 45fe0e11..defd39d6 100644 --- a/test/commit-and-push.ts +++ b/test/commit-and-push.ts @@ -12,7 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {expect} from 'chai'; +/* eslint-disable node/no-unsupported-features/node-builtins */ + +import * as assert from 'assert'; import {describe, it, before, afterEach} from 'mocha'; import {octokit, setup} from './util'; import * as sinon from 'sinon'; @@ -50,7 +52,7 @@ describe('Push', () => { it('GitHub tree objects that are generated correctly for text files in a sub-directory', () => { const changes: Changes = new Map(); changes.set('a/foo.txt', new FileData('Foo content')); - expect(handler.generateTreeObjects(changes)).to.deep.equal([ + assert.deepStrictEqual(handler.generateTreeObjects(changes), [ { path: 'a/foo.txt', mode: '100644', @@ -62,7 +64,7 @@ describe('Push', () => { it('has objects that are generated correctly for text files that are deleted', () => { const changes: Changes = new Map(); changes.set('b/bar.txt', new FileData(null)); - expect(handler.generateTreeObjects(changes)).to.deep.equal([ + assert.deepStrictEqual(handler.generateTreeObjects(changes), [ { path: 'b/bar.txt', mode: '100644', @@ -74,7 +76,7 @@ describe('Push', () => { it('has objects that are generated correctly for deleted exe files', () => { const changes: Changes = new Map(); changes.set('baz.exe', new FileData(null, '100755')); - expect(handler.generateTreeObjects(changes)).to.deep.equal([ + assert.deepStrictEqual(handler.generateTreeObjects(changes), [ { path: 'baz.exe', mode: '100755', @@ -86,7 +88,7 @@ describe('Push', () => { it('has objects that are generated correctly for empty text files', () => { const changes: Changes = new Map(); changes.set('empty.txt', new FileData('')); - expect(handler.generateTreeObjects(changes)).to.deep.equal([ + assert.deepStrictEqual(handler.generateTreeObjects(changes), [ { path: 'empty.txt', mode: '100644', @@ -161,7 +163,7 @@ describe('Push', () => { tree, base_tree: getCommitResponse.data.tree.sha, }); - expect(treeSha).to.equal(createTreeResponse.data.sha); + assert.strictEqual(treeSha, createTreeResponse.data.sha); }); }); @@ -199,7 +201,7 @@ describe('Commit', () => { treeSha, message ); - expect(sha).equals(createCommitResponse.data.sha); + assert.strictEqual(sha, createCommitResponse.data.sha); sandbox.assert.calledOnceWithExactly(stubCreateCommit, { owner: origin.owner, repo: origin.repo, @@ -330,55 +332,16 @@ describe('Commit and push function', async () => { }); }); it('Forwards GitHub error if getCommit fails', async () => { - // setup - const commitErrorMsg = 'Error committing'; - sandbox.stub(octokit.git, 'getCommit').rejects(Error(commitErrorMsg)); - try { - // tests - await handler.createTree(octokit, origin, '', []); - } catch (err) { - expect(err.message).to.equal(commitErrorMsg); - } + const error = new Error('Error committing'); + sandbox.stub(octokit.git, 'getCommit').rejects(error); + await assert.rejects(handler.createTree(octokit, origin, '', []), error); }); it('Forwards GitHub error if createTree fails', async () => { // setup - const createTreeErrorMsg = 'Error committing'; - sandbox.stub(octokit.git, 'getCommit').resolves(getCommitResponse); - sandbox.stub(octokit.git, 'createTree').rejects(Error(createTreeErrorMsg)); - try { - // tests - await handler.createTree(octokit, origin, '', []); - } catch (err) { - expect(err.message).to.equal(createTreeErrorMsg); - } - }); - it('Forwards GitHub error if createCommit fails', async () => { - // setup + const error = new Error('Error committing'); sandbox.stub(octokit.git, 'getCommit').resolves(getCommitResponse); - sandbox.stub(octokit.git, 'createTree').resolves(createTreeResponse); - const createCommitErrorMsg = 'Error creating commit'; - sandbox - .stub(octokit.git, 'createCommit') - .rejects(Error(createCommitErrorMsg)); - try { - // tests - await handler.createTree(octokit, origin, '', []); - } catch (err) { - expect(err.message).to.equal(createCommitErrorMsg); - } - }); - it('Forwards GitHub error if updateRef fails', async () => { - // setup - sandbox.stub(octokit.git, 'getCommit').resolves(getCommitResponse); - sandbox.stub(octokit.git, 'createTree').resolves(createTreeResponse); - sandbox.stub(octokit.git, 'createCommit').resolves(createCommitResponse); - const updateRefErrorMsg = 'Error updating reference'; - sandbox.stub(octokit.git, 'updateRef').rejects(Error(updateRefErrorMsg)); - try { - // tests - await handler.createTree(octokit, origin, '', []); - } catch (err) { - expect(err.message).to.equal(updateRefErrorMsg); - } + sandbox.stub(octokit.git, 'createTree').rejects(error); + // tests + await assert.rejects(handler.createTree(octokit, origin, '', []), error); }); }); diff --git a/test/diff-utils.ts b/test/diff-utils.ts index b49b2447..2cbefd37 100644 --- a/test/diff-utils.ts +++ b/test/diff-utils.ts @@ -17,7 +17,7 @@ import {readFileSync} from 'fs'; import {setup} from './util'; import {resolve} from 'path'; import {parseAllHunks} from '../src/github-handler/diff-utils'; -import {expect} from 'chai'; +import * as assert from 'assert'; const fixturePath = 'test/fixtures/diffs'; @@ -31,9 +31,9 @@ describe('parseAllHunks', () => { resolve(fixturePath, 'one-line-to-one.diff') ).toString(); const allHunks = parseAllHunks(diff); - expect(allHunks.size).to.equal(1); + assert.strictEqual(allHunks.size, 1); const hunks = allHunks.get('cloudbuild.yaml'); - expect(hunks).to.eql([ + assert.deepStrictEqual(hunks, [ { oldStart: 5, oldEnd: 5, @@ -50,9 +50,9 @@ describe('parseAllHunks', () => { resolve(fixturePath, 'one-line-to-many.diff') ).toString(); const allHunks = parseAllHunks(diff); - expect(allHunks.size).to.equal(1); + assert.strictEqual(allHunks.size, 1); const hunks = allHunks.get('cloudbuild.yaml'); - expect(hunks).to.eql([ + assert.deepStrictEqual(hunks, [ { oldStart: 7, oldEnd: 7, @@ -68,9 +68,9 @@ describe('parseAllHunks', () => { resolve(fixturePath, 'one-line-to-many-newline.diff') ).toString(); const allHunks = parseAllHunks(diff); - expect(allHunks.size).to.equal(1); + assert.strictEqual(allHunks.size, 1); const hunks = allHunks.get('cloudbuild.yaml'); - expect(hunks).to.eql([ + assert.deepStrictEqual(hunks, [ { oldStart: 5, oldEnd: 5, @@ -86,9 +86,9 @@ describe('parseAllHunks', () => { resolve(fixturePath, 'many-to-many.diff') ).toString(); const allHunks = parseAllHunks(diff); - expect(allHunks.size).to.equal(1); + assert.strictEqual(allHunks.size, 1); const hunks = allHunks.get('cloudbuild.yaml'); - expect(hunks).to.eql([ + assert.deepStrictEqual(hunks, [ { oldStart: 2, oldEnd: 5, @@ -106,9 +106,9 @@ describe('parseAllHunks', () => { resolve(fixturePath, 'many-to-one.diff') ).toString(); const allHunks = parseAllHunks(diff); - expect(allHunks.size).to.equal(1); + assert.strictEqual(allHunks.size, 1); const hunks = allHunks.get('cloudbuild.yaml'); - expect(hunks).to.eql([ + assert.deepStrictEqual(hunks, [ { oldStart: 2, oldEnd: 5, @@ -123,9 +123,9 @@ describe('parseAllHunks', () => { it('parses deletions', () => { const diff = readFileSync(resolve(fixturePath, 'deletion.diff')).toString(); const allHunks = parseAllHunks(diff); - expect(allHunks.size).to.equal(1); + assert.strictEqual(allHunks.size, 1); const hunks = allHunks.get('cloudbuild.yaml'); - expect(hunks).to.eql([ + assert.deepStrictEqual(hunks, [ { oldStart: 4, oldEnd: 5, @@ -140,9 +140,9 @@ describe('parseAllHunks', () => { it('parses additions', () => { const diff = readFileSync(resolve(fixturePath, 'addition.diff')).toString(); const allHunks = parseAllHunks(diff); - expect(allHunks.size).to.equal(1); + assert.strictEqual(allHunks.size, 1); const hunks = allHunks.get('cloudbuild.yaml'); - expect(hunks).to.eql([ + assert.deepStrictEqual(hunks, [ { oldStart: 6, oldEnd: 5, diff --git a/test/fork.ts b/test/fork.ts index e7f5a72a..e9725f3a 100644 --- a/test/fork.ts +++ b/test/fork.ts @@ -12,7 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {expect} from 'chai'; +/* eslint-disable node/no-unsupported-features/node-builtins */ + +import * as assert from 'assert'; import {describe, it, before, afterEach} from 'mocha'; import {octokit, setup} from './util'; import * as sinon from 'sinon'; @@ -64,20 +66,16 @@ describe('Forking function', () => { sandbox.stub(octokit.repos, 'createFork').resolves(createRefResponse); // tests const res = await fork(octokit, upstream); - expect(res.owner).equals(responseData.owner.login); - expect(res.repo).equals(responseData.name); + assert.strictEqual(res.owner, responseData.owner.login); + assert.strictEqual(res.repo, responseData.name); }); it('Passes the error message with a throw when octokit fails', async () => { // setup const errorMsg = 'Error message'; sandbox.stub(octokit.repos, 'createFork').rejects(errorMsg); - try { - await fork(octokit, upstream); - expect.fail( - 'The fork function should have failed because Octokit failed.' - ); - } catch (err) { - expect(err.message).to.equal(errorMsg); - } + await assert.rejects( + fork(octokit, upstream), + 'The fork function should have failed because Octokit failed.' + ); }); }); diff --git a/test/git-dir-handler.ts b/test/git-dir-handler.ts index 8b4dc4f5..26e84cf3 100644 --- a/test/git-dir-handler.ts +++ b/test/git-dir-handler.ts @@ -12,8 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +/* eslint-disable node/no-unsupported-features/node-builtins */ + import {describe, it, before, afterEach} from 'mocha'; -import {assert, expect} from 'chai'; +import * as assert from 'assert'; import {setup} from './util'; import { getGitFileData, @@ -31,9 +33,6 @@ before(() => { setup(); }); -// tslint:disable:no-unused-expression -// .null triggers ts-lint failure, but is valid chai -// .true triggers ts-lint failure, but is valid chai describe('git directory diff output to git file data + content', () => { const relativeGitDir = '/fixtures/some/git/dir'; const absoluteGitDir = process.cwd() + relativeGitDir; @@ -47,9 +46,9 @@ describe('git directory diff output to git file data + content', () => { const stubReadFile = sandbox.stub(fs, 'readFile').resolves('Text'); const gitDiffTxt = ':100644 000000 8e6c063 0000000 D\tReadme.md'; const gitFileData = await getGitFileData(absoluteGitDir, gitDiffTxt); - expect(gitFileData.path).equals('Readme.md'); - expect(gitFileData.fileData.mode).equals('100644'); - expect(gitFileData.fileData.content).is.null; + assert.strictEqual(gitFileData.path, 'Readme.md'); + assert.strictEqual(gitFileData.fileData.mode, '100644'); + assert.strictEqual(gitFileData.fileData.content, null); sinon.assert.notCalled(stubReadFile); }); it('gets the new file mode, content and path for created files', async () => { @@ -57,9 +56,9 @@ describe('git directory diff output to git file data + content', () => { const stubReadFile = sandbox.stub(fs, 'readFile').yields(null, 'Text'); const gitDiffTxtAdd = ':000000 100644 0000000 8e6c063 A\tReadme.md'; const gitFileDataAdd = await getGitFileData(absoluteGitDir, gitDiffTxtAdd); - expect(gitFileDataAdd.fileData.content).equals('Text'); - expect(gitFileDataAdd.fileData.mode).equals('100644'); - expect(gitFileDataAdd.path).equals('Readme.md'); + assert.strictEqual(gitFileDataAdd.fileData.content, 'Text'); + assert.strictEqual(gitFileDataAdd.fileData.mode, '100644'); + assert.strictEqual(gitFileDataAdd.path, 'Readme.md'); sinon.assert.calledOnce(stubReadFile); }); it('gets the new file mode, content and path for content modified files', async () => { @@ -71,9 +70,9 @@ describe('git directory diff output to git file data + content', () => { absoluteGitDir, gitDiffTxtModified ); - expect(gitFileDataModified.fileData.content).equals('new text'); - expect(gitFileDataModified.fileData.mode).equals('100644'); - expect(gitFileDataModified.path).equals('modified/test.txt'); + assert.strictEqual(gitFileDataModified.fileData.content, 'new text'); + assert.strictEqual(gitFileDataModified.fileData.mode, '100644'); + assert.strictEqual(gitFileDataModified.path, 'modified/test.txt'); sinon.assert.calledOnce(stubReadFile); }); @@ -88,9 +87,12 @@ describe('git directory diff output to git file data + content', () => { absoluteGitDir, gitDiffTxtToExecutable ); - expect(gitFileDataTxtToExecutable.fileData.content).equals('#!/bin/bash'); - expect(gitFileDataTxtToExecutable.fileData.mode).equals('100755'); - expect(gitFileDataTxtToExecutable.path).equals('bin/main/test.exe'); + assert.strictEqual( + gitFileDataTxtToExecutable.fileData.content, + '#!/bin/bash' + ); + assert.strictEqual(gitFileDataTxtToExecutable.fileData.mode, '100755'); + assert.strictEqual(gitFileDataTxtToExecutable.path, 'bin/main/test.exe'); sinon.assert.calledOnce(stubReadFile); }); }); @@ -119,13 +121,13 @@ describe('Path resolving', () => { it("Resolves to absolute path when './' is a prefix", () => { const relativeGitDir = './test/fixtures'; const testingPath = resolvePath(relativeGitDir); - expect(path.isAbsolute(testingPath)).to.be.true; + assert.strictEqual(path.isAbsolute(testingPath), true); }); it('Resolves to absolute path when the leading chars are letters', () => { const relativeGitDir = 'test/fixtures'; const testingPath = resolvePath(relativeGitDir); - expect(path.isAbsolute(testingPath)).to.be.true; + assert.strictEqual(path.isAbsolute(testingPath), true); }); }); @@ -139,18 +141,16 @@ describe('Finding repository root', () => { sandbox .stub(child_process, 'execSync') .returns(Buffer.from('/home/user/work\n')); - expect(findRepoRoot('home/user/work/subdir')).equals('/home/user/work'); + assert.strictEqual( + findRepoRoot('home/user/work/subdir'), + '/home/user/work' + ); }); it('Rethrows execsync error', () => { - sandbox.stub(child_process, 'execSync').throws(Error('Execsync error')); - try { - findRepoRoot('home/user/work/subdir'); - assert.fail(); - } catch (err) { - assert.isOk(true); - expect(err.message).equals('Execsync error'); - } + const error = new Error('Execsync error'); + sandbox.stub(child_process, 'execSync').throws(error); + assert.throws(() => findRepoRoot('home/user/work/subdir'), error); }); }); @@ -170,9 +170,12 @@ describe('parse all git diff output', () => { ) ); const diffs = getAllDiffs(testDir); - expect(diffs[0]).equals(':000000 100644 0000000 8e6c063 A\tadded.txt'); - expect(diffs[1]).equals(':100644 000000 8e6c063 0000000 D\tdeleted.txt'); - expect(diffs.length).equals(2); + assert.strictEqual(diffs[0], ':000000 100644 0000000 8e6c063 A\tadded.txt'); + assert.strictEqual( + diffs[1], + ':100644 000000 8e6c063 0000000 D\tdeleted.txt' + ); + assert.strictEqual(diffs.length, 2); }); }); @@ -190,39 +193,24 @@ describe('parse changes', () => { it('populates change object with everything from a diff output', async () => { sandbox.stub(fs, 'readFile').yields(null, 'new text'); const changes = await parseChanges(diffs, testDir); - expect(changes.get('added.txt')?.mode).equals('100644'); - expect(changes.get('added.txt')?.content).equals('new text'); - expect(changes.get('deleted.txt')?.mode).equals('100644'); - expect(changes.get('deleted.txt')?.content).is.null; + assert.strictEqual(changes.get('added.txt')?.mode, '100644'); + assert.strictEqual(changes.get('added.txt')?.content, 'new text'); + assert.strictEqual(changes.get('deleted.txt')?.mode, '100644'); + assert.strictEqual(changes.get('deleted.txt')?.content, null); }); it('Passes up the error message with a throw when it is ', async () => { // setup sandbox.stub(fs, 'readFile').throws(Error()); - try { - await parseChanges(diffs, ''); - assert.fail(); - } catch (err) { - assert.isOk(true); - } + await assert.rejects(parseChanges(diffs, '')); }); it('Passes up the error message with a throw when reading the file fails', async () => { // setup sandbox.stub(fs, 'readFile').yields(Error(), ''); - try { - await parseChanges(diffs, ''); - assert.fail(); - } catch (err) { - assert.isOk(true); - } + await assert.rejects(parseChanges(diffs, '')); }); it('Passes up the error message with a throw when parsing the diff fails', async () => { // setup - try { - const badDiff = [':000000 100644 0000000 8e6c063 Aadded.txt']; - await parseChanges(badDiff, ''); - assert.fail(); - } catch (err) { - assert.isOk(true); - } + const badDiff = [':000000 100644 0000000 8e6c063 Aadded.txt']; + await assert.rejects(parseChanges(badDiff, '')); }); }); diff --git a/test/helper-review-pull-request.ts b/test/helper-review-pull-request.ts index 5792ccaf..bdf030ed 100644 --- a/test/helper-review-pull-request.ts +++ b/test/helper-review-pull-request.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {assert, expect} from 'chai'; +import * as assert from 'assert'; import {describe, it, before} from 'mocha'; import {octokit, setup} from './util'; import {RepoDomain, FileDiffContent, Hunk} from '../src/types'; @@ -54,12 +54,12 @@ describe('reviewPullRequest', () => { testPullNumber: number, testPageSize: number ) => { - expect(testOctokit).equals(octokit); - expect(testRemote.owner).equals(owner); - expect(testOctokit).equals(octokit); - expect(testRemote.repo).equals(repo); - expect(testPullNumber).equals(pullNumber); - expect(testPageSize).equals(pageSize); + assert.strictEqual(testOctokit, octokit); + assert.strictEqual(testRemote.owner, owner); + assert.strictEqual(testOctokit, octokit); + assert.strictEqual(testRemote.repo, repo); + assert.strictEqual(testPullNumber, pullNumber); + assert.strictEqual(testPageSize, pageSize); numMockedHelpersCalled += 1; return validFileHunks; }, @@ -68,7 +68,7 @@ describe('reviewPullRequest', () => { getRawSuggestionHunks: ( testDiffContents: Map ) => { - expect(testDiffContents).equals(diffContents); + assert.strictEqual(testDiffContents, diffContents); numMockedHelpersCalled += 1; return suggestionHunks; }, @@ -78,8 +78,8 @@ describe('reviewPullRequest', () => { testPullRequestHunks: Map, testSuggestedHunks: Map ) => { - expect(testPullRequestHunks).equals(validFileHunks); - expect(testSuggestedHunks).equals(suggestionHunks); + assert.strictEqual(testPullRequestHunks, validFileHunks); + assert.strictEqual(testSuggestedHunks, suggestionHunks); numMockedHelpersCalled += 1; return {validHunks: validFileHunks, invalidHunks: invalidFileHunks}; }, @@ -92,11 +92,11 @@ describe('reviewPullRequest', () => { testRemote: RepoDomain, testPullNumber: number ) => { - expect(testOctokit).equals(octokit); - expect(testValidHunks).equals(validFileHunks); - expect(testRemote).equals(remote); - expect(testInvalidHunks).equals(invalidFileHunks); - expect(testPullNumber).equals(pullNumber); + assert.strictEqual(testOctokit, octokit); + assert.strictEqual(testValidHunks, validFileHunks); + assert.strictEqual(testRemote, remote); + assert.strictEqual(testInvalidHunks, invalidFileHunks); + assert.strictEqual(testPullNumber, pullNumber); numMockedHelpersCalled += 1; }, }, @@ -109,7 +109,7 @@ describe('reviewPullRequest', () => { pageSize, diffContents ); - expect(numMockedHelpersCalled).equals(4); + assert.strictEqual(numMockedHelpersCalled, 4); }); it('Succeeds when diff string provided', async () => { @@ -138,12 +138,12 @@ describe('reviewPullRequest', () => { testPullNumber: number, testPageSize: number ) => { - expect(testOctokit).equals(octokit); - expect(testRemote.owner).equals(owner); - expect(testOctokit).equals(octokit); - expect(testRemote.repo).equals(repo); - expect(testPullNumber).equals(pullNumber); - expect(testPageSize).equals(pageSize); + assert.strictEqual(testOctokit, octokit); + assert.strictEqual(testRemote.owner, owner); + assert.strictEqual(testOctokit, octokit); + assert.strictEqual(testRemote.repo, repo); + assert.strictEqual(testPullNumber, pullNumber); + assert.strictEqual(testPageSize, pageSize); numMockedHelpersCalled += 1; return validFileHunks; }, @@ -156,11 +156,11 @@ describe('reviewPullRequest', () => { testRemote: RepoDomain, testPullNumber: number ) => { - expect(testOctokit).equals(octokit); - expect(testValidHunks.size).to.equal(1); - expect(testRemote).equals(remote); - expect(testInvalidHunks.size).to.equal(0); - expect(testPullNumber).equals(pullNumber); + assert.strictEqual(testOctokit, octokit); + assert.strictEqual(testValidHunks.size, 1); + assert.strictEqual(testRemote, remote); + assert.strictEqual(testInvalidHunks.size, 0); + assert.strictEqual(testPullNumber, pullNumber); numMockedHelpersCalled += 1; }, }, @@ -173,7 +173,7 @@ describe('reviewPullRequest', () => { pageSize, diffString ); - expect(numMockedHelpersCalled).equals(2); + assert.strictEqual(numMockedHelpersCalled, 2); }); it('Passes up the error message when getPullRequestHunks helper fails', async () => { @@ -188,12 +188,12 @@ describe('reviewPullRequest', () => { testPullNumber: number, testPageSize: number ) => { - expect(testOctokit).equals(octokit); - expect(testRemote.owner).equals(owner); - expect(testOctokit).equals(octokit); - expect(testRemote.repo).equals(repo); - expect(testPullNumber).equals(pullNumber); - expect(testPageSize).equals(pageSize); + assert.strictEqual(testOctokit, octokit); + assert.strictEqual(testRemote.owner, owner); + assert.strictEqual(testOctokit, octokit); + assert.strictEqual(testRemote.repo, repo); + assert.strictEqual(testPullNumber, pullNumber); + assert.strictEqual(testPageSize, pageSize); numMockedHelpersCalled += 1; throw new Error('getPullRequestHunks failed'); }, @@ -210,8 +210,8 @@ describe('reviewPullRequest', () => { ); assert.ok(false); } catch (err) { - expect(numMockedHelpersCalled).equals(1); - expect(err.message).equals('getPullRequestHunks failed'); + assert.strictEqual(numMockedHelpersCalled, 1); + assert.strictEqual(err.message, 'getPullRequestHunks failed'); } }); }); diff --git a/test/hunk-utils.ts b/test/hunk-utils.ts index e10c6590..c3290e97 100644 --- a/test/hunk-utils.ts +++ b/test/hunk-utils.ts @@ -14,7 +14,7 @@ import {describe, it, before} from 'mocha'; import {setup} from './util'; -import {expect} from 'chai'; +import * as assert from 'assert'; import {adjustHunkUp, adjustHunkDown} from '../src/github-handler/hunk-utils'; before(() => { @@ -33,7 +33,7 @@ describe('adjustHunkUp', () => { previousLine: "- name: 'ubuntu'", }; const adjustedHunk = adjustHunkUp(hunk); - expect(adjustedHunk).to.eql({ + assert.deepStrictEqual(adjustedHunk, { oldStart: 4, oldEnd: 5, newStart: 4, @@ -50,7 +50,7 @@ describe('adjustHunkUp', () => { newContent: [" args: ['sleep', '301']"], }; const adjustedHunk = adjustHunkUp(hunk); - expect(adjustedHunk).to.eql(null); + assert.strictEqual(adjustedHunk, null); }); }); @@ -66,7 +66,7 @@ describe('adjustHunkDown', () => { previousLine: "- name: 'ubuntu'", }; const adjustedHunk = adjustHunkDown(hunk); - expect(adjustedHunk).to.eql({ + assert.deepStrictEqual(adjustedHunk, { oldStart: 5, oldEnd: 6, newStart: 5, @@ -83,6 +83,6 @@ describe('adjustHunkDown', () => { newContent: [" args: ['sleep', '301']"], }; const adjustedHunk = adjustHunkDown(hunk); - expect(adjustedHunk).to.eql(null); + assert.deepStrictEqual(adjustedHunk, null); }); }); diff --git a/test/inline-suggest.ts b/test/inline-suggest.ts index 8fcb4792..e7dc06cb 100644 --- a/test/inline-suggest.ts +++ b/test/inline-suggest.ts @@ -12,7 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {expect} from 'chai'; +/* eslint-disable node/no-unsupported-features/node-builtins */ + +import * as assert from 'assert'; import {describe, it, before, afterEach} from 'mocha'; import { GetResponseTypeFromEndpointMethod, @@ -56,7 +58,7 @@ describe('buildFileComments', () => { }; suggestions.set(fileName1, [hunk1]); const comments = buildReviewComments(suggestions); - expect(comments).deep.equals([ + assert.deepStrictEqual(comments, [ { body: '```suggestion\nFoo\n```', path: 'foo.txt', @@ -88,7 +90,7 @@ describe('buildFileComments', () => { suggestions.set(fileName2, [hunk1]); suggestions.set(fileName1, [hunk1, hunk2]); const comments = buildReviewComments(suggestions); - expect(comments).deep.equals([ + assert.deepStrictEqual(comments, [ { body: '```suggestion\nFoo\n```', path: 'bar.txt', @@ -118,7 +120,7 @@ describe('buildFileComments', () => { it('Maps empty suggestion to empty list', () => { const suggestions: Map = new Map(); const comments = buildReviewComments(suggestions); - expect(comments.length).deep.equals(0); + assert.strictEqual(comments.length, 0); }); it('Builds single line comments', () => { const suggestions: Map = new Map(); @@ -132,7 +134,7 @@ describe('buildFileComments', () => { }; suggestions.set(fileName1, [hunk1]); const comments = buildReviewComments(suggestions); - expect(comments).deep.equals([ + assert.deepStrictEqual(comments, [ { body: '```suggestion\nFoo\n```', path: 'foo.txt', @@ -241,7 +243,7 @@ describe('makeInlineSuggestions', () => { remote, pullNumber ); - expect(reivewNumber).equals(80); + assert.strictEqual(reivewNumber, 80); sandbox.assert.calledOnceWithExactly(stubGetPulls, { owner: remote.owner, repo: remote.repo, @@ -324,7 +326,7 @@ describe('makeInlineSuggestions', () => { ); sandbox.assert.notCalled(stubGetPulls); sandbox.assert.notCalled(stubCreateReview); - expect(reviewNumber).equals(null); + assert.strictEqual(reviewNumber, null); }); it('Throws and does not continue when get pull request fails', async () => { @@ -336,19 +338,18 @@ describe('makeInlineSuggestions', () => { const stubCreateReview = sandbox.stub(octokit.pulls, 'createReview'); // tests - try { - await makeInlineSuggestions( + await assert.rejects( + makeInlineSuggestions( octokit, suggestions, outOfScopeSuggestions, remote, pullNumber - ); - expect.fail('Should have failed because get pull request failed'); - } catch (err) { - sandbox.assert.called(stubGetPulls); - sandbox.assert.notCalled(stubCreateReview); - } + ), + 'Should have failed because get pull request failed' + ); + sandbox.assert.called(stubGetPulls); + sandbox.assert.notCalled(stubCreateReview); }); it('Throws when create review comments fails', async () => { // setup @@ -371,20 +372,17 @@ describe('makeInlineSuggestions', () => { .stub(octokit.pulls, 'createReview') .rejects(new Error()); // tests - try { - await makeInlineSuggestions( + await assert.rejects( + makeInlineSuggestions( octokit, suggestions, outOfScopeSuggestions, remote, pullNumber - ); - expect.fail( - 'Should have failed because create pull request review failed' - ); - } catch (err) { - sandbox.assert.called(stubGetPulls); - sandbox.assert.called(stubCreateReview); - } + ), + 'Should have failed because create pull request review failed' + ); + sandbox.assert.called(stubGetPulls); + sandbox.assert.called(stubCreateReview); }); }); diff --git a/test/invalid-hunks.ts b/test/invalid-hunks.ts index ce4c0530..80164a18 100644 --- a/test/invalid-hunks.ts +++ b/test/invalid-hunks.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {expect} from 'chai'; +import * as assert from 'assert'; import {describe, it, before} from 'mocha'; import {setup} from './util'; import {buildSummaryComment} from '../src/github-handler/comment-handler/make-review-handler/message-handler'; @@ -27,7 +27,7 @@ describe('buildErrorMessage', () => { const expectedMessage = ''; const errorMessage = buildSummaryComment(invalidHunks); - expect(errorMessage).to.be.equal(expectedMessage); + assert.strictEqual(errorMessage, expectedMessage); }); it('should handle multiple file entries', () => { @@ -45,7 +45,7 @@ describe('buildErrorMessage', () => { * lines 3-4`; const errorMessage = buildSummaryComment(invalidHunks); - expect(errorMessage).to.be.equal(expectedMessage); + assert.strictEqual(errorMessage, expectedMessage); }); it('should handle multiple entries for a file', () => { @@ -60,6 +60,6 @@ describe('buildErrorMessage', () => { * lines 3-4`; const errorMessage = buildSummaryComment(invalidHunks); - expect(errorMessage).to.be.equal(expectedMessage); + assert.strictEqual(errorMessage, expectedMessage); }); }); diff --git a/test/issues.ts b/test/issues.ts index a7795f85..7211970f 100644 --- a/test/issues.ts +++ b/test/issues.ts @@ -12,7 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {expect} from 'chai'; +/* eslint-disable node/no-unsupported-features/node-builtins */ + +import * as assert from 'assert'; import {describe, it, before, afterEach} from 'mocha'; import {octokit, setup} from './util'; import * as sinon from 'sinon'; @@ -69,7 +71,7 @@ describe('Adding labels', async () => { issue_number: issue_number, labels: labels, }); - expect(resultingLabels).to.deep.equal(['bug', 'enhancement']); + assert.deepStrictEqual(resultingLabels, ['bug', 'enhancement']); }); it('No-op undefined labels', async () => { @@ -83,7 +85,7 @@ describe('Adding labels', async () => { issue_number ); sandbox.assert.neverCalledWith(stub, sinon.match.any); - expect(resultingLabels).to.deep.equal([]); + assert.deepStrictEqual(resultingLabels, []); }); it('No-op with empty labels', async () => { @@ -98,18 +100,16 @@ describe('Adding labels', async () => { [] ); sandbox.assert.neverCalledWith(stub, sinon.match.any); - expect(resultingLabels).to.deep.equal([]); + assert.deepStrictEqual(resultingLabels, []); }); it('Passes up the error message with a throw when octokit issues add labels fails', async () => { // setup - const errorMsg = 'Error message'; - sandbox.stub(octokit.issues, 'addLabels').rejects(Error(errorMsg)); - try { - await addLabels(octokit, upstream, origin, issue_number, labels); - expect.fail(); - } catch (err) { - expect(err.message).to.equal(errorMsg); - } + const error = new Error('Error message'); + sandbox.stub(octokit.issues, 'addLabels').rejects(error); + await assert.rejects( + addLabels(octokit, upstream, origin, issue_number, labels), + error + ); }); }); diff --git a/test/main-make-pr.ts b/test/main-make-pr.ts index 30ceb8c6..ec98ce8f 100644 --- a/test/main-make-pr.ts +++ b/test/main-make-pr.ts @@ -12,7 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {expect} from 'chai'; +/* eslint-disable node/no-unsupported-features/node-builtins */ + import * as assert from 'assert'; import {describe, it, before, afterEach} from 'mocha'; import {octokit, setup} from './util'; @@ -66,8 +67,8 @@ describe('Make PR main function', () => { it('Returns correct values on success', async () => { const stubHelperHandlers = { fork: (octokit: Octokit, upstream: {owner: string; repo: string}) => { - expect(upstream.owner).equals(upstreamOwner); - expect(upstream.repo).equals(upstreamRepo); + assert.strictEqual(upstream.owner, upstreamOwner); + assert.strictEqual(upstream.repo, upstreamRepo); return { owner: originOwner, repo: originRepo, @@ -80,12 +81,12 @@ describe('Make PR main function', () => { testBranch: string, testprimary: string ) => { - expect(origin.owner).equals(originOwner); - expect(origin.repo).equals(originRepo); - expect(upstream.owner).equals(upstreamOwner); - expect(upstream.repo).equals(upstreamRepo); - expect(testBranch).equals(branch); - expect(testprimary).equals(primary); + assert.strictEqual(origin.owner, originOwner); + assert.strictEqual(origin.repo, originRepo); + assert.strictEqual(upstream.owner, upstreamOwner); + assert.strictEqual(upstream.repo, upstreamRepo); + assert.strictEqual(testBranch, branch); + assert.strictEqual(testprimary, primary); return oldHeadSha; }, commitAndPush: ( @@ -95,12 +96,12 @@ describe('Make PR main function', () => { originBranch: {owner: string; repo: string; branch: string}, testMessage: string ) => { - expect(testOldHeadSha).equals(oldHeadSha); - expect(originBranch.owner).equals(originOwner); - expect(originBranch.repo).equals(originRepo); - expect(originBranch.branch).equals(branch); - expect(testChanges).deep.equals(changes); - expect(testMessage).equals(message); + assert.strictEqual(testOldHeadSha, oldHeadSha); + assert.strictEqual(originBranch.owner, originOwner); + assert.strictEqual(originBranch.repo, originRepo); + assert.strictEqual(originBranch.branch, branch); + assert.deepStrictEqual(testChanges, changes); + assert.strictEqual(testMessage, message); }, openPullRequest: ( octokit: Octokit, @@ -110,15 +111,15 @@ describe('Make PR main function', () => { testMaintainersCanModify: boolean, testPrimary: string ) => { - expect(originBranch.owner).equals(originOwner); - expect(originBranch.repo).equals(originRepo); - expect(originBranch.branch).equals(branch); - expect(upstream.owner).equals(upstreamOwner); - expect(upstream.repo).equals(upstreamRepo); - expect(testDescription.body).equals(description); - expect(testDescription.title).equals(title); - expect(testMaintainersCanModify).equals(maintainersCanModify); - expect(testPrimary).equals(primary); + assert.strictEqual(originBranch.owner, originOwner); + assert.strictEqual(originBranch.repo, originRepo); + assert.strictEqual(originBranch.branch, branch); + assert.strictEqual(upstream.owner, upstreamOwner); + assert.strictEqual(upstream.repo, upstreamRepo); + assert.strictEqual(testDescription.body, description); + assert.strictEqual(testDescription.title, title); + assert.strictEqual(testMaintainersCanModify, maintainersCanModify); + assert.strictEqual(testPrimary, primary); }, addLabels: ( octokit: Octokit, @@ -127,12 +128,12 @@ describe('Make PR main function', () => { issue_number: number, labels: string[] ) => { - expect(originBranch.owner).equals(originOwner); - expect(originBranch.repo).equals(originRepo); - expect(originBranch.branch).equals(branch); - expect(upstream.owner).equals(upstreamOwner); - expect(upstream.repo).equals(upstreamRepo); - expect(labels).equals(labelsToAdd); + assert.strictEqual(originBranch.owner, originOwner); + assert.strictEqual(originBranch.repo, originRepo); + assert.strictEqual(originBranch.branch, branch); + assert.strictEqual(upstream.owner, upstreamOwner); + assert.strictEqual(upstream.repo, upstreamRepo); + assert.strictEqual(labels, labelsToAdd); }, }; const stubMakePr = proxyquire.noCallThru()('../src/', { @@ -153,8 +154,8 @@ describe('Make PR main function', () => { testBranch: string, testprimary: string ) => { - expect(upstream.owner).equals(origin.owner); - expect(upstream.repo).equals(origin.repo); + assert.strictEqual(upstream.owner, origin.owner); + assert.strictEqual(upstream.repo, origin.repo); return oldHeadSha; }, commitAndPush: ( @@ -164,12 +165,12 @@ describe('Make PR main function', () => { originBranch: {owner: string; repo: string; branch: string}, testMessage: string ) => { - expect(testOldHeadSha).equals(oldHeadSha); - expect(originBranch.owner).equals(upstreamOwner); - expect(originBranch.repo).equals(upstreamRepo); - expect(originBranch.branch).equals(branch); - expect(testChanges).deep.equals(changes); - expect(testMessage).equals(message); + assert.strictEqual(testOldHeadSha, oldHeadSha); + assert.strictEqual(originBranch.owner, upstreamOwner); + assert.strictEqual(originBranch.repo, upstreamRepo); + assert.strictEqual(originBranch.branch, branch); + assert.deepStrictEqual(testChanges, changes); + assert.strictEqual(testMessage, message); }, openPullRequest: ( octokit: Octokit, @@ -179,15 +180,15 @@ describe('Make PR main function', () => { testMaintainersCanModify: boolean, testPrimary: string ) => { - expect(originBranch.owner).equals(upstreamOwner); - expect(originBranch.repo).equals(upstreamRepo); - expect(originBranch.branch).equals(branch); - expect(upstream.owner).equals(upstreamOwner); - expect(upstream.repo).equals(upstreamRepo); - expect(testDescription.body).equals(description); - expect(testDescription.title).equals(title); - expect(testMaintainersCanModify).equals(maintainersCanModify); - expect(testPrimary).equals(primary); + assert.strictEqual(originBranch.owner, upstreamOwner); + assert.strictEqual(originBranch.repo, upstreamRepo); + assert.strictEqual(originBranch.branch, branch); + assert.strictEqual(upstream.owner, upstreamOwner); + assert.strictEqual(upstream.repo, upstreamRepo); + assert.strictEqual(testDescription.body, description); + assert.strictEqual(testDescription.title, title); + assert.strictEqual(testMaintainersCanModify, maintainersCanModify); + assert.strictEqual(testPrimary, primary); }, addLabels: ( octokit: Octokit, @@ -196,12 +197,12 @@ describe('Make PR main function', () => { issue_number: number, labels: string[] ) => { - expect(originBranch.owner).equals(upstreamOwner); - expect(originBranch.repo).equals(upstreamRepo); - expect(originBranch.branch).equals(branch); - expect(upstream.owner).equals(upstreamOwner); - expect(upstream.repo).equals(upstreamRepo); - expect(labels).equals(labelsToAdd); + assert.strictEqual(originBranch.owner, upstreamOwner); + assert.strictEqual(originBranch.repo, upstreamRepo); + assert.strictEqual(originBranch.branch, branch); + assert.strictEqual(upstream.owner, upstreamOwner); + assert.strictEqual(upstream.repo, upstreamRepo); + assert.strictEqual(labels, labelsToAdd); }, }; const stubMakePr = proxyquire.noCallThru()('../src/', { @@ -215,38 +216,34 @@ describe('Make PR main function', () => { }); it('Passes up the error message with a throw when create fork helper function fails', async () => { - // setup - + const error = new Error('Create fork helper failed'); const stubHelperHandlers = { fork: () => { - throw Error('Create fork helper failed'); + throw error; }, }; const stubMakePr = proxyquire.noCallThru()('../src/', { './github-handler': stubHelperHandlers, }); - try { - await stubMakePr.createPullRequest(octokit, changes, options); - expect.fail( - 'The main function should have errored because the fork helper function failed.' - ); - } catch (err) { - expect(err.message).equals('Create fork helper failed'); - } + await assert.rejects( + stubMakePr.createPullRequest(octokit, changes, options), + error + ); }); it('Passes up the error message with a throw when create branch helper fails', async () => { // setup + const error = new Error('Create branch helper failed'); const stubHelperHandlers = { fork: (octokit: Octokit, upstream: {owner: string; repo: string}) => { - expect(upstream.owner).equals(upstreamOwner); - expect(upstream.repo).equals(upstreamRepo); + assert.strictEqual(upstream.owner, upstreamOwner); + assert.strictEqual(upstream.repo, upstreamRepo); return { owner: originOwner, repo: originRepo, }; }, branch: () => { - throw Error('Create branch helper failed'); + throw error; }, }; const stubMakePr = proxyquire.noCallThru()('../src/', { @@ -255,23 +252,19 @@ describe('Make PR main function', () => { fn: Function, options: {[index: string]: unknown} ) => { - expect(options.retries).equals(5); - expect(options.factor).equals(2.8411); - expect(options.minTimeout).equals(3000); - expect(options.randomize).equals(false); + assert.strictEqual(options.retries, 5); + assert.strictEqual(options.factor, 2.8411); + assert.strictEqual(options.minTimeout, 3000); + assert.strictEqual(options.randomize, false); await retry(() => fn(), { retries: 0, }); }, }); - try { - await stubMakePr.createPullRequest(octokit, changes, options); - expect.fail( - 'The main function should have errored because the branch helper function failed.' - ); - } catch (err) { - expect(err.message).equals('Create branch helper failed'); - } + await assert.rejects( + stubMakePr.createPullRequest(octokit, changes, options), + error + ); }); it('should respect the retry flag', async () => { @@ -294,11 +287,11 @@ describe('Make PR main function', () => { it('Passes up the error message with a throw when helper commit and push helper function fails', async () => { // setup - + const error = new Error('Commit and push helper failed'); const stubHelperHandlers = { fork: (octokit: Octokit, upstream: {owner: string; repo: string}) => { - expect(upstream.owner).equals(upstreamOwner); - expect(upstream.repo).equals(upstreamRepo); + assert.strictEqual(upstream.owner, upstreamOwner); + assert.strictEqual(upstream.repo, upstreamRepo); return { owner: originOwner, repo: originRepo, @@ -311,37 +304,33 @@ describe('Make PR main function', () => { testBranch: string, testprimary: string ) => { - expect(origin.owner).equals(originOwner); - expect(origin.repo).equals(originRepo); - expect(upstream.owner).equals(upstreamOwner); - expect(upstream.repo).equals(upstreamRepo); - expect(testBranch).equals(branch); - expect(testprimary).equals(primary); + assert.strictEqual(origin.owner, originOwner); + assert.strictEqual(origin.repo, originRepo); + assert.strictEqual(upstream.owner, upstreamOwner); + assert.strictEqual(upstream.repo, upstreamRepo); + assert.strictEqual(testBranch, branch); + assert.strictEqual(testprimary, primary); return oldHeadSha; }, commitAndPush: () => { - throw Error('Commit and push helper failed'); + throw error; }, }; const stubMakePr = proxyquire.noCallThru()('../src/', { './github-handler': stubHelperHandlers, }); - try { - await stubMakePr.createPullRequest(octokit, changes, options); - expect.fail( - 'The main function should have errored because the commit and push helper function failed.' - ); - } catch (err) { - expect(err.message).equals('Commit and push helper failed'); - } + await assert.rejects( + stubMakePr.createPullRequest(octokit, changes, options), + error + ); }); it('Passes up the error message with a throw when helper create pr helper function fails', async () => { // setup - + const error = new Error('Create PR helper failed'); const stubHelperHandlers = { fork: (octokit: Octokit, upstream: {owner: string; repo: string}) => { - expect(upstream.owner).equals(upstreamOwner); - expect(upstream.repo).equals(upstreamRepo); + assert.strictEqual(upstream.owner, upstreamOwner); + assert.strictEqual(upstream.repo, upstreamRepo); return { owner: originOwner, repo: originRepo, @@ -354,12 +343,12 @@ describe('Make PR main function', () => { testBranch: string, testprimary: string ) => { - expect(origin.owner).equals(originOwner); - expect(origin.repo).equals(originRepo); - expect(upstream.owner).equals(upstreamOwner); - expect(upstream.repo).equals(upstreamRepo); - expect(testBranch).equals(branch); - expect(testprimary).equals(primary); + assert.strictEqual(origin.owner, originOwner); + assert.strictEqual(origin.repo, originRepo); + assert.strictEqual(upstream.owner, upstreamOwner); + assert.strictEqual(upstream.repo, upstreamRepo); + assert.strictEqual(testBranch, branch); + assert.strictEqual(testprimary, primary); return oldHeadSha; }, commitAndPush: ( @@ -369,49 +358,45 @@ describe('Make PR main function', () => { originBranch: {owner: string; repo: string; branch: string}, testMessage: string ) => { - expect(testOldHeadSha).equals(oldHeadSha); - expect(originBranch.owner).equals(originOwner); - expect(originBranch.repo).equals(originRepo); - expect(originBranch.branch).equals(branch); - expect(testChanges).deep.equals(changes); - expect(testMessage).equals(message); + assert.strictEqual(testOldHeadSha, oldHeadSha); + assert.strictEqual(originBranch.owner, originOwner); + assert.strictEqual(originBranch.repo, originRepo); + assert.strictEqual(originBranch.branch, branch); + assert.deepStrictEqual(testChanges, changes); + assert.strictEqual(testMessage, message); }, openPullRequest: () => { - throw Error('Create PR helper failed'); + throw error; }, }; const stubMakePr = proxyquire.noCallThru()('../src/', { './github-handler': stubHelperHandlers, }); - try { - await stubMakePr.createPullRequest(octokit, changes, options); - expect.fail( - 'The main function should have errored because the commit and push helper function failed.' - ); - } catch (err) { - expect(err.message).equals('Create PR helper failed'); - } + await assert.rejects( + stubMakePr.createPullRequest(octokit, changes, options), + error + ); }); it('Does not execute any GitHub API calls when there are no changes to commit', async () => { // setup const stubHelperHandlers = { fork: () => { - expect.fail( + assert.fail( 'When changeset is null or undefined then GitHub forking should not execute' ); }, branch: () => { - expect.fail( + assert.fail( 'When changeset is null or undefined then GitHub forking should not execute' ); }, commitAndPush: () => { - expect.fail( + assert.fail( 'When changeset is null or undefined then GitHub forking should not execute' ); }, openPullRequest: () => { - expect.fail( + assert.fail( 'When changeset is null or undefined then GitHub forking should not execute' ); }, diff --git a/test/main-pr-option-defaults.ts b/test/main-pr-option-defaults.ts index 1ee58a9a..f7924fc5 100644 --- a/test/main-pr-option-defaults.ts +++ b/test/main-pr-option-defaults.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {expect} from 'chai'; +import * as assert from 'assert'; import {describe, it, before} from 'mocha'; import {setup} from './util'; import { @@ -38,7 +38,7 @@ describe('addPullRequestDefaults', () => { message: 'chore: custom description', }; const gitHubPr1 = addPullRequestDefaults(upstreamOnly); - expect(gitHubPr1).to.deep.equal({ + assert.deepStrictEqual(gitHubPr1, { upstreamOwner: 'owner', upstreamRepo: 'repo', branch: 'code-suggestions', @@ -58,7 +58,7 @@ describe('addPullRequestDefaults', () => { message: 'chore: custom description', }; const gitHubPr2 = addPullRequestDefaults(upstreamAndPrimary); - expect(gitHubPr2).to.deep.equal({ + assert.deepStrictEqual(gitHubPr2, { upstreamOwner: 'owner', upstreamRepo: 'repo', branch: 'code-suggestions', @@ -77,7 +77,7 @@ describe('addPullRequestDefaults', () => { message: 'chore: custom code suggestions message', }; const gitHubPr3 = addPullRequestDefaults(upstreamAndPrDescription); - expect(gitHubPr3).to.deep.equal({ + assert.deepStrictEqual(gitHubPr3, { upstreamOwner: 'owner', upstreamRepo: 'repo', branch: 'code-suggestions', @@ -102,7 +102,7 @@ describe('addPullRequestDefaults', () => { maintainersCanModify: false, }; const gitHubPr = addPullRequestDefaults(options); - expect(gitHubPr).to.deep.equal(options); + assert.deepStrictEqual(gitHubPr, options); }); }); @@ -116,7 +116,7 @@ describe('addReviewCommentsDefaults', () => { const gitHubPrReview = addReviewCommentsDefaults( reviewOptionsWithDefaultPageSize ); - expect(gitHubPrReview).to.deep.equal({ + assert.deepStrictEqual(gitHubPrReview, { owner: 'owner', repo: 'repo', pullNumber: 12345678, @@ -131,6 +131,6 @@ describe('addReviewCommentsDefaults', () => { pageSize: 4321, }; const gitHubPrReview = addReviewCommentsDefaults(reviewOptions); - expect(gitHubPrReview).to.deep.equal(reviewOptions); + assert.deepStrictEqual(gitHubPrReview, reviewOptions); }); }); diff --git a/test/main-review-pull-request.ts b/test/main-review-pull-request.ts index 76c37556..905bfb76 100644 --- a/test/main-review-pull-request.ts +++ b/test/main-review-pull-request.ts @@ -12,7 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {assert, expect} from 'chai'; +/* eslint-disable node/no-unsupported-features/node-builtins */ + +import * as assert from 'assert'; import {describe, it, before} from 'mocha'; import {octokit, setup} from './util'; import {CreateReviewComment, RepoDomain, FileDiffContent} from '../src/types'; @@ -23,8 +25,6 @@ before(() => { }); /* eslint-disable @typescript-eslint/no-unused-vars */ -// tslint:disable:no-unused-expression -// .true triggers ts-lint failure, but is valid chai describe('reviewPullRequest', () => { const diffContents: Map = new Map(); diffContents.set('src/index.ts', { @@ -52,11 +52,11 @@ describe('reviewPullRequest', () => { testPPageSize: number, testDiffContents: Map ) => { - expect(remote.owner).equals(owner); - expect(remote.repo).equals(repo); - expect(testPullNumber).equals(pullNumber); - expect(testPPageSize).equals(pageSize); - expect(testDiffContents).equals(diffContents); + assert.strictEqual(remote.owner, owner); + assert.strictEqual(remote.repo, repo); + assert.strictEqual(testPullNumber, pullNumber); + assert.strictEqual(testPPageSize, pageSize); + assert.strictEqual(testDiffContents, diffContents); numMockedHelpersCalled += 1; }, }; @@ -64,7 +64,7 @@ describe('reviewPullRequest', () => { './github-handler': stubHelperHandlers, }); await stubReviewPr.reviewPullRequest(octokit, diffContents, options); - expect(numMockedHelpersCalled).equals(1); + assert.strictEqual(numMockedHelpersCalled, 1); }); it('Does not call the github handlers when there are no changes to make because user passed null', async () => { @@ -76,7 +76,7 @@ describe('reviewPullRequest', () => { pageSize: number, testDiffContents: Map ) => { - assert.isOk(false); + assert.fail(); }, }; const stubReviewPr = proxyquire.noCallThru()('../src/', { @@ -94,7 +94,7 @@ describe('reviewPullRequest', () => { pageSize: number, testDiffContents: Map ) => { - assert.isOk(false); + assert.fail(); }, }; const stubReviewPr = proxyquire.noCallThru()('../src/', { @@ -112,7 +112,7 @@ describe('reviewPullRequest', () => { pageSize: number, testDiffContents: Map ) => { - assert.isOk(false); + assert.fail(); }, }; const stubReviewPr = proxyquire.noCallThru()('../src/', { @@ -122,8 +122,7 @@ describe('reviewPullRequest', () => { }); it('Passes up the error message when the create review comment helper fails', async () => { - // setup - + const error = new Error('Review pull request helper failed'); const stubHelperHandlers = { reviewPullRequest: ( octokit: Octokit, @@ -132,19 +131,15 @@ describe('reviewPullRequest', () => { pageSize: number, testDiffContents: Map ) => { - throw Error('Review pull request helper failed'); + throw error; }, }; const stubReviewPr = proxyquire.noCallThru()('../src/', { './github-handler': stubHelperHandlers, }); - try { - await stubReviewPr.reviewPullRequest(octokit, diffContents, options); - expect.fail( - 'The main function should have errored because the sub-function failed.' - ); - } catch (err) { - expect(err.message).equals('Review pull request helper failed'); - } + await assert.rejects( + stubReviewPr.reviewPullRequest(octokit, diffContents, options), + error + ); }); }); diff --git a/test/parse-text-files.ts b/test/parse-text-files.ts index ae406bb3..e3eb67e4 100644 --- a/test/parse-text-files.ts +++ b/test/parse-text-files.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {expect} from 'chai'; +import * as assert from 'assert'; import {describe, it, before} from 'mocha'; import {setup} from './util'; import {parseTextFiles} from '../src/'; @@ -22,32 +22,27 @@ before(() => { setup(); }); -// tslint:disable:no-unused-expression -// .true triggers ts-lint failure, but is valid chai - describe('Parse text files function', () => { - it('Parses map objects into Change object', () => { + it('should parse map objects into Change object', () => { const userFilesMap = new Map(); userFilesMap.set('src/index.js', "console.log('hello index!'"); userFilesMap.set('src/foo.js', "console.log('hello foo!'"); userFilesMap.set('src/deleted.js', null); const parsedMap = new Map(); - parsedMap.set('src/index.js', { - content: "console.log('hello index!'", - mode: '100644', - }); - parsedMap.set('src/foo.js', { - content: "console.log('hello foo!'", - mode: '100644', - }); - parsedMap.set('src/deleted.js', { - content: null, - mode: '100644', - }); + parsedMap.set( + 'src/index.js', + new FileData("console.log('hello index!'", '100644') + ); + parsedMap.set( + 'src/foo.js', + new FileData("console.log('hello foo!'", '100644') + ); + parsedMap.set('src/deleted.js', new FileData(null, '100644')); // tests const changes = parseTextFiles(userFilesMap); - expect(changes).to.deep.equal(parsedMap); + console.log(changes); + assert.deepStrictEqual(changes, parsedMap); }); it('Parses objects of string property value into Change object', () => { const userFilesObject = { @@ -55,93 +50,81 @@ describe('Parse text files function', () => { 'src/foo.js': "console.log('hello foo!'", 'src/deleted.js': null, }; - const parsedMap = new Map(); - parsedMap.set('src/index.js', { - content: "console.log('hello index!'", - mode: '100644', - }); - parsedMap.set('src/foo.js', { - content: "console.log('hello foo!'", - mode: '100644', - }); - parsedMap.set('src/deleted.js', { - content: null, - mode: '100644', - }); + parsedMap.set( + 'src/index.js', + new FileData("console.log('hello index!'", '100644') + ); + parsedMap.set( + 'src/foo.js', + new FileData("console.log('hello foo!'", '100644') + ); + parsedMap.set('src/deleted.js', new FileData(null, '100644')); // tests const changes = parseTextFiles(userFilesObject); - expect(changes).to.deep.equal(parsedMap); + assert.deepStrictEqual(changes, parsedMap); }); it('Rejects invalid file data for objects', () => { // tests - try { - const userFilesObject = { - 'src/index.js': [], - }; - parseTextFiles((userFilesObject as unknown) as {[index: string]: string}); - expect.fail( - 'Text files parsing should not accept non-null/non-string content' - ); - } catch (err) { - expect(err instanceof TypeError).true; - } - try { - const userFilesObject = { - 'src/foo.js': 1234, - }; - parseTextFiles((userFilesObject as unknown) as {[index: string]: string}); - expect.fail( - 'Text files parsing should not accept non-null/non-string content' - ); - } catch (err) { - expect(err instanceof TypeError).true; - } - try { - const userFilesObject = { - 'src/deleted.js': undefined, - }; - parseTextFiles((userFilesObject as unknown) as {[index: string]: string}); - expect.fail( - 'Text files parsing should not accept non-null/non-string content' - ); - } catch (err) { - expect(err instanceof TypeError).true; - } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + let userFilesObject: any = { + 'src/index.js': [], + }; + assert.throws( + () => parseTextFiles(userFilesObject), + err => { + assert.ok(err instanceof TypeError); + return true; + } + ); + userFilesObject = { + 'src/foo.js': 1234, + }; + assert.throws( + () => parseTextFiles(userFilesObject), + err => { + assert.ok(err instanceof TypeError); + return true; + } + ); + userFilesObject = { + 'src/deleted.js': undefined, + }; + assert.throws( + () => parseTextFiles(userFilesObject), + err => { + assert.ok(err instanceof TypeError); + return true; + } + ); }); it('Rejects invalid file data for map', () => { - // tests - try { - const userFilesMap = new Map(); - userFilesMap.set('asfd', 1); - parseTextFiles(userFilesMap); - expect.fail( - 'Text files parsing should not accept non-null/non-string content' - ); - } catch (err) { - expect(err instanceof TypeError).true; - } - try { - const userFilesMap = new Map(); - userFilesMap.set('asfd', undefined); - parseTextFiles(userFilesMap); - parseTextFiles(userFilesMap); - expect.fail( - 'Text files parsing should not accept non-null/non-string content' - ); - } catch (err) { - expect(err instanceof TypeError).true; - } - try { - const userFilesMap = new Map(); - userFilesMap.set('asfd', []); - parseTextFiles(userFilesMap); - parseTextFiles(userFilesMap); - expect.fail( - 'Text files parsing should not accept non-null/non-string content' - ); - } catch (err) { - expect(err instanceof TypeError).true; - } + const userFilesMap = new Map(); + userFilesMap.set('asfd', 1); + assert.throws( + () => parseTextFiles(userFilesMap), + err => { + assert.ok(err instanceof TypeError); + return true; + } + ); + + userFilesMap.set('asfd', undefined); + assert.throws( + () => parseTextFiles(userFilesMap), + err => { + assert.ok(err instanceof TypeError); + return true; + } + ); + + userFilesMap.set('asfd', []); + assert.throws( + () => parseTextFiles(userFilesMap), + err => { + assert.ok(err instanceof TypeError); + return true; + } + ); }); }); diff --git a/test/pr.ts b/test/pr.ts index 111347d1..45066407 100644 --- a/test/pr.ts +++ b/test/pr.ts @@ -12,7 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {expect} from 'chai'; +/* eslint-disable node/no-unsupported-features/node-builtins */ + +import * as assert from 'assert'; import {describe, it, before, afterEach} from 'mocha'; import {octokit, setup} from './util'; import * as sinon from 'sinon'; @@ -95,7 +97,7 @@ describe('Opening a pull request', async () => { body: description.body, maintainer_can_modify: true, }); - expect(number).to.equal(1347); + assert.strictEqual(number, 1347); }); describe('When there are similar refs with pull requests open, the current new and unique ref still opens a pr', async () => { @@ -256,14 +258,12 @@ describe('Opening a pull request', async () => { it('Passes up the error message with a throw when octokit list pull fails', async () => { // setup - const errorMsg = 'Error message'; - sandbox.stub(octokit.pulls, 'list').rejects(Error(errorMsg)); - try { - await openPullRequest(octokit, upstream, origin, description); - expect.fail(); - } catch (err) { - expect(err.message).to.equal(errorMsg); - } + const error = new Error('Error message'); + sandbox.stub(octokit.pulls, 'list').rejects(error); + await assert.rejects( + openPullRequest(octokit, upstream, origin, description), + error + ); }); it('Passes up the error message with a throw when octokit create pull fails', async () => { @@ -275,13 +275,11 @@ describe('Opening a pull request', async () => { data: [], }; sandbox.stub(octokit.pulls, 'list').resolves(listPullResponse); - const errorMsg = 'Error message'; - sandbox.stub(octokit.pulls, 'create').rejects(Error(errorMsg)); - try { - await openPullRequest(octokit, upstream, origin, description); - expect.fail(); - } catch (err) { - expect(err.message).to.equal(errorMsg); - } + const error = new Error('Error message'); + sandbox.stub(octokit.pulls, 'create').rejects(error); + await assert.rejects( + openPullRequest(octokit, upstream, origin, description), + error + ); }); }); diff --git a/test/pull-request-hunks.ts b/test/pull-request-hunks.ts index 0cefedd2..bdd176cf 100644 --- a/test/pull-request-hunks.ts +++ b/test/pull-request-hunks.ts @@ -12,7 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {expect} from 'chai'; +/* eslint-disable node/no-unsupported-features/node-builtins */ + +import * as assert from 'assert'; import {describe, it, before, afterEach} from 'mocha'; import {setup} from './util'; import * as sinon from 'sinon'; @@ -87,25 +89,21 @@ describe('getPullRequestHunks', () => { per_page: pageSize, }); const hunks = pullRequestHunks.get('Readme.md'); - expect(hunks).not.equals(null); - expect(hunks!.length).equals(1); - expect(hunks![0].newStart).equals(2); - expect(hunks![0].newEnd).equals(5); + assert.notStrictEqual(hunks, null); + assert.strictEqual(hunks!.length, 1); + assert.strictEqual(hunks![0].newStart, 2); + assert.strictEqual(hunks![0].newEnd, 5); }); it('Passes up the error when a sub-method fails', async () => { // setup - const errorMsg = 'Test error for list files'; - sandbox.stub(octokit.pulls, 'listFiles').rejects(new Error(errorMsg)); + const error = new Error('Test error for list files'); + sandbox.stub(octokit.pulls, 'listFiles').rejects(error); // tests - try { - await getPullRequestHunks(octokit, upstream, pullNumber, pageSize); - expect.fail( - 'The getPullRequestHunks function should have failed because Octokit failed.' - ); - } catch (err) { - expect(err.message).equals(errorMsg); - } + await assert.rejects( + getPullRequestHunks(octokit, upstream, pullNumber, pageSize), + error + ); }); }); diff --git a/test/remote-github-patch-text.ts b/test/remote-github-patch-text.ts index 7a77f4eb..daba4f77 100644 --- a/test/remote-github-patch-text.ts +++ b/test/remote-github-patch-text.ts @@ -12,7 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {expect} from 'chai'; +/* eslint-disable node/no-unsupported-features/node-builtins */ + +import * as assert from 'assert'; import {describe, it, before, afterEach} from 'mocha'; import {setup} from './util'; import * as sinon from 'sinon'; @@ -141,35 +143,29 @@ describe('getCurrentPullRequestPatches', () => { pullNumber, pageSize ); - expect(patches.size).equals(3); - expect(patches.get(listFilesOfPRResult.data[0].filename)).equals( + assert.strictEqual(patches.size, 3); + assert.strictEqual( + patches.get(listFilesOfPRResult.data[0].filename), '@@ -1,2 +1,5 @@\n Hello world\n-!\n+Goodbye World\n+gOodBYE world\n+\n+Goodbye World' ); - expect(patches.get(listFilesOfPRResult.data[1].filename)).equals( + assert.strictEqual( + patches.get(listFilesOfPRResult.data[1].filename), '@@ -1 +1 @@\n-Hello foo\n+' ); - expect(patches.get(listFilesOfPRResult.data[2].filename)).equals( + assert.strictEqual( + patches.get(listFilesOfPRResult.data[2].filename), '@@ -1 +0,0 @@\n-hello world' ); - expect(filesMissingPatch.length).equals(0); + assert.strictEqual(filesMissingPatch.length, 0); }); it('Passes the error message up from octokit when octokit fails', async () => { // setup - const errorMsg = 'Error message'; - sandbox.stub(octokit.pulls, 'listFiles').rejects(Error(errorMsg)); - try { - await getCurrentPullRequestPatches( - octokit, - upstream, - pullNumber, - pageSize - ); - expect.fail( - 'The getCurrentPulLRequestPatches function should have failed because Octokit failed.' - ); - } catch (err) { - expect(err.message).to.equal(errorMsg); - } + const error = new Error('Error message'); + sandbox.stub(octokit.pulls, 'listFiles').rejects(error); + await assert.rejects( + getCurrentPullRequestPatches(octokit, upstream, pullNumber, pageSize), + error + ); }); it('Throws when there is no list file data returned from octokit', async () => { // setup @@ -180,19 +176,10 @@ describe('getCurrentPullRequestPatches', () => { data: [], }; sandbox.stub(octokit.pulls, 'listFiles').resolves(listFilesOfPRResult); - try { - await getCurrentPullRequestPatches( - octokit, - upstream, - pullNumber, - pageSize - ); - expect.fail( - 'The getCurrentPulLRequestPatches function should have failed because Octokit failed.' - ); - } catch (err) { - expect(err.message).to.equal('Empty Pull Request'); - } + await assert.rejects( + getCurrentPullRequestPatches(octokit, upstream, pullNumber, pageSize), + /Empty Pull Request/ + ); }); it('Does not error when there is list file data but no patch data', async () => { // setup @@ -237,7 +224,7 @@ describe('getCurrentPullRequestPatches', () => { pageSize ); sandbox.assert.called(stub); - expect(filesMissingPatch.length).equals(1); - expect(filesMissingPatch[0]).equals('Readme.md'); + assert.strictEqual(filesMissingPatch.length, 1); + assert.strictEqual(filesMissingPatch[0], 'Readme.md'); }); }); diff --git a/test/scope-handler.ts b/test/scope-handler.ts index 31b3297d..b47b917f 100644 --- a/test/scope-handler.ts +++ b/test/scope-handler.ts @@ -15,7 +15,7 @@ import {describe, it, before} from 'mocha'; import {setup} from './util'; import {partitionSuggestedHunksByScope} from '../src/github-handler/comment-handler/get-hunk-scope-handler/scope-handler'; -import {expect} from 'chai'; +import * as assert from 'assert'; import {Hunk} from '../src/types'; before(() => { @@ -55,9 +55,9 @@ describe('partitionSuggestedHunksByScope', () => { pullRequestHunks, suggestedHunks ); - expect(validHunks.get('file1.txt')!.length).to.equal(2); - expect(validHunks.get('file1.txt')).to.eql([hunk1, hunk2]); - expect(invalidHunks.size).to.equal(0); + assert.strictEqual(validHunks.get('file1.txt')!.length, 2); + assert.deepStrictEqual(validHunks.get('file1.txt'), [hunk1, hunk2]); + assert.strictEqual(invalidHunks.size, 0); }); it('allows suggestions in a second hunk', () => { @@ -92,9 +92,9 @@ describe('partitionSuggestedHunksByScope', () => { pullRequestHunks, suggestedHunks ); - expect(validHunks.get('file1.txt')!.length).to.equal(1); - expect(validHunks.get('file1.txt')).to.eql([hunk1]); - expect(invalidHunks.size).to.equal(0); + assert.strictEqual(validHunks.get('file1.txt')!.length, 1); + assert.deepStrictEqual(validHunks.get('file1.txt'), [hunk1]); + assert.strictEqual(invalidHunks.size, 0); }); it('disallows hunk not included in range', () => { @@ -129,9 +129,9 @@ describe('partitionSuggestedHunksByScope', () => { pullRequestHunks, suggestedHunks ); - expect(validHunks.size).to.equal(0); - expect(invalidHunks.get('file1.txt')!.length).to.equal(2); - expect(invalidHunks.get('file1.txt')).to.eql([hunk1, hunk2]); + assert.strictEqual(validHunks.size, 0); + assert.strictEqual(invalidHunks.get('file1.txt')!.length, 2); + assert.deepStrictEqual(invalidHunks.get('file1.txt'), [hunk1, hunk2]); }); it('disallows files not included in valid files', () => { @@ -152,9 +152,9 @@ describe('partitionSuggestedHunksByScope', () => { pullRequestHunks, suggestedHunks ); - expect(validHunks.size).to.equal(0); - expect(invalidHunks.get('file2.txt')!.length).to.equal(1); - expect(invalidHunks.get('file2.txt')).to.eql([hunk1]); + assert.strictEqual(validHunks.size, 0); + assert.strictEqual(invalidHunks.get('file2.txt')!.length, 1); + assert.deepStrictEqual(invalidHunks.get('file2.txt'), [hunk1]); }); it('allows an addition only suggestion', () => { @@ -190,9 +190,9 @@ describe('partitionSuggestedHunksByScope', () => { newEnd: 15, newContent: ['previousLine', 'original'], }; - expect(validHunks.get('file1.txt')!.length).to.equal(1); - expect(validHunks.get('file1.txt')).to.eql([expectedHunk]); - expect(invalidHunks.size).to.equal(0); + assert.strictEqual(validHunks.get('file1.txt')!.length, 1); + assert.deepStrictEqual(validHunks.get('file1.txt'), [expectedHunk]); + assert.strictEqual(invalidHunks.size, 0); }); it('allows an addition only suggestion at the beginning of the range', () => { @@ -229,9 +229,9 @@ describe('partitionSuggestedHunksByScope', () => { newEnd: 11, newContent: ['original', 'nextLine'], }; - expect(validHunks.get('file1.txt')!.length).to.equal(1); - expect(validHunks.get('file1.txt')).to.eql([expectedHunk]); - expect(invalidHunks.size).to.equal(0); + assert.strictEqual(validHunks.get('file1.txt')!.length, 1); + assert.deepStrictEqual(validHunks.get('file1.txt'), [expectedHunk]); + assert.strictEqual(invalidHunks.size, 0); }); it('allows an deletion only suggestion', () => { @@ -267,9 +267,9 @@ describe('partitionSuggestedHunksByScope', () => { newEnd: 14, newContent: ['previousLine'], }; - expect(validHunks.get('file1.txt')!.length).to.equal(1); - expect(validHunks.get('file1.txt')).to.eql([expectedHunk]); - expect(invalidHunks.size).to.equal(0); + assert.strictEqual(validHunks.get('file1.txt')!.length, 1); + assert.deepStrictEqual(validHunks.get('file1.txt'), [expectedHunk]); + assert.strictEqual(invalidHunks.size, 0); }); it('allows an deletion only suggestion at the start of the range', () => { @@ -306,8 +306,8 @@ describe('partitionSuggestedHunksByScope', () => { newEnd: 10, newContent: ['nextLine'], }; - expect(validHunks.get('file1.txt')!.length).to.equal(1); - expect(validHunks.get('file1.txt')).to.eql([expectedHunk]); - expect(invalidHunks.size).to.equal(0); + assert.strictEqual(validHunks.get('file1.txt')!.length, 1); + assert.deepStrictEqual(validHunks.get('file1.txt'), [expectedHunk]); + assert.strictEqual(invalidHunks.size, 0); }); }); diff --git a/test/suggestion-hunk.ts b/test/suggestion-hunk.ts index 64ac7d3c..392daec3 100644 --- a/test/suggestion-hunk.ts +++ b/test/suggestion-hunk.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {expect} from 'chai'; +import * as assert from 'assert'; import {describe, it, before} from 'mocha'; import {setup} from './util'; import {getRawSuggestionHunks} from '../src/github-handler/comment-handler/raw-patch-handler/raw-hunk-handler'; @@ -41,42 +41,46 @@ describe('getRawSuggestionHunks', () => { it("Does not update the user's input of text file diff contents", () => { getRawSuggestionHunks(diffContents); - expect(fileDiffContent1.oldContent).equals('foo'); - expect(fileDiffContent1.newContent).equals('FOO'); - expect(diffContents.get(fileName1)!.oldContent).equals('foo'); - expect(diffContents.get(fileName1)!.newContent).equals('FOO'); - expect(fileDiffContent2.oldContent).equals( + assert.strictEqual(fileDiffContent1.oldContent, 'foo'); + assert.strictEqual(fileDiffContent1.newContent, 'FOO'); + assert.strictEqual(diffContents.get(fileName1)!.oldContent, 'foo'); + assert.strictEqual(diffContents.get(fileName1)!.newContent, 'FOO'); + assert.strictEqual( + fileDiffContent2.oldContent, 'bar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar' ); - expect(fileDiffContent2.newContent).equals( + assert.strictEqual( + fileDiffContent2.newContent, 'foo\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nfoo' ); - expect(diffContents.get(fileName2)!.oldContent).equals( + assert.strictEqual( + diffContents.get(fileName2)!.oldContent, 'bar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar' ); - expect(diffContents.get(fileName2)!.newContent).equals( + assert.strictEqual( + diffContents.get(fileName2)!.newContent, 'foo\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nbar\nfoo' ); }); it('Generates the hunks that are produced by the diff library for all files that are updated', () => { const fileHunks = getRawSuggestionHunks(diffContents); - expect(fileHunks.size).equals(2); - expect(fileHunks.get(fileName1)!.length).equals(1); - expect(fileHunks.get(fileName1)![0].oldStart).equals(1); - expect(fileHunks.get(fileName1)![0].oldEnd).equals(1); - expect(fileHunks.get(fileName1)![0].newStart).equals(1); - expect(fileHunks.get(fileName1)![0].newEnd).equals(1); - expect(fileHunks.get(fileName2)!.length).equals(2); - expect(fileHunks.get(fileName2)![0].oldStart).equals(1); + assert.strictEqual(fileHunks.size, 2); + assert.strictEqual(fileHunks.get(fileName1)!.length, 1); + assert.strictEqual(fileHunks.get(fileName1)![0].oldStart, 1); + assert.strictEqual(fileHunks.get(fileName1)![0].oldEnd, 1); + assert.strictEqual(fileHunks.get(fileName1)![0].newStart, 1); + assert.strictEqual(fileHunks.get(fileName1)![0].newEnd, 1); + assert.strictEqual(fileHunks.get(fileName2)!.length, 2); + assert.strictEqual(fileHunks.get(fileName2)![0].oldStart, 1); // FIXME: See #126 - expect(fileHunks.get(fileName2)![0].oldEnd).equals(0); - expect(fileHunks.get(fileName2)![0].newStart).equals(1); - expect(fileHunks.get(fileName2)![0].newEnd).equals(1); - expect(fileHunks.get(fileName2)![1].oldStart).equals(16); - expect(fileHunks.get(fileName2)![1].oldEnd).equals(16); - expect(fileHunks.get(fileName2)![1].newStart).equals(17); - expect(fileHunks.get(fileName2)![1].newEnd).equals(18); + assert.strictEqual(fileHunks.get(fileName2)![0].oldEnd, 0); + assert.strictEqual(fileHunks.get(fileName2)![0].newStart, 1); + assert.strictEqual(fileHunks.get(fileName2)![0].newEnd, 1); + assert.strictEqual(fileHunks.get(fileName2)![1].oldStart, 16); + assert.strictEqual(fileHunks.get(fileName2)![1].oldEnd, 16); + assert.strictEqual(fileHunks.get(fileName2)![1].newStart, 17); + assert.strictEqual(fileHunks.get(fileName2)![1].newEnd, 18); }); it('Does not generate hunks for changes that contain no updates', () => { @@ -87,6 +91,6 @@ describe('getRawSuggestionHunks', () => { newContent: 'same', }); const fileHunks = getRawSuggestionHunks(samediffContents); - expect(fileHunks.size).equals(0); + assert.strictEqual(fileHunks.size, 0); }); });