@0x/contracts-test-utils: Update testWithReferenceFunctionAsync() to

support `RevertError`s.
This commit is contained in:
Lawrence Forman
2019-08-01 14:44:19 -04:00
parent d03f13a729
commit 4600a656d1
2 changed files with 128 additions and 77 deletions

View File

@@ -1,11 +1,8 @@
import * as chai from 'chai';
import { AnyRevertError, StringRevertError } from '@0x/utils';
import { chaiSetup } from '../src/chai_setup';
import { expect } from '../src';
import { testWithReferenceFuncAsync } from '../src/test_with_reference';
chaiSetup.configure();
const expect = chai.expect;
async function divAsync(x: number, y: number): Promise<number> {
if (y === 0) {
throw new Error('MathError: divide by zero');
@@ -18,46 +15,77 @@ function alwaysValueFunc(value: number): (x: number, y: number) => Promise<numbe
return async (x: number, y: number) => value;
}
// returns an async function which always throws/rejects with the given error
// message.
function alwaysFailFunc(errMessage: string): (x: number, y: number) => Promise<number> {
// returns an async function which always throws/rejects with the given error.
function alwaysFailFunc(error: Error): (x: number, y: number) => Promise<number> {
return async (x: number, y: number) => {
throw new Error(errMessage);
throw error;
};
}
describe('testWithReferenceFuncAsync', () => {
it('passes when both succeed and actual === expected', async () => {
await testWithReferenceFuncAsync(alwaysValueFunc(0.5), divAsync, [1, 2]);
it('passes when both succeed and actual == expected', async () => {
return testWithReferenceFuncAsync(alwaysValueFunc(0.5), divAsync, [1, 2]);
});
it('passes when both fail and actual error contains expected error', async () => {
await testWithReferenceFuncAsync(alwaysFailFunc('divide by zero'), divAsync, [1, 0]);
it('fails when both succeed and actual != expected', async () => {
return expect(testWithReferenceFuncAsync(alwaysValueFunc(3), divAsync, [1, 2]))
.to.be.rejectedWith('{"x":1,"y":2}: expected 0.5 to deeply equal 3');
});
it('fails when both succeed and actual !== expected', async () => {
expect(testWithReferenceFuncAsync(alwaysValueFunc(3), divAsync, [1, 2])).to.be.rejectedWith(
'Test case {"x":1,"y":2}: expected { value: 0.5 } to deeply equal { value: 3 }',
);
it('passes when both fail and error messages are the same', async () => {
const err = new Error('whoopsie');
return testWithReferenceFuncAsync(alwaysFailFunc(err), alwaysFailFunc(err), [1, 1]);
});
it('fails when both fail and actual error does not contain expected error', async () => {
expect(
testWithReferenceFuncAsync(alwaysFailFunc('Unexpected math error'), divAsync, [1, 0]),
it('fails when both fail and error messages are not identical', async () => {
const errorMessage = 'whoopsie';
const notErrorMessage = 'not whoopsie';
const error = new Error(errorMessage);
const notError = new Error(notErrorMessage);
return expect(
testWithReferenceFuncAsync(alwaysFailFunc(notError), alwaysFailFunc(error), [1, 2]),
).to.be.rejectedWith(
'MathError: divide by zero\n\tTest case: {"x":1,"y":0}: expected \'MathError: divide by zero\' to include \'Unexpected math error\'',
`{"x":1,"y":2}: expected error message '${errorMessage}' to equal '${notErrorMessage}'`,
);
});
it('passes when both fail with compatible RevertErrors', async () => {
const error1 = new StringRevertError('whoopsie');
const error2 = new AnyRevertError();
return testWithReferenceFuncAsync(alwaysFailFunc(error1), alwaysFailFunc(error2), [1, 1]);
});
it('fails when both fail with incompatible RevertErrors', async () => {
const error1 = new StringRevertError('whoopsie');
const error2 = new StringRevertError('not whoopsie');
return expect(testWithReferenceFuncAsync(alwaysFailFunc(error1), alwaysFailFunc(error2), [1, 1]))
.to.be.rejectedWith(
`{"x":1,"y":1}: expected error StringRevertError({ message: 'not whoopsie' }) to equal StringRevertError({ message: 'whoopsie' })`,
);
});
it('fails when reference function fails with a RevertError but test function fails with a regular Error', async () => {
const error1 = new StringRevertError('whoopsie');
const error2 = new Error('whoopsie');
return expect(testWithReferenceFuncAsync(alwaysFailFunc(error1), alwaysFailFunc(error2), [1, 1]))
.to.be.rejectedWith(
`{"x":1,"y":1}: expected a RevertError but received an Error`,
);
});
it('fails when referenceFunc succeeds and testFunc fails', async () => {
expect(testWithReferenceFuncAsync(alwaysValueFunc(0), divAsync, [1, 0])).to.be.rejectedWith(
'Test case {"x":1,"y":0}: expected { error: \'MathError: divide by zero\' } to deeply equal { value: 0 }',
);
const error = new Error('whoopsie');
return expect(testWithReferenceFuncAsync(alwaysValueFunc(0), alwaysFailFunc(error), [1, 2]))
.to.be.rejectedWith(
`{"x":1,"y":2}: expected success but instead failed`,
);
});
it('fails when referenceFunc fails and testFunc succeeds', async () => {
expect(testWithReferenceFuncAsync(alwaysFailFunc('divide by zero'), divAsync, [1, 2])).to.be.rejectedWith(
'Expected error containing divide by zero but got no error\n\tTest case: {"x":1,"y":2}',
);
const error = new Error('whoopsie');
return expect(testWithReferenceFuncAsync(alwaysFailFunc(error), divAsync, [1, 2]))
.to.be.rejectedWith(
'{"x":1,"y":2}: expected failure but instead succeeded',
);
});
});