diff options
Diffstat (limited to 'packages/dev-utils')
-rw-r--r-- | packages/dev-utils/CHANGELOG.json | 9 | ||||
-rw-r--r-- | packages/dev-utils/package.json | 2 | ||||
-rw-r--r-- | packages/dev-utils/src/callback_error_reporter.ts | 68 | ||||
-rw-r--r-- | packages/dev-utils/src/index.ts | 1 |
4 files changed, 79 insertions, 1 deletions
diff --git a/packages/dev-utils/CHANGELOG.json b/packages/dev-utils/CHANGELOG.json index 9da323f48..3f70a93cc 100644 --- a/packages/dev-utils/CHANGELOG.json +++ b/packages/dev-utils/CHANGELOG.json @@ -1,5 +1,14 @@ [ { + "version": "0.4.2", + "changes": [ + { + "note": "Move callbackErrorReporter over from 0x.js", + "pr": 579 + } + ] + }, + { "timestamp": 1525477860, "version": "0.4.1", "changes": [ diff --git a/packages/dev-utils/package.json b/packages/dev-utils/package.json index 0422794ff..67b448024 100644 --- a/packages/dev-utils/package.json +++ b/packages/dev-utils/package.json @@ -13,7 +13,7 @@ "test:coverage": "nyc npm run test --all && yarn coverage:report:lcov", "coverage:report:lcov": "nyc report --reporter=text-lcov > coverage/lcov.info", "clean": "shx rm -rf lib scripts", - "lint": "tslint --project . 'src/**/*.ts' 'test/**/*.ts'", + "lint": "tslint --project .", "manual:postpublish": "yarn build; node ./scripts/postpublish.js" }, "license": "Apache-2.0", 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..c8e693673 --- /dev/null +++ b/packages/dev-utils/src/callback_error_reporter.ts @@ -0,0 +1,68 @@ +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) { + const callback = <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; + }; + return callback; + }, + reportNodeCallbackErrors(done: DoneCallback, expectToBeCalledOnce = true) { + const callback = <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; + }; + return callback; + }, + 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'; |