In `@0x/dev-utils` add more `RevertError` chai helper tests for backwards compatibility with `rejectedWith`. In `@0x/dev-utils` instead of overriding `rejectedWith`, add a new method `revertWith`. In `@0x/dev-utils` clean up the code for the `RevertError` chai helper.
149 lines
7.0 KiB
TypeScript
149 lines
7.0 KiB
TypeScript
import { StringRevertError } from '@0x/utils';
|
|
import * as chai from 'chai';
|
|
|
|
import { chaiSetup } from '../src';
|
|
|
|
chaiSetup.configure();
|
|
const expect = chai.expect;
|
|
|
|
class DescendantRevertError extends StringRevertError {
|
|
constructor(msg: string) {
|
|
super(msg);
|
|
}
|
|
}
|
|
|
|
describe('Chai tests', () => {
|
|
describe('RevertErrors', () => {
|
|
describe('#equal', () => {
|
|
it('should equate two identical RevertErrors', () => {
|
|
const message = 'foo';
|
|
const revert1 = new StringRevertError(message);
|
|
const revert2 = new StringRevertError(message);
|
|
expect(revert1).is.equal(revert2);
|
|
});
|
|
it('should equate two RevertErrors where one has missing fields', () => {
|
|
const revert1 = new StringRevertError('foo');
|
|
const revert2 = new StringRevertError();
|
|
expect(revert1).is.equal(revert2);
|
|
});
|
|
it('should not equate two RevertErrors with diferent fields', () => {
|
|
const revert1 = new StringRevertError('foo1');
|
|
const revert2 = new StringRevertError('foo2');
|
|
expect(revert1).is.not.equal(revert2);
|
|
});
|
|
it('should not equate two RevertErrors with diferent types', () => {
|
|
const message = 'foo';
|
|
const revert1 = new StringRevertError(message);
|
|
const revert2 = new DescendantRevertError(message);
|
|
expect(revert1).is.not.equal(revert2);
|
|
});
|
|
it('should equate a StringRevertError to a string equal to message', () => {
|
|
const message = 'foo';
|
|
const revert = new StringRevertError(message);
|
|
expect(message).is.equal(revert);
|
|
});
|
|
it('should equate an Error to a StringRevertError with an equal message', () => {
|
|
const message = 'foo';
|
|
const revert = new StringRevertError(message);
|
|
const error = new Error(message);
|
|
expect(error).is.equal(revert);
|
|
});
|
|
it('should equate a string to a StringRevertError with the same message', () => {
|
|
const message = 'foo';
|
|
const revert = new StringRevertError(message);
|
|
expect(revert).is.equal(message);
|
|
});
|
|
it('should not equate a StringRevertError to a string not equal to message', () => {
|
|
const revert = new StringRevertError('foo1');
|
|
expect('foo2').is.not.equal(revert);
|
|
});
|
|
it('should not equate a string to a StringRevertError with a different message', () => {
|
|
const revert = new StringRevertError('foo1');
|
|
expect(revert).is.not.equal('foo2');
|
|
});
|
|
it('should not equate an Error to a StringRevertError with a different message', () => {
|
|
const revert = new StringRevertError('foo1');
|
|
const error = new Error('foo2');
|
|
expect(error).is.not.equal(revert);
|
|
});
|
|
});
|
|
describe('#revertWith', () => {
|
|
it('should equate a promise that rejects to identical RevertErrors', async () => {
|
|
const message = 'foo';
|
|
const revert1 = new StringRevertError(message);
|
|
const revert2 = new StringRevertError(message);
|
|
const promise = (async () => {
|
|
throw revert1;
|
|
})();
|
|
return expect(promise).to.revertWith(revert2);
|
|
});
|
|
it('should not equate a promise that rejects to a StringRevertError with a different messages', async () => {
|
|
const revert1 = new StringRevertError('foo1');
|
|
const revert2 = new StringRevertError('foo2');
|
|
const promise = (async () => {
|
|
throw revert1;
|
|
})();
|
|
return expect(promise).to.not.revertWith(revert2);
|
|
});
|
|
it('should not equate a promise that rejects to different RevertError types', async () => {
|
|
const message = 'foo';
|
|
const revert1 = new StringRevertError(message);
|
|
const revert2 = new DescendantRevertError(message);
|
|
const promise = (async () => {
|
|
throw revert1;
|
|
})();
|
|
return expect(promise).to.not.revertWith(revert2);
|
|
});
|
|
});
|
|
describe('#become', () => {
|
|
it('should equate a promise that resolves to an identical RevertErrors', async () => {
|
|
const message = 'foo';
|
|
const revert1 = new StringRevertError(message);
|
|
const revert2 = new StringRevertError(message);
|
|
const promise = (async () => revert1)();
|
|
return expect(promise).to.become(revert2);
|
|
});
|
|
it('should not equate a promise that resolves to a StringRevertError with a different messages', async () => {
|
|
const revert1 = new StringRevertError('foo1');
|
|
const revert2 = new StringRevertError('foo2');
|
|
const promise = (async () => revert1)();
|
|
return expect(promise).to.not.become(revert2);
|
|
});
|
|
it('should not equate a promise that resolves to different RevertError types', async () => {
|
|
const message = 'foo';
|
|
const revert1 = new StringRevertError(message);
|
|
const revert2 = new DescendantRevertError(message);
|
|
const promise = (async () => revert1)();
|
|
return expect(promise).to.not.become(revert2);
|
|
});
|
|
});
|
|
// TODO: Remove these tests when we no longer coerce `Error` types to `StringRevertError` types
|
|
// for backwards compatibility.
|
|
describe('#rejectedWith (backwards compatibility)', () => {
|
|
it('should equate a promise that rejects with an Error to a string of the same message', async () => {
|
|
const message = 'foo';
|
|
const revert1 = new Error(message);
|
|
const promise = (async () => {
|
|
throw revert1;
|
|
})();
|
|
return expect(promise).to.rejectedWith(message);
|
|
});
|
|
it('should equate a promise that rejects with an StringRevertErrors to a string of the same message', async () => {
|
|
const message = 'foo';
|
|
const revert = new StringRevertError(message);
|
|
const promise = (async () => {
|
|
throw revert;
|
|
})();
|
|
return expect(promise).to.rejectedWith(message);
|
|
});
|
|
it('should not equate a promise that rejects with an StringRevertErrors to a string with different messages', async () => {
|
|
const revert = new StringRevertError('foo1');
|
|
const promise = (async () => {
|
|
throw revert;
|
|
})();
|
|
return expect(promise).to.not.be.rejectedWith('foo2');
|
|
});
|
|
});
|
|
});
|
|
});
|