aboutsummaryrefslogtreecommitdiffstats
path: root/packages/dev-utils/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/dev-utils/src')
-rw-r--r--packages/dev-utils/src/callback_error_reporter.ts66
-rw-r--r--packages/dev-utils/src/index.ts1
2 files changed, 67 insertions, 0 deletions
diff --git a/packages/dev-utils/src/callback_error_reporter.ts b/packages/dev-utils/src/callback_error_reporter.ts
new file mode 100644
index 000000000..874343ccc
--- /dev/null
+++ b/packages/dev-utils/src/callback_error_reporter.ts
@@ -0,0 +1,66 @@
+import * as chai from 'chai';
+import * as _ from 'lodash';
+
+import { DoneCallback } from '@0xproject/types';
+
+const expect = chai.expect;
+
+export const callbackErrorReporter = {
+ reportNoErrorCallbackErrors(done: DoneCallback, expectToBeCalledOnce = true) {
+ return <T>(f?: (value: T) => void) => {
+ const wrapped = (value: T) => {
+ if (_.isUndefined(f)) {
+ done();
+ return;
+ }
+ try {
+ f(value);
+ if (expectToBeCalledOnce) {
+ done();
+ }
+ } catch (err) {
+ done(err);
+ }
+ };
+ return wrapped;
+ };
+ },
+ reportNodeCallbackErrors(done: DoneCallback, expectToBeCalledOnce = true) {
+ return <T>(f?: (value: T) => void) => {
+ const wrapped = (error: Error | null, value: T | undefined) => {
+ if (!_.isNull(error)) {
+ done(error);
+ } else {
+ if (_.isUndefined(f)) {
+ done();
+ return;
+ }
+ try {
+ f(value as T);
+ if (expectToBeCalledOnce) {
+ done();
+ }
+ } catch (err) {
+ done(err);
+ }
+ }
+ };
+ return wrapped;
+ };
+ },
+ assertNodeCallbackError(done: DoneCallback, errMsg: string) {
+ const wrapped = <T>(error: Error | null, value: T | undefined) => {
+ if (_.isNull(error)) {
+ done(new Error('Expected callback to receive an error'));
+ } else {
+ try {
+ expect(error.message).to.be.equal(errMsg);
+ done();
+ } catch (err) {
+ done(err);
+ }
+ }
+ };
+ return wrapped;
+ },
+};
diff --git a/packages/dev-utils/src/index.ts b/packages/dev-utils/src/index.ts
index 2ff2a2238..9124f3e28 100644
--- a/packages/dev-utils/src/index.ts
+++ b/packages/dev-utils/src/index.ts
@@ -3,3 +3,4 @@ export { web3Factory } from './web3_factory';
export { constants as devConstants } from './constants';
export { coverage } from './coverage';
export { env, EnvVars } from './env';
+export { callbackErrorReporter } from './callback_error_reporter';