Skip to content

Commit

Permalink
test: use assert.rejects and drop chai (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Mar 31, 2021
1 parent a1a0a7b commit 3d4aa4b
Show file tree
Hide file tree
Showing 24 changed files with 538 additions and 703 deletions.
53 changes: 0 additions & 53 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,13 @@
"@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",
"@types/pino": "^6.3.0",
"@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",
Expand Down
2 changes: 0 additions & 2 deletions src/bin/code-suggester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <command> [args]')
Expand Down
4 changes: 2 additions & 2 deletions system-test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
64 changes: 30 additions & 34 deletions test/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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/+++');
});
});
47 changes: 23 additions & 24 deletions test/cli.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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());
});
});

Expand All @@ -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);
});
});
Loading

0 comments on commit 3d4aa4b

Please sign in to comment.