Skip to content

Commit

Permalink
fix: Failing async methods don't fail the test
Browse files Browse the repository at this point in the history
  • Loading branch information
maxjoehnk committed Jul 29, 2019
1 parent 920f82c commit a8aed07
Show file tree
Hide file tree
Showing 4 changed files with 823 additions and 66 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
],
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.5",
"@types/mocha": "^5.2.6",
"@types/sinon": "^7.0.11",
"chai": "^4.2.0",
"mocha": "^5.2.0",
"mocha": "^6.1.4",
"rimraf": "^2.6.3",
"sinon": "^7.3.2",
"ts-node": "^7.0.1",
"typescript": "^3.2.4"
}
Expand Down
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ export function setup<Runner extends TestRunner>(testRunner: Runner): EnhancedTe

function runTestCases(msg: string, testFunction: () => void) {
testCases.forEach(testCase => {
testRunner(`${msg} (${printTestCase(testCase)})`, () => {
testFunction.apply(null, testCase);
});
testRunner(`${msg} (${printTestCase(testCase)})`, testFunction.bind(null, ...testCase));
});
}

Expand Down
68 changes: 56 additions & 12 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,73 @@
import { setup } from '../src';
import * as mocha from 'mocha';
import { assert } from 'chai';
import * as sinon from 'sinon';

const test = setup(mocha.test);
const { suite } = mocha;

// @ts-ignore
process.on('unhandledRejection', (reason) => {
throw reason;
});

suite('Test', () => {
test
.case(1, 2, 3)
.case(4, 5, 9)
.run('a + b = c', (a, b, c) => {
assert.equal(a + b, c);
});
mocha.test('should handle simple cases', () => {
const runner = sinon.stub();
const test = setup(runner);

test
.case(1, 2, 3)
.case(4, 5, 9)
.run('a + b = c', (a, b, c) => {
assert.equal(a + b, c);
});

test('a basic test', () => {
assert.equal(true, true);
assert.doesNotThrow(() => runner.getCall(0).callback());
assert.doesNotThrow(() => runner.getCall(1).callback());
sinon.assert.calledWith(runner, 'a + b = c (1, 2, 3)');
sinon.assert.calledWith(runner, 'a + b = c (4, 5, 9)');
});

test
.case(null)
.run('null case', (arg) => {
assert.isNull(arg);
mocha.test('should handle tests without test cases', () => {
const runner = sinon.stub();
const test = setup(runner);

test('a basic test', () => {
assert.equal(true, true);
});

assert.doesNotThrow(() => runner.getCall(0).callback());
sinon.assert.calledWith(runner, 'a basic test');
});

mocha.test('should handle null case', () => {
const runner = sinon.stub();
const test = setup(runner);

test
.case(null)
.run('null case', (arg) => {
assert.isNull(arg);
});
});


test.case(undefined).run('undefined case', arg => {
assert.isUndefined(arg);
});

mocha.test('it should handle promise rejections', () => {
const runner = (title, run) => {
run()
.then(() => assert.fail('error got discarded'))
.catch(() => {});
};
const test = setup(runner);

test.case()
.run('', async () => {
throw new Error('test');
});
});
});
Loading

0 comments on commit a8aed07

Please sign in to comment.