diff options
author | Alex Browne <stephenalexbrowne@gmail.com> | 2018-08-28 07:07:38 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-28 07:07:38 +0800 |
commit | 2eab0e30b753fb33729db53d141c8e22017cadec (patch) | |
tree | 092d68e0c91459b90fe68980d2203c4cd0b2f3e6 /packages/contracts/test/utils_test | |
parent | fb5ea5d99f444967a72fdf2ea5cc50a3490f17e8 (diff) | |
download | dexon-sol-tools-2eab0e30b753fb33729db53d141c8e22017cadec.tar dexon-sol-tools-2eab0e30b753fb33729db53d141c8e22017cadec.tar.gz dexon-sol-tools-2eab0e30b753fb33729db53d141c8e22017cadec.tar.bz2 dexon-sol-tools-2eab0e30b753fb33729db53d141c8e22017cadec.tar.lz dexon-sol-tools-2eab0e30b753fb33729db53d141c8e22017cadec.tar.xz dexon-sol-tools-2eab0e30b753fb33729db53d141c8e22017cadec.tar.zst dexon-sol-tools-2eab0e30b753fb33729db53d141c8e22017cadec.zip |
fix(contracts): Catch cases where the actual error differs from the expected error (#1032)
* Catch cases where the actual error differs from the expected error
* Add tests for testWithReferenceFuncAsync
* Small style and comment fixes
Diffstat (limited to 'packages/contracts/test/utils_test')
-rw-r--r-- | packages/contracts/test/utils_test/test_with_reference.ts | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/packages/contracts/test/utils_test/test_with_reference.ts b/packages/contracts/test/utils_test/test_with_reference.ts new file mode 100644 index 000000000..8d633cd1f --- /dev/null +++ b/packages/contracts/test/utils_test/test_with_reference.ts @@ -0,0 +1,63 @@ +import * as chai from 'chai'; + +import { chaiSetup } from '../utils/chai_setup'; +import { testWithReferenceFuncAsync } from '../utils/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'); + } + return x / y; +} + +// returns an async function that always returns the given value. +function alwaysValueFunc(value: number): (x: number, y: number) => Promise<number> { + 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> { + return async (x: number, y: number) => { + throw new Error(errMessage); + }; +} + +describe('testWithReferenceFuncAsync', () => { + it('passes when both succeed and actual === expected', async () => { + await 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 () => { + 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('fails when both fail and actual error does not contain expected error', async () => { + expect( + testWithReferenceFuncAsync(alwaysFailFunc('Unexpected math error'), divAsync, [1, 0]), + ).to.be.rejectedWith( + 'MathError: divide by zero\n\tTest case: {"x":1,"y":0}: expected \'MathError: divide by zero\' to include \'Unexpected math 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 }', + ); + }); + + 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}', + ); + }); +}); |