From 0335b677348c2ba253c114e8b0828bcc67819c40 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Mon, 13 May 2024 20:31:42 +0100 Subject: [PATCH] chore: add prettier (#286) Adds prettier and the associated formatting changes. This also updates the editor config to match that of chai itself. --- .editorconfig | 3 - .prettierrc.json | 10 + lib/chai-as-promised.js | 720 ++++++------ package-lock.json | 18 +- package.json | 6 +- test/assert-eventually.js | 253 +++-- test/assert-promise-specific.js | 523 ++++----- test/chainable-methods-compat.js | 52 +- test/configurable-asserter-args.js | 85 +- test/configurable-promiseness-transfer.js | 34 +- test/custom-messages.js | 68 +- test/proxy-guard.js | 149 +-- test/should-eventually.js | 421 +++---- test/should-promise-specific.js | 1247 +++++++++++---------- test/support/common.js | 90 +- test/support/setup.js | 12 +- 16 files changed, 1973 insertions(+), 1718 deletions(-) create mode 100755 .prettierrc.json diff --git a/.editorconfig b/.editorconfig index dfad077..4960585 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,7 +6,4 @@ insert_final_newline = true trim_trailing_whitespace = true charset = utf-8 indent_style = space -indent_size = 4 - -[.eslintrc.json] indent_size = 2 diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100755 index 0000000..fba4260 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,10 @@ +{ + "bracketSpacing": false, + "printWidth": 80, + "semi": true, + "singleQuote": true, + "tabWidth": 2, + "trailingComma": "none", + "useTabs": false, + "arrowParens": "always" +} diff --git a/lib/chai-as-promised.js b/lib/chai-as-promised.js index 5600ab9..c0dd173 100644 --- a/lib/chai-as-promised.js +++ b/lib/chai-as-promised.js @@ -1,364 +1,422 @@ -"use strict"; +'use strict'; /* eslint-disable no-invalid-this */ -let checkError = require("check-error"); +let checkError = require('check-error'); module.exports = (chai, utils) => { - const Assertion = chai.Assertion; - const assert = chai.assert; - const proxify = utils.proxify; - - // If we are using a version of Chai that has checkError on it, - // we want to use that version to be consistent. Otherwise, we use - // what was passed to the factory. - if (utils.checkError) { - checkError = utils.checkError; + const Assertion = chai.Assertion; + const assert = chai.assert; + const proxify = utils.proxify; + + // If we are using a version of Chai that has checkError on it, + // we want to use that version to be consistent. Otherwise, we use + // what was passed to the factory. + if (utils.checkError) { + checkError = utils.checkError; + } + + function isLegacyJQueryPromise(thenable) { + // jQuery promises are Promises/A+-compatible since 3.0.0. jQuery 3.0.0 is also the first version + // to define the catch method. + return ( + typeof thenable.catch !== 'function' && + typeof thenable.always === 'function' && + typeof thenable.done === 'function' && + typeof thenable.fail === 'function' && + typeof thenable.pipe === 'function' && + typeof thenable.progress === 'function' && + typeof thenable.state === 'function' + ); + } + + function assertIsAboutPromise(assertion) { + if (typeof assertion._obj.then !== 'function') { + throw new TypeError( + utils.inspect(assertion._obj) + ' is not a thenable.' + ); } - - function isLegacyJQueryPromise(thenable) { - // jQuery promises are Promises/A+-compatible since 3.0.0. jQuery 3.0.0 is also the first version - // to define the catch method. - return typeof thenable.catch !== "function" && - typeof thenable.always === "function" && - typeof thenable.done === "function" && - typeof thenable.fail === "function" && - typeof thenable.pipe === "function" && - typeof thenable.progress === "function" && - typeof thenable.state === "function"; - } - - function assertIsAboutPromise(assertion) { - if (typeof assertion._obj.then !== "function") { - throw new TypeError(utils.inspect(assertion._obj) + " is not a thenable."); - } - if (isLegacyJQueryPromise(assertion._obj)) { - throw new TypeError("Chai as Promised is incompatible with thenables of jQuery<3.0.0, sorry! Please " + - "upgrade jQuery or use another Promises/A+ compatible library (see " + - "http://promisesaplus.com/)."); - } + if (isLegacyJQueryPromise(assertion._obj)) { + throw new TypeError( + 'Chai as Promised is incompatible with thenables of jQuery<3.0.0, sorry! Please ' + + 'upgrade jQuery or use another Promises/A+ compatible library (see ' + + 'http://promisesaplus.com/).' + ); } + } - function proxifyIfSupported(assertion) { - return proxify === undefined ? assertion : proxify(assertion); - } - - function method(name, asserter) { - utils.addMethod(Assertion.prototype, name, function () { - assertIsAboutPromise(this); - return asserter.apply(this, arguments); - }); - } - - function property(name, asserter) { - utils.addProperty(Assertion.prototype, name, function () { - assertIsAboutPromise(this); - return proxifyIfSupported(asserter.apply(this, arguments)); - }); - } + function proxifyIfSupported(assertion) { + return proxify === undefined ? assertion : proxify(assertion); + } - function doNotify(promise, done) { - promise.then(() => done(), done); - } + function method(name, asserter) { + utils.addMethod(Assertion.prototype, name, function () { + assertIsAboutPromise(this); + return asserter.apply(this, arguments); + }); + } - // These are for clarity and to bypass Chai refusing to allow `undefined` as actual when used with `assert`. - function assertIfNegated(assertion, message, extra) { - assertion.assert(true, null, message, extra.expected, extra.actual); - } + function property(name, asserter) { + utils.addProperty(Assertion.prototype, name, function () { + assertIsAboutPromise(this); + return proxifyIfSupported(asserter.apply(this, arguments)); + }); + } + + function doNotify(promise, done) { + promise.then(() => done(), done); + } + + // These are for clarity and to bypass Chai refusing to allow `undefined` as actual when used with `assert`. + function assertIfNegated(assertion, message, extra) { + assertion.assert(true, null, message, extra.expected, extra.actual); + } + + function assertIfNotNegated(assertion, message, extra) { + assertion.assert(false, message, null, extra.expected, extra.actual); + } + + function getBasePromise(assertion) { + // We need to chain subsequent asserters on top of ones in the chain already (consider + // `eventually.have.property("foo").that.equals("bar")`), only running them after the existing ones pass. + // So the first base-promise is `assertion._obj`, but after that we use the assertions themselves, i.e. + // previously derived promises, to chain off of. + return typeof assertion.then === 'function' ? assertion : assertion._obj; + } + + function getReasonName(reason) { + return reason instanceof Error + ? reason.toString() + : checkError.getConstructorName(reason); + } + + // Grab these first, before we modify `Assertion.prototype`. + + const propertyNames = Object.getOwnPropertyNames(Assertion.prototype); + + const propertyDescs = {}; + for (const name of propertyNames) { + propertyDescs[name] = Object.getOwnPropertyDescriptor( + Assertion.prototype, + name + ); + } + + property('fulfilled', function () { + const derivedPromise = getBasePromise(this).then( + (value) => { + assertIfNegated( + this, + 'expected promise not to be fulfilled but it was fulfilled with #{act}', + {actual: value} + ); + return value; + }, + (reason) => { + assertIfNotNegated( + this, + 'expected promise to be fulfilled but it was rejected with #{act}', + {actual: getReasonName(reason)} + ); + return reason; + } + ); + + module.exports.transferPromiseness(this, derivedPromise); + return this; + }); + + property('rejected', function () { + const derivedPromise = getBasePromise(this).then( + (value) => { + assertIfNotNegated( + this, + 'expected promise to be rejected but it was fulfilled with #{act}', + {actual: value} + ); + return value; + }, + (reason) => { + assertIfNegated( + this, + 'expected promise not to be rejected but it was rejected with #{act}', + {actual: getReasonName(reason)} + ); - function assertIfNotNegated(assertion, message, extra) { - assertion.assert(false, message, null, extra.expected, extra.actual); + // Return the reason, transforming this into a fulfillment, to allow further assertions, e.g. + // `promise.should.be.rejected.and.eventually.equal("reason")`. + return reason; + } + ); + + module.exports.transferPromiseness(this, derivedPromise); + return this; + }); + + method('rejectedWith', function (errorLike, errMsgMatcher, message) { + let errorLikeName = null; + const negate = utils.flag(this, 'negate') || false; + + // rejectedWith with that is called without arguments is + // the same as a plain ".rejected" use. + if ( + errorLike === undefined && + errMsgMatcher === undefined && + message === undefined + ) { + /* eslint-disable no-unused-expressions */ + return this.rejected; + /* eslint-enable no-unused-expressions */ } - function getBasePromise(assertion) { - // We need to chain subsequent asserters on top of ones in the chain already (consider - // `eventually.have.property("foo").that.equals("bar")`), only running them after the existing ones pass. - // So the first base-promise is `assertion._obj`, but after that we use the assertions themselves, i.e. - // previously derived promises, to chain off of. - return typeof assertion.then === "function" ? assertion : assertion._obj; + if (message !== undefined) { + utils.flag(this, 'message', message); } - function getReasonName(reason) { - return reason instanceof Error ? reason.toString() : checkError.getConstructorName(reason); + if (errorLike instanceof RegExp || typeof errorLike === 'string') { + errMsgMatcher = errorLike; + errorLike = null; + } else if (errorLike && errorLike instanceof Error) { + errorLikeName = errorLike.toString(); + } else if (typeof errorLike === 'function') { + errorLikeName = checkError.getConstructorName(errorLike); + } else { + errorLike = null; } + const everyArgIsDefined = Boolean(errorLike && errMsgMatcher); - // Grab these first, before we modify `Assertion.prototype`. - - const propertyNames = Object.getOwnPropertyNames(Assertion.prototype); - - const propertyDescs = {}; - for (const name of propertyNames) { - propertyDescs[name] = Object.getOwnPropertyDescriptor(Assertion.prototype, name); + let matcherRelation = 'including'; + if (errMsgMatcher instanceof RegExp) { + matcherRelation = 'matching'; } - property("fulfilled", function () { - const derivedPromise = getBasePromise(this).then( - value => { - assertIfNegated(this, - "expected promise not to be fulfilled but it was fulfilled with #{act}", - { actual: value }); - return value; - }, - reason => { - assertIfNotNegated(this, - "expected promise to be fulfilled but it was rejected with #{act}", - { actual: getReasonName(reason) }); - return reason; - } - ); - - module.exports.transferPromiseness(this, derivedPromise); - return this; - }); - - property("rejected", function () { - const derivedPromise = getBasePromise(this).then( - value => { - assertIfNotNegated(this, - "expected promise to be rejected but it was fulfilled with #{act}", - { actual: value }); - return value; - }, - reason => { - assertIfNegated(this, - "expected promise not to be rejected but it was rejected with #{act}", - { actual: getReasonName(reason) }); - - // Return the reason, transforming this into a fulfillment, to allow further assertions, e.g. - // `promise.should.be.rejected.and.eventually.equal("reason")`. - return reason; - } - ); - - module.exports.transferPromiseness(this, derivedPromise); - return this; - }); - - method("rejectedWith", function (errorLike, errMsgMatcher, message) { - let errorLikeName = null; - const negate = utils.flag(this, "negate") || false; - - // rejectedWith with that is called without arguments is - // the same as a plain ".rejected" use. - if (errorLike === undefined && errMsgMatcher === undefined && - message === undefined) { - /* eslint-disable no-unused-expressions */ - return this.rejected; - /* eslint-enable no-unused-expressions */ + const derivedPromise = getBasePromise(this).then( + (value) => { + let assertionMessage = null; + let expected = null; + + if (errorLike) { + assertionMessage = + 'expected promise to be rejected with #{exp} but it was fulfilled with #{act}'; + expected = errorLikeName; + } else if (errMsgMatcher) { + assertionMessage = + `expected promise to be rejected with an error ${matcherRelation} #{exp} but ` + + `it was fulfilled with #{act}`; + expected = errMsgMatcher; } - if (message !== undefined) { - utils.flag(this, "message", message); - } - - if (errorLike instanceof RegExp || typeof errorLike === "string") { - errMsgMatcher = errorLike; - errorLike = null; - } else if (errorLike && errorLike instanceof Error) { - errorLikeName = errorLike.toString(); - } else if (typeof errorLike === "function") { - errorLikeName = checkError.getConstructorName(errorLike); + assertIfNotNegated(this, assertionMessage, {expected, actual: value}); + return value; + }, + (reason) => { + const errorLikeCompatible = + errorLike && + (errorLike instanceof Error + ? checkError.compatibleInstance(reason, errorLike) + : checkError.compatibleConstructor(reason, errorLike)); + + const errMsgMatcherCompatible = + errMsgMatcher && checkError.compatibleMessage(reason, errMsgMatcher); + + const reasonName = getReasonName(reason); + + if (negate && everyArgIsDefined) { + if (errorLikeCompatible && errMsgMatcherCompatible) { + this.assert( + true, + null, + 'expected promise not to be rejected with #{exp} but it was rejected ' + + 'with #{act}', + errorLikeName, + reasonName + ); + } } else { - errorLike = null; + if (errorLike) { + this.assert( + errorLikeCompatible, + 'expected promise to be rejected with #{exp} but it was rejected with #{act}', + 'expected promise not to be rejected with #{exp} but it was rejected ' + + 'with #{act}', + errorLikeName, + reasonName + ); + } + + if (errMsgMatcher) { + this.assert( + errMsgMatcherCompatible, + `expected promise to be rejected with an error ${matcherRelation} #{exp} but got ` + + `#{act}`, + `expected promise not to be rejected with an error ${matcherRelation} #{exp}`, + errMsgMatcher, + checkError.getMessage(reason) + ); + } } - const everyArgIsDefined = Boolean(errorLike && errMsgMatcher); - let matcherRelation = "including"; - if (errMsgMatcher instanceof RegExp) { - matcherRelation = "matching"; + return reason; + } + ); + + module.exports.transferPromiseness(this, derivedPromise); + return this; + }); + + property('eventually', function () { + utils.flag(this, 'eventually', true); + return this; + }); + + method('notify', function (done) { + doNotify(getBasePromise(this), done); + return this; + }); + + method('become', function (value, message) { + return this.eventually.deep.equal(value, message); + }); + + // ### `eventually` + + // We need to be careful not to trigger any getters, thus `Object.getOwnPropertyDescriptor` usage. + const methodNames = propertyNames.filter((name) => { + return name !== 'assert' && typeof propertyDescs[name].value === 'function'; + }); + + methodNames.forEach((methodName) => { + Assertion.overwriteMethod( + methodName, + (originalMethod) => + function () { + return doAsserterAsyncAndAddThen(originalMethod, this, arguments); } - - const derivedPromise = getBasePromise(this).then( - value => { - let assertionMessage = null; - let expected = null; - - if (errorLike) { - assertionMessage = "expected promise to be rejected with #{exp} but it was fulfilled with #{act}"; - expected = errorLikeName; - } else if (errMsgMatcher) { - assertionMessage = `expected promise to be rejected with an error ${matcherRelation} #{exp} but ` + - `it was fulfilled with #{act}`; - expected = errMsgMatcher; - } - - assertIfNotNegated(this, assertionMessage, { expected, actual: value }); - return value; - }, - reason => { - const errorLikeCompatible = errorLike && (errorLike instanceof Error ? - checkError.compatibleInstance(reason, errorLike) : - checkError.compatibleConstructor(reason, errorLike)); - - const errMsgMatcherCompatible = errMsgMatcher && checkError.compatibleMessage(reason, errMsgMatcher); - - const reasonName = getReasonName(reason); - - if (negate && everyArgIsDefined) { - if (errorLikeCompatible && errMsgMatcherCompatible) { - this.assert(true, - null, - "expected promise not to be rejected with #{exp} but it was rejected " + - "with #{act}", - errorLikeName, - reasonName); - } - } else { - if (errorLike) { - this.assert(errorLikeCompatible, - "expected promise to be rejected with #{exp} but it was rejected with #{act}", - "expected promise not to be rejected with #{exp} but it was rejected " + - "with #{act}", - errorLikeName, - reasonName); - } - - if (errMsgMatcher) { - this.assert(errMsgMatcherCompatible, - `expected promise to be rejected with an error ${matcherRelation} #{exp} but got ` + - `#{act}`, - `expected promise not to be rejected with an error ${matcherRelation} #{exp}`, - errMsgMatcher, - checkError.getMessage(reason)); - } - } - - return reason; - } - ); - - module.exports.transferPromiseness(this, derivedPromise); - return this; - }); - - property("eventually", function () { - utils.flag(this, "eventually", true); - return this; - }); - - method("notify", function (done) { - doNotify(getBasePromise(this), done); - return this; - }); - - method("become", function (value, message) { - return this.eventually.deep.equal(value, message); - }); - - // ### `eventually` - - // We need to be careful not to trigger any getters, thus `Object.getOwnPropertyDescriptor` usage. - const methodNames = propertyNames.filter(name => { - return name !== "assert" && typeof propertyDescs[name].value === "function"; - }); - - methodNames.forEach(methodName => { - Assertion.overwriteMethod(methodName, originalMethod => function () { + ); + }); + + const getterNames = propertyNames.filter((name) => { + return name !== '_obj' && typeof propertyDescs[name].get === 'function'; + }); + + getterNames.forEach((getterName) => { + // Chainable methods are things like `an`, which can work both for `.should.be.an.instanceOf` and as + // `should.be.an("object")`. We need to handle those specially. + const isChainableMethod = Object.prototype.hasOwnProperty.call( + Assertion.prototype.__methods, + getterName + ); + + if (isChainableMethod) { + Assertion.overwriteChainableMethod( + getterName, + (originalMethod) => + function () { return doAsserterAsyncAndAddThen(originalMethod, this, arguments); - }); - }); - - const getterNames = propertyNames.filter(name => { - return name !== "_obj" && typeof propertyDescs[name].get === "function"; - }); - - getterNames.forEach(getterName => { - // Chainable methods are things like `an`, which can work both for `.should.be.an.instanceOf` and as - // `should.be.an("object")`. We need to handle those specially. - const isChainableMethod = Object.prototype.hasOwnProperty.call( - Assertion.prototype.__methods, - getterName - ); - - if (isChainableMethod) { - Assertion.overwriteChainableMethod( - getterName, - originalMethod => function () { - return doAsserterAsyncAndAddThen(originalMethod, this, arguments); - }, - originalGetter => function () { - return doAsserterAsyncAndAddThen(originalGetter, this); - } + }, + (originalGetter) => + function () { + return doAsserterAsyncAndAddThen(originalGetter, this); + } + ); + } else { + Assertion.overwriteProperty( + getterName, + (originalGetter) => + function () { + return proxifyIfSupported( + doAsserterAsyncAndAddThen(originalGetter, this) ); - } else { - Assertion.overwriteProperty(getterName, originalGetter => function () { - return proxifyIfSupported(doAsserterAsyncAndAddThen(originalGetter, this)); - }); - } - }); - - function doAsserterAsyncAndAddThen(asserter, assertion, args) { - // Since we're intercepting all methods/properties, we need to just pass through if they don't want - // `eventually`, or if we've already fulfilled the promise (see below). - if (!utils.flag(assertion, "eventually")) { - asserter.apply(assertion, args); - return assertion; - } - - const derivedPromise = getBasePromise(assertion).then(value => { - // Set up the environment for the asserter to actually run: `_obj` should be the fulfillment value, and - // now that we have the value, we're no longer in "eventually" mode, so we won't run any of this code, - // just the base Chai code that we get to via the short-circuit above. - assertion._obj = value; - utils.flag(assertion, "eventually", false); - - return args ? module.exports.transformAsserterArgs(args) : args; - }).then(newArgs => { - asserter.apply(assertion, newArgs); - - // Because asserters, for example `property`, can change the value of `_obj` (i.e. change the "object" - // flag), we need to communicate this value change to subsequent chained asserters. Since we build a - // promise chain paralleling the asserter chain, we can use it to communicate such changes. - return assertion._obj; - }); - - module.exports.transferPromiseness(assertion, derivedPromise); - return assertion; + } + ); + } + }); + + function doAsserterAsyncAndAddThen(asserter, assertion, args) { + // Since we're intercepting all methods/properties, we need to just pass through if they don't want + // `eventually`, or if we've already fulfilled the promise (see below). + if (!utils.flag(assertion, 'eventually')) { + asserter.apply(assertion, args); + return assertion; } - // ### Now use the `Assertion` framework to build an `assert` interface. - const originalAssertMethods = Object.getOwnPropertyNames(assert).filter(propName => { - return typeof assert[propName] === "function"; - }); - - assert.isFulfilled = (promise, message) => (new Assertion(promise, message)).to.be.fulfilled; - - assert.isRejected = (promise, errorLike, errMsgMatcher, message) => { - const assertion = new Assertion(promise, message); - return assertion.to.be.rejectedWith(errorLike, errMsgMatcher, message); - }; - - assert.becomes = (promise, value, message) => assert.eventually.deepEqual(promise, value, message); - - assert.doesNotBecome = (promise, value, message) => assert.eventually.notDeepEqual(promise, value, message); - - assert.eventually = {}; - originalAssertMethods.forEach(assertMethodName => { - assert.eventually[assertMethodName] = function (promise) { - const otherArgs = Array.prototype.slice.call(arguments, 1); - - let customRejectionHandler; - const message = arguments[assert[assertMethodName].length - 1]; - if (typeof message === "string") { - customRejectionHandler = reason => { - throw new chai.AssertionError(`${message}\n\nOriginal reason: ${utils.inspect(reason)}`); - }; - } - - const returnedPromise = promise.then( - fulfillmentValue => assert[assertMethodName].apply(assert, [fulfillmentValue].concat(otherArgs)), - customRejectionHandler - ); - - returnedPromise.notify = done => { - doNotify(returnedPromise, done); - }; - - return returnedPromise; + const derivedPromise = getBasePromise(assertion) + .then((value) => { + // Set up the environment for the asserter to actually run: `_obj` should be the fulfillment value, and + // now that we have the value, we're no longer in "eventually" mode, so we won't run any of this code, + // just the base Chai code that we get to via the short-circuit above. + assertion._obj = value; + utils.flag(assertion, 'eventually', false); + + return args ? module.exports.transformAsserterArgs(args) : args; + }) + .then((newArgs) => { + asserter.apply(assertion, newArgs); + + // Because asserters, for example `property`, can change the value of `_obj` (i.e. change the "object" + // flag), we need to communicate this value change to subsequent chained asserters. Since we build a + // promise chain paralleling the asserter chain, we can use it to communicate such changes. + return assertion._obj; + }); + + module.exports.transferPromiseness(assertion, derivedPromise); + return assertion; + } + + // ### Now use the `Assertion` framework to build an `assert` interface. + const originalAssertMethods = Object.getOwnPropertyNames(assert).filter( + (propName) => { + return typeof assert[propName] === 'function'; + } + ); + + assert.isFulfilled = (promise, message) => + new Assertion(promise, message).to.be.fulfilled; + + assert.isRejected = (promise, errorLike, errMsgMatcher, message) => { + const assertion = new Assertion(promise, message); + return assertion.to.be.rejectedWith(errorLike, errMsgMatcher, message); + }; + + assert.becomes = (promise, value, message) => + assert.eventually.deepEqual(promise, value, message); + + assert.doesNotBecome = (promise, value, message) => + assert.eventually.notDeepEqual(promise, value, message); + + assert.eventually = {}; + originalAssertMethods.forEach((assertMethodName) => { + assert.eventually[assertMethodName] = function (promise) { + const otherArgs = Array.prototype.slice.call(arguments, 1); + + let customRejectionHandler; + const message = arguments[assert[assertMethodName].length - 1]; + if (typeof message === 'string') { + customRejectionHandler = (reason) => { + throw new chai.AssertionError( + `${message}\n\nOriginal reason: ${utils.inspect(reason)}` + ); }; - }); + } + + const returnedPromise = promise.then( + (fulfillmentValue) => + assert[assertMethodName].apply( + assert, + [fulfillmentValue].concat(otherArgs) + ), + customRejectionHandler + ); + + returnedPromise.notify = (done) => { + doNotify(returnedPromise, done); + }; + + return returnedPromise; + }; + }); }; module.exports.transferPromiseness = (assertion, promise) => { - assertion.then = promise.then.bind(promise); + assertion.then = promise.then.bind(promise); }; -module.exports.transformAsserterArgs = values => values; +module.exports.transformAsserterArgs = (values) => values; diff --git a/package-lock.json b/package-lock.json index 1250a11..d312e77 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,8 @@ "c8": "^9.1.0", "chai": "^4.4.1", "eslint": "^8.57.0", - "mocha": "^10.4.0" + "mocha": "^10.4.0", + "prettier": "^3.2.5" }, "peerDependencies": { "chai": ">= 2.1.2 < 6" @@ -1640,6 +1641,21 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, + "node_modules/prettier": { + "version": "3.2.5", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.2.5.tgz", + "integrity": "sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A==", + "dev": true, + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/punycode": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", diff --git a/package.json b/package.json index 39270a1..36c3624 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ ], "scripts": { "test": "c8 mocha", - "lint": "eslint ." + "lint": "eslint .", + "format": "prettier --write lib test" }, "dependencies": { "check-error": "^1.0.2" @@ -33,6 +34,7 @@ "c8": "^9.1.0", "chai": "^4.4.1", "eslint": "^8.57.0", - "mocha": "^10.4.0" + "mocha": "^10.4.0", + "prettier": "^3.2.5" } } diff --git a/test/assert-eventually.js b/test/assert-eventually.js index 8749dd7..1c243a5 100644 --- a/test/assert-eventually.js +++ b/test/assert-eventually.js @@ -1,139 +1,152 @@ -"use strict"; -require("./support/setup.js"); -const shouldPass = require("./support/common.js").shouldPass; -const shouldFail = require("./support/common.js").shouldFail; -const assert = require("chai").assert; -const expect = require("chai").expect; +'use strict'; +require('./support/setup.js'); +const shouldPass = require('./support/common.js').shouldPass; +const shouldFail = require('./support/common.js').shouldFail; +const assert = require('chai').assert; +const expect = require('chai').expect; -describe("Assert interface with eventually extender:", () => { - let promise = null; +describe('Assert interface with eventually extender:', () => { + let promise = null; - describe("Direct tests of fulfilled promises", () => { - it(".eventually.isNull(promise)", done => { - assert.eventually.isNull(Promise.resolve(null)).notify(done); - }); - it(".eventually.isFunction(promise)", done => { - assert.eventually.isFunction(Promise.resolve(() => { /* Forever pending */ })).notify(done); - }); - it(".eventually.typeOf(promise, 'string')", done => { - assert.eventually.typeOf(Promise.resolve("hello"), "string").notify(done); - }); - it(".eventually.include(promiseForString, 'substring')", done => { - assert.eventually.include(Promise.resolve("hello"), "hell").notify(done); - }); - it(".eventually.include(promiseForArray, arrayMember)", done => { - assert.eventually.include(Promise.resolve([1, 2, 3]), 1).notify(done); - }); + describe('Direct tests of fulfilled promises', () => { + it('.eventually.isNull(promise)', (done) => { + assert.eventually.isNull(Promise.resolve(null)).notify(done); }); + it('.eventually.isFunction(promise)', (done) => { + assert.eventually + .isFunction( + Promise.resolve(() => { + /* Forever pending */ + }) + ) + .notify(done); + }); + it(".eventually.typeOf(promise, 'string')", (done) => { + assert.eventually.typeOf(Promise.resolve('hello'), 'string').notify(done); + }); + it(".eventually.include(promiseForString, 'substring')", (done) => { + assert.eventually.include(Promise.resolve('hello'), 'hell').notify(done); + }); + it('.eventually.include(promiseForArray, arrayMember)', (done) => { + assert.eventually.include(Promise.resolve([1, 2, 3]), 1).notify(done); + }); + }); - describe("On a promise fulfilled with the number 42", () => { - beforeEach(() => { - promise = Promise.resolve(42); - }); + describe('On a promise fulfilled with the number 42', () => { + beforeEach(() => { + promise = Promise.resolve(42); + }); - describe(".eventually.isNull(promise)", () => { - shouldFail({ - op: () => assert.eventually.isNull(promise), - message: "to equal null" - }); - }); - describe(".eventually.isDefined(promise)", () => { - shouldPass(() => assert.eventually.isDefined(promise)); - }); - describe(".eventually.ok(promise)", () => { - shouldPass(() => assert.eventually.ok(promise)); - }); - describe(".eventually.equal(promise, 42)", () => { - shouldPass(() => assert.eventually.equal(promise, 42)); - }); - describe(".eventually.equal(promise, 52)", () => { - shouldFail({ - op: () => assert.eventually.equal(promise, 52), - message: "to equal 52" - }); + describe('.eventually.isNull(promise)', () => { + shouldFail({ + op: () => assert.eventually.isNull(promise), + message: 'to equal null' + }); + }); + describe('.eventually.isDefined(promise)', () => { + shouldPass(() => assert.eventually.isDefined(promise)); + }); + describe('.eventually.ok(promise)', () => { + shouldPass(() => assert.eventually.ok(promise)); + }); + describe('.eventually.equal(promise, 42)', () => { + shouldPass(() => assert.eventually.equal(promise, 42)); + }); + describe('.eventually.equal(promise, 52)', () => { + shouldFail({ + op: () => assert.eventually.equal(promise, 52), + message: 'to equal 52' + }); - function shouldFailWithCorrectActual(promiseProducer) { - it("should return a promise rejected with an assertion error that has actual/expected properties " + - "correct", done => { - expect(promiseProducer().then( - () => { - throw new Error("promise fulfilled"); - }, - e => { - e.actual.should.equal(42); - e.expected.should.equal(52); - } - )).to.be.fulfilled.notify(done); - }); - } + function shouldFailWithCorrectActual(promiseProducer) { + it( + 'should return a promise rejected with an assertion error that has actual/expected properties ' + + 'correct', + (done) => { + expect( + promiseProducer().then( + () => { + throw new Error('promise fulfilled'); + }, + (e) => { + e.actual.should.equal(42); + e.expected.should.equal(52); + } + ) + ).to.be.fulfilled.notify(done); + } + ); + } - describe("assert", () => { - shouldFailWithCorrectActual(() => assert.eventually.equal(promise, 52)); - }); - describe("expect", () => { - shouldFailWithCorrectActual(() => expect(promise).to.eventually.equal(52)); - }); - describe("should", () => { - shouldFailWithCorrectActual(() => promise.should.eventually.equal(52)); - }); - }); + describe('assert', () => { + shouldFailWithCorrectActual(() => assert.eventually.equal(promise, 52)); + }); + describe('expect', () => { + shouldFailWithCorrectActual(() => + expect(promise).to.eventually.equal(52) + ); + }); + describe('should', () => { + shouldFailWithCorrectActual(() => promise.should.eventually.equal(52)); + }); + }); - describe(".eventually.notEqual(promise, 42)", () => { - shouldFail({ - op: () => assert.eventually.notEqual(promise, 42), - message: "to not equal 42" - }); - }); - describe(".eventually.notEqual(promise, 52)", () => { - shouldPass(() => assert.eventually.notEqual(promise, 52)); - }); + describe('.eventually.notEqual(promise, 42)', () => { + shouldFail({ + op: () => assert.eventually.notEqual(promise, 42), + message: 'to not equal 42' + }); }); + describe('.eventually.notEqual(promise, 52)', () => { + shouldPass(() => assert.eventually.notEqual(promise, 52)); + }); + }); - describe("On a promise fulfilled with { foo: 'bar' }", () => { - beforeEach(() => { - promise = Promise.resolve({ foo: "bar" }); - }); + describe("On a promise fulfilled with { foo: 'bar' }", () => { + beforeEach(() => { + promise = Promise.resolve({foo: 'bar'}); + }); - describe(".eventually.equal(promise, { foo: 'bar' })", () => { - shouldFail({ - op: () => assert.eventually.equal(promise, { foo: "bar" }), - message: "to equal { foo: 'bar' }" - }); - }); - describe(".eventually.deepEqual(promise, { foo: 'bar' })", () => { - shouldPass(() => assert.eventually.deepEqual(promise, { foo: "bar" })); - }); + describe(".eventually.equal(promise, { foo: 'bar' })", () => { + shouldFail({ + op: () => assert.eventually.equal(promise, {foo: 'bar'}), + message: "to equal { foo: 'bar' }" + }); }); + describe(".eventually.deepEqual(promise, { foo: 'bar' })", () => { + shouldPass(() => assert.eventually.deepEqual(promise, {foo: 'bar'})); + }); + }); - describe("Assertion messages", () => { - const message = "He told me enough! He told me you killed him!"; + describe('Assertion messages', () => { + const message = 'He told me enough! He told me you killed him!'; - describe("should pass through for .eventually.isNull(promise, message) for fulfilled", () => { - shouldFail({ - op: () => assert.eventually.isNull(Promise.resolve(42), message), - message - }); - }); + describe('should pass through for .eventually.isNull(promise, message) for fulfilled', () => { + shouldFail({ + op: () => assert.eventually.isNull(Promise.resolve(42), message), + message + }); + }); - describe("should pass through for .eventually.isNull(promise, message) for rejected", () => { - shouldFail({ - op: () => assert.eventually.isNull(Promise.reject(), message), - message - }); - }); + describe('should pass through for .eventually.isNull(promise, message) for rejected', () => { + shouldFail({ + op: () => assert.eventually.isNull(Promise.reject(), message), + message + }); + }); - describe("should pass through for .eventually.equal(promise, 52, message) for fulfilled", () => { - shouldFail({ - op: () => assert.eventually.equal(Promise.resolve(42), 52, message), - message - }); - }); + describe('should pass through for .eventually.equal(promise, 52, message) for fulfilled', () => { + shouldFail({ + op: () => assert.eventually.equal(Promise.resolve(42), 52, message), + message + }); + }); - describe("should pass through for .eventually.equal(promise, 52, message) for rejected", () => { - shouldFail({ - op: () => assert.eventually.equal(Promise.reject(), 52, message), - message - }); - }); + describe('should pass through for .eventually.equal(promise, 52, message) for rejected', () => { + shouldFail({ + op: () => assert.eventually.equal(Promise.reject(), 52, message), + message + }); }); + }); }); diff --git a/test/assert-promise-specific.js b/test/assert-promise-specific.js index af6d641..2e1c34c 100644 --- a/test/assert-promise-specific.js +++ b/test/assert-promise-specific.js @@ -1,288 +1,295 @@ -"use strict"; -require("./support/setup.js"); -const shouldPass = require("./support/common.js").shouldPass; -const shouldFail = require("./support/common.js").shouldFail; -const assert = require("chai").assert; - -describe("Assert interface:", () => { - let promise = null; - const error = new Error("boo"); - const custom = "No. I am your father."; - - describe("when the promise is fulfilled", () => { - beforeEach(() => { - promise = Promise.resolve({ foo: "bar" }); - }); +'use strict'; +require('./support/setup.js'); +const shouldPass = require('./support/common.js').shouldPass; +const shouldFail = require('./support/common.js').shouldFail; +const assert = require('chai').assert; + +describe('Assert interface:', () => { + let promise = null; + const error = new Error('boo'); + const custom = 'No. I am your father.'; + + describe('when the promise is fulfilled', () => { + beforeEach(() => { + promise = Promise.resolve({foo: 'bar'}); + }); - describe(".isFulfilled(promise)", () => { - shouldPass(() => assert.isFulfilled(promise)); - }); + describe('.isFulfilled(promise)', () => { + shouldPass(() => assert.isFulfilled(promise)); + }); - describe(".becomes(promise, correctValue)", () => { - shouldPass(() => assert.becomes(promise, { foo: "bar" })); - }); - describe(".becomes(promise, incorrectValue)", () => { - shouldFail({ - op: () => assert.becomes(promise, { baz: "quux" }), - message: "to deeply equal { baz: 'quux' }" - }); - }); + describe('.becomes(promise, correctValue)', () => { + shouldPass(() => assert.becomes(promise, {foo: 'bar'})); + }); + describe('.becomes(promise, incorrectValue)', () => { + shouldFail({ + op: () => assert.becomes(promise, {baz: 'quux'}), + message: "to deeply equal { baz: 'quux' }" + }); + }); - describe(".becomes(promise, incorrectValue, custom)", () => { - shouldFail({ - op: () => assert.becomes(promise, { baz: "quux" }, custom), - message: custom - }); - }); + describe('.becomes(promise, incorrectValue, custom)', () => { + shouldFail({ + op: () => assert.becomes(promise, {baz: 'quux'}, custom), + message: custom + }); + }); - describe(".doesNotBecome(promise, correctValue)", () => { - shouldFail({ - op: () => assert.doesNotBecome(promise, { foo: "bar" }), - message: "to not deeply equal { foo: 'bar' }" - }); - }); - describe(".doesNotBecome(promise, incorrectValue)", () => { - shouldPass(() => assert.doesNotBecome(promise, { baz: "quux" })); - }); + describe('.doesNotBecome(promise, correctValue)', () => { + shouldFail({ + op: () => assert.doesNotBecome(promise, {foo: 'bar'}), + message: "to not deeply equal { foo: 'bar' }" + }); + }); + describe('.doesNotBecome(promise, incorrectValue)', () => { + shouldPass(() => assert.doesNotBecome(promise, {baz: 'quux'})); + }); - describe(".doesNotBecome(promise, correctValue, custom)", () => { - shouldFail({ - op: () => assert.doesNotBecome(promise, { foo: "bar" }, custom), - message: custom - }); - }); + describe('.doesNotBecome(promise, correctValue, custom)', () => { + shouldFail({ + op: () => assert.doesNotBecome(promise, {foo: 'bar'}, custom), + message: custom + }); + }); - describe(".isRejected(promise)", () => { - shouldFail({ - op: () => assert.isRejected(promise), - message: "to be rejected" - }); - }); - describe(".isRejected(promise, TypeError)", () => { - shouldFail({ - op: () => assert.isRejected(promise, TypeError), - message: "to be rejected" - }); - }); - describe(".isRejected(promise, /regexp/)", () => { - shouldFail({ - op: () => assert.isRejected(promise, /regexp/), - message: "to be rejected" - }); - }); - describe(".isRejected(promise, TypeError, /regexp/)", () => { - shouldFail({ - op: () => assert.isRejected(promise, TypeError, /regexp/), - message: "to be rejected" - }); + describe('.isRejected(promise)', () => { + shouldFail({ + op: () => assert.isRejected(promise), + message: 'to be rejected' + }); + }); + describe('.isRejected(promise, TypeError)', () => { + shouldFail({ + op: () => assert.isRejected(promise, TypeError), + message: 'to be rejected' + }); + }); + describe('.isRejected(promise, /regexp/)', () => { + shouldFail({ + op: () => assert.isRejected(promise, /regexp/), + message: 'to be rejected' + }); + }); + describe('.isRejected(promise, TypeError, /regexp/)', () => { + shouldFail({ + op: () => assert.isRejected(promise, TypeError, /regexp/), + message: 'to be rejected' + }); + }); + describe('.isRejected(promise, errorInstance)', () => { + shouldFail({ + op: () => assert.isRejected(promise, error), + message: 'to be rejected' + }); + }); + // Chai never interprets the 3rd parameter to assert.throws as + // a custom error message. This is what we are checking here. + describe('.isRejected(promise, /quux/, custom)', () => { + shouldFail({ + op: () => assert.isRejected(promise, /quux/, custom), + notMessage: custom + }); + }); + }); + + describe('when the promise is rejected', () => { + beforeEach(() => { + promise = Promise.reject(error); + }); + + describe('.isFulfilled(promise)', () => { + shouldFail({ + op: () => assert.isFulfilled(promise), + message: 'to be fulfilled' + }); + }); + + describe('.isFulfilled(promise, custom)', () => { + shouldFail({ + op: () => assert.isFulfilled(promise, custom), + message: custom + }); + }); + + describe('.isRejected(promise)', () => { + shouldPass(() => assert.isRejected(promise)); + }); + + describe('.isRejected(promise, theError)', () => { + shouldPass(() => assert.isRejected(promise, error)); + }); + + describe('.isRejected(promise, differentError)', () => { + shouldFail({ + op: () => assert.isRejected(promise, new Error()), + message: 'to be rejected with' + }); + }); + + // Chai never interprets the 3rd parameter to assert.throws as + // a custom error message. This is what we are checking here. + describe('.isRejected(promise, differentError, custom)', () => { + shouldFail({ + op: () => assert.isRejected(promise, new Error(), custom), + notMessage: custom + }); + }); + + describe("with an Error having message 'foo bar'", () => { + beforeEach(() => { + promise = Promise.reject(new Error('foo bar')); + }); + + describe(".isRejected(promise, 'bar')", () => { + shouldPass(() => assert.isRejected(promise, 'bar')); + }); + + describe(".isRejected(promise, 'bar', custom)", () => { + shouldPass(() => assert.isRejected(promise, 'bar', custom)); + }); + + describe('.isRejected(promise, /bar/)', () => { + shouldPass(() => assert.isRejected(promise, /bar/)); + }); + + describe('.isRejected(promise, /bar/, custom)', () => { + shouldPass(() => assert.isRejected(promise, /bar/, custom)); + }); + + describe(".isRejected(promise, 'quux')", () => { + shouldFail({ + op: () => assert.isRejected(promise, 'quux'), + message: 'to be rejected with' }); - describe(".isRejected(promise, errorInstance)", () => { - shouldFail({ - op: () => assert.isRejected(promise, error), - message: "to be rejected" - }); + }); + + // Chai 3.5.0 never interprets the 3rd parameter to assert.throws as + // a custom error message. This is what we are checking here. + describe(".isRejected(promise, 'quux', custom)", () => { + shouldFail({ + op: () => assert.isRejected(promise, 'quux', custom), + notMessage: custom }); - // Chai never interprets the 3rd parameter to assert.throws as - // a custom error message. This is what we are checking here. - describe(".isRejected(promise, /quux/, custom)", () => { - shouldFail({ - op: () => assert.isRejected(promise, /quux/, custom), - notMessage: custom - }); + }); + + describe('.isRejected(promise, /quux/)', () => { + shouldFail({ + op: () => assert.isRejected(promise, /quux/), + message: 'to be rejected with' }); + }); }); + describe('with a RangeError', () => { + beforeEach(() => { + promise = Promise.reject(new RangeError()); + }); - describe("when the promise is rejected", () => { - beforeEach(() => { - promise = Promise.reject(error); - }); + describe('.isRejected(promise, RangeError)', () => { + shouldPass(() => assert.isRejected(promise, RangeError)); + }); - describe(".isFulfilled(promise)", () => { - shouldFail({ - op: () => assert.isFulfilled(promise), - message: "to be fulfilled" - }); + describe('.isRejected(promise, TypeError)', () => { + shouldFail({ + op: () => assert.isRejected(promise, TypeError), + message: 'to be rejected' }); + }); + }); - describe(".isFulfilled(promise, custom)", () => { - shouldFail({ - op: () => assert.isFulfilled(promise, custom), - message: custom - }); - }); + describe("with a RangeError having a message 'foo bar'", () => { + beforeEach(() => { + promise = Promise.reject(new RangeError('foo bar')); + }); - describe(".isRejected(promise)", () => { - shouldPass(() => assert.isRejected(promise)); - }); + describe(".isRejected(promise, RangeError, 'foo')", () => { + shouldPass(() => assert.isRejected(promise, RangeError, 'foo')); + }); + + describe('.isRejected(promise, RangeError, /bar/)', () => { + shouldPass(() => assert.isRejected(promise, RangeError, /bar/)); + }); - describe(".isRejected(promise, theError)", () => { - shouldPass(() => assert.isRejected(promise, error)); + describe(".isRejected(promise, RangeError, 'quux')", () => { + shouldFail({ + op: () => assert.isRejected(promise, RangeError, 'quux'), + message: + "to be rejected with an error including 'quux' but got 'foo bar'" }); + }); - describe(".isRejected(promise, differentError)", () => { - shouldFail({ - op: () => assert.isRejected(promise, new Error()), - message: "to be rejected with" - }); + describe('.isRejected(promise, RangeError, /quux/)', () => { + shouldFail({ + op: () => assert.isRejected(promise, RangeError, /quux/), + message: + "to be rejected with an error matching /quux/ but got 'foo bar'" }); + }); - // Chai never interprets the 3rd parameter to assert.throws as - // a custom error message. This is what we are checking here. - describe(".isRejected(promise, differentError, custom)", () => { - shouldFail({ - op: () => assert.isRejected(promise, new Error(), custom), - notMessage: custom - }); + describe(".isRejected(promise, TypeError, 'foo')", () => { + shouldFail({ + op: () => assert.isRejected(promise, TypeError, 'foo'), + message: + "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'" }); + }); + describe('.isRejected(promise, TypeError, /bar/)', () => { + shouldFail({ + op: () => assert.isRejected(promise, TypeError, /bar/), + message: + "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'" + }); + }); - describe("with an Error having message 'foo bar'", () => { - beforeEach(() => { - promise = Promise.reject(new Error("foo bar")); - }); - - describe(".isRejected(promise, 'bar')", () => { - shouldPass(() => assert.isRejected(promise, "bar")); - }); - - describe(".isRejected(promise, 'bar', custom)", () => { - shouldPass(() => assert.isRejected(promise, "bar", custom)); - }); - - describe(".isRejected(promise, /bar/)", () => { - shouldPass(() => assert.isRejected(promise, /bar/)); - }); - - describe(".isRejected(promise, /bar/, custom)", () => { - shouldPass(() => assert.isRejected(promise, /bar/, custom)); - }); - - describe(".isRejected(promise, 'quux')", () => { - shouldFail({ - op: () => assert.isRejected(promise, "quux"), - message: "to be rejected with" - }); - }); - - // Chai 3.5.0 never interprets the 3rd parameter to assert.throws as - // a custom error message. This is what we are checking here. - describe(".isRejected(promise, 'quux', custom)", () => { - shouldFail({ - op: () => assert.isRejected(promise, "quux", custom), - notMessage: custom - }); - }); - - describe(".isRejected(promise, /quux/)", () => { - shouldFail({ - op: () => assert.isRejected(promise, /quux/), - message: "to be rejected with" - }); - }); + describe(".isRejected(promise, TypeError, 'quux')", () => { + shouldFail({ + op: () => assert.isRejected(promise, TypeError, 'quux'), + message: + "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'" + }); + }); + describe('.isRejected(promise, TypeError, /quux/)', () => { + shouldFail({ + op: () => assert.isRejected(promise, TypeError, /quux/), + message: + "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'" }); + }); - describe("with a RangeError", () => { - beforeEach(() => { - promise = Promise.reject(new RangeError()); - }); - - describe(".isRejected(promise, RangeError)", () => { - shouldPass(() => assert.isRejected(promise, RangeError)); - }); - - describe(".isRejected(promise, TypeError)", () => { - shouldFail({ - op: () => assert.isRejected(promise, TypeError), - message: "to be rejected" - }); - }); + describe(".isRejected(promise, RangeError, 'foo', custom)", () => { + shouldPass(() => assert.isRejected(promise, RangeError, 'foo', custom)); + }); + + describe('.isRejected(promise, RangeError, /bar/, custom)', () => { + shouldPass(() => assert.isRejected(promise, RangeError, /bar/, custom)); + }); + + describe(".isRejected(promise, RangeError, 'quux', custom)", () => { + shouldFail({ + op: () => assert.isRejected(promise, RangeError, 'quux', custom), + message: custom }); + }); - describe("with a RangeError having a message 'foo bar'", () => { - beforeEach(() => { - promise = Promise.reject(new RangeError("foo bar")); - }); - - describe(".isRejected(promise, RangeError, 'foo')", () => { - shouldPass(() => assert.isRejected(promise, RangeError, "foo")); - }); - - describe(".isRejected(promise, RangeError, /bar/)", () => { - shouldPass(() => assert.isRejected(promise, RangeError, /bar/)); - }); - - describe(".isRejected(promise, RangeError, 'quux')", () => { - shouldFail({ - op: () => assert.isRejected(promise, RangeError, "quux"), - message: "to be rejected with an error including 'quux' but got 'foo bar'" - }); - }); - - describe(".isRejected(promise, RangeError, /quux/)", () => { - shouldFail({ - op: () => assert.isRejected(promise, RangeError, /quux/), - message: "to be rejected with an error matching /quux/ but got 'foo bar'" - }); - }); - - describe(".isRejected(promise, TypeError, 'foo')", () => { - shouldFail({ - op: () => assert.isRejected(promise, TypeError, "foo"), - message: "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'" - }); - }); - describe(".isRejected(promise, TypeError, /bar/)", () => { - shouldFail({ - op: () => assert.isRejected(promise, TypeError, /bar/), - message: "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'" - }); - }); - - describe(".isRejected(promise, TypeError, 'quux')", () => { - shouldFail({ - op: () => assert.isRejected(promise, TypeError, "quux"), - message: "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'" - }); - }); - describe(".isRejected(promise, TypeError, /quux/)", () => { - shouldFail({ - op: () => assert.isRejected(promise, TypeError, /quux/), - message: "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'" - }); - }); - - describe(".isRejected(promise, RangeError, 'foo', custom)", () => { - shouldPass(() => assert.isRejected(promise, RangeError, "foo", custom)); - }); - - describe(".isRejected(promise, RangeError, /bar/, custom)", () => { - shouldPass(() => assert.isRejected(promise, RangeError, /bar/, custom)); - }); - - describe(".isRejected(promise, RangeError, 'quux', custom)", () => { - shouldFail({ - op: () => assert.isRejected(promise, RangeError, "quux", custom), - message: custom - }); - }); - - describe(".isRejected(promise, RangeError, /quux/, custom)", () => { - shouldFail({ - op: () => assert.isRejected(promise, RangeError, /quux/, custom), - message: custom - }); - }); - - describe(".isRejected(promise, RangeError, undefined, custom)", () => { - shouldPass(() => assert.isRejected(promise, RangeError, undefined, custom)); - }); - - describe(".isRejected(promise, TypeError, undefined, custom)", () => { - shouldFail({ - op: () => assert.isRejected(promise, TypeError, undefined, custom), - message: custom - }); - }); + describe('.isRejected(promise, RangeError, /quux/, custom)', () => { + shouldFail({ + op: () => assert.isRejected(promise, RangeError, /quux/, custom), + message: custom + }); + }); + + describe('.isRejected(promise, RangeError, undefined, custom)', () => { + shouldPass(() => + assert.isRejected(promise, RangeError, undefined, custom) + ); + }); + + describe('.isRejected(promise, TypeError, undefined, custom)', () => { + shouldFail({ + op: () => assert.isRejected(promise, TypeError, undefined, custom), + message: custom }); + }); }); + }); }); diff --git a/test/chainable-methods-compat.js b/test/chainable-methods-compat.js index 5a7f832..e25c3ee 100644 --- a/test/chainable-methods-compat.js +++ b/test/chainable-methods-compat.js @@ -1,44 +1,44 @@ -"use strict"; -const chai = require("chai"); -const chaiAsPromised = require(".."); +'use strict'; +const chai = require('chai'); +const chaiAsPromised = require('..'); const expect = chai.expect; function newMethod() { - // Do nothing + // Do nothing } function newMethodChain() { - /* eslint-disable no-invalid-this */ - return this.assert(this._obj.__property === true); - /* eslint-enable no-invalid-this */ + /* eslint-disable no-invalid-this */ + return this.assert(this._obj.__property === true); + /* eslint-enable no-invalid-this */ } function makeFunction() { - function fn() { - // Do nothing - } - fn.__property = true; - return fn; + function fn() { + // Do nothing + } + fn.__property = true; + return fn; } -chai.use(ctx => { - ctx.Assertion.addChainableMethod("newMethod", newMethod, newMethodChain); +chai.use((ctx) => { + ctx.Assertion.addChainableMethod('newMethod', newMethod, newMethodChain); }); -describe("New method `newMethod` added to chai", () => { - describe("before executing chai.use(chaiAsPromised)", () => { - it("should work", () => { - expect(makeFunction()).to.have.been.newMethod(); - }); +describe('New method `newMethod` added to chai', () => { + describe('before executing chai.use(chaiAsPromised)', () => { + it('should work', () => { + expect(makeFunction()).to.have.been.newMethod(); }); + }); - describe("after executing chai.use(chaiAsPromised)", () => { - before(() => { - chai.use(chaiAsPromised); - }); + describe('after executing chai.use(chaiAsPromised)', () => { + before(() => { + chai.use(chaiAsPromised); + }); - it("should still work", () => { - expect(makeFunction()).to.have.been.newMethod(); - }); + it('should still work', () => { + expect(makeFunction()).to.have.been.newMethod(); }); + }); }); diff --git a/test/configurable-asserter-args.js b/test/configurable-asserter-args.js index 861e433..6fbb095 100644 --- a/test/configurable-asserter-args.js +++ b/test/configurable-asserter-args.js @@ -1,40 +1,47 @@ -"use strict"; -require("./support/setup.js"); -const chaiAsPromised = require(".."); -const originalTransformAsserterArgs = require("..").transformAsserterArgs; - -describe("Configuring the way in which asserter arguments are transformed", () => { - beforeEach(() => { - chaiAsPromised.transformAsserterArgs = Promise.all.bind(Promise); - }); - - afterEach(() => { - chaiAsPromised.transformAsserterArgs = originalTransformAsserterArgs; - }); - - it("should override transformAsserterArgs and allow to compare promises", () => { - const value = "test it"; - - return Promise.resolve(value).should.eventually.equal(Promise.resolve(value)); - }); - - it("should override transformAsserterArgs and wait until all promises are resolved", () => { - return Promise.resolve(5).should.eventually.be.within(Promise.resolve(3), Promise.resolve(6)); - }); - - it("should not invoke transformAsserterArgs for chai properties", () => { - chaiAsPromised.transformAsserterArgs = () => { - throw new Error("transformAsserterArgs should not be called for chai properties"); - }; - - return Promise.resolve(true).should.eventually.be.true; - }); - - it("should transform asserter args", () => { - chaiAsPromised.transformAsserterArgs = args => { - return Array.from(args).map(x => x + 1); - }; - - return Promise.resolve(3).should.eventually.equal(2); - }); +'use strict'; +require('./support/setup.js'); +const chaiAsPromised = require('..'); +const originalTransformAsserterArgs = require('..').transformAsserterArgs; + +describe('Configuring the way in which asserter arguments are transformed', () => { + beforeEach(() => { + chaiAsPromised.transformAsserterArgs = Promise.all.bind(Promise); + }); + + afterEach(() => { + chaiAsPromised.transformAsserterArgs = originalTransformAsserterArgs; + }); + + it('should override transformAsserterArgs and allow to compare promises', () => { + const value = 'test it'; + + return Promise.resolve(value).should.eventually.equal( + Promise.resolve(value) + ); + }); + + it('should override transformAsserterArgs and wait until all promises are resolved', () => { + return Promise.resolve(5).should.eventually.be.within( + Promise.resolve(3), + Promise.resolve(6) + ); + }); + + it('should not invoke transformAsserterArgs for chai properties', () => { + chaiAsPromised.transformAsserterArgs = () => { + throw new Error( + 'transformAsserterArgs should not be called for chai properties' + ); + }; + + return Promise.resolve(true).should.eventually.be.true; + }); + + it('should transform asserter args', () => { + chaiAsPromised.transformAsserterArgs = (args) => { + return Array.from(args).map((x) => x + 1); + }; + + return Promise.resolve(3).should.eventually.equal(2); + }); }); diff --git a/test/configurable-promiseness-transfer.js b/test/configurable-promiseness-transfer.js index df3819c..4c24e9d 100644 --- a/test/configurable-promiseness-transfer.js +++ b/test/configurable-promiseness-transfer.js @@ -1,22 +1,22 @@ -"use strict"; -require("./support/setup.js"); -const chaiAsPromised = require(".."); -const originalTransferPromiseness = require("..").transferPromiseness; +'use strict'; +require('./support/setup.js'); +const chaiAsPromised = require('..'); +const originalTransferPromiseness = require('..').transferPromiseness; -describe("Configuring the way in which promise-ness is transferred", () => { - afterEach(() => { - chaiAsPromised.transferPromiseness = originalTransferPromiseness; - }); +describe('Configuring the way in which promise-ness is transferred', () => { + afterEach(() => { + chaiAsPromised.transferPromiseness = originalTransferPromiseness; + }); - it("should return a promise with the custom modifications applied", () => { - chaiAsPromised.transferPromiseness = (assertion, promise) => { - assertion.then = promise.then.bind(promise); - assertion.isCustomized = true; - }; + it('should return a promise with the custom modifications applied', () => { + chaiAsPromised.transferPromiseness = (assertion, promise) => { + assertion.then = promise.then.bind(promise); + assertion.isCustomized = true; + }; - const promise = Promise.resolve("1234"); - const assertion = promise.should.become("1234"); + const promise = Promise.resolve('1234'); + const assertion = promise.should.become('1234'); - assertion.should.have.property("isCustomized", true); - }); + assertion.should.have.property('isCustomized', true); + }); }); diff --git a/test/custom-messages.js b/test/custom-messages.js index 67b83ad..46706d6 100644 --- a/test/custom-messages.js +++ b/test/custom-messages.js @@ -1,43 +1,43 @@ -"use strict"; -require("./support/setup.js"); -const shouldPass = require("./support/common.js").shouldPass; -const shouldFail = require("./support/common.js").shouldFail; +'use strict'; +require('./support/setup.js'); +const shouldPass = require('./support/common.js').shouldPass; +const shouldFail = require('./support/common.js').shouldFail; -describe("Custom messages", () => { - let promise = null; - const message = "He told me enough! He told me you killed him!"; +describe('Custom messages', () => { + let promise = null; + const message = 'He told me enough! He told me you killed him!'; - beforeEach(() => { - promise = Promise.resolve(42); - }); + beforeEach(() => { + promise = Promise.resolve(42); + }); - describe("should pass through for .become(value, message) for 42", () => { - shouldPass(() => promise.should.become(42, message)); - }); - describe("should pass through for .become(value, message) for 52", () => { - shouldFail({ - op: () => promise.should.become(52, message), - message - }); + describe('should pass through for .become(value, message) for 42', () => { + shouldPass(() => promise.should.become(42, message)); + }); + describe('should pass through for .become(value, message) for 52', () => { + shouldFail({ + op: () => promise.should.become(52, message), + message }); + }); - describe("should pass through for .not.become(42, message)", () => { - shouldFail({ - op: () => promise.should.not.become(42, message), - message - }); - }); - describe("should pass through for .not.become(52, message)", () => { - shouldPass(() => promise.should.not.become(52, message)); + describe('should pass through for .not.become(42, message)', () => { + shouldFail({ + op: () => promise.should.not.become(42, message), + message }); + }); + describe('should pass through for .not.become(52, message)', () => { + shouldPass(() => promise.should.not.become(52, message)); + }); - describe("should pass through for .eventually.equal(42)", () => { - shouldPass(() => promise.should.eventually.equal(42, message)); - }); - describe("should pass through for .not.eventually.equal(42)", () => { - shouldFail({ - op: () => promise.should.not.eventually.equal(42, message), - message - }); + describe('should pass through for .eventually.equal(42)', () => { + shouldPass(() => promise.should.eventually.equal(42, message)); + }); + describe('should pass through for .not.eventually.equal(42)', () => { + shouldFail({ + op: () => promise.should.not.eventually.equal(42, message), + message }); + }); }); diff --git a/test/proxy-guard.js b/test/proxy-guard.js index bf7397e..3de509a 100644 --- a/test/proxy-guard.js +++ b/test/proxy-guard.js @@ -1,79 +1,86 @@ -"use strict"; -const chai = require("chai"); -const chaiAsPromised = require(".."); +'use strict'; +const chai = require('chai'); +const chaiAsPromised = require('..'); chai.should(); chai.use(chaiAsPromised); function shouldGuard(fn, msg) { - fn.should.throw("Invalid Chai property: " + msg); + fn.should.throw('Invalid Chai property: ' + msg); } -describe("Proxy guard", () => { - const number = 42; - const promise = Promise.resolve(42); - - before(function () { - if (typeof Proxy === "undefined" || typeof Reflect === "undefined" || chai.util.proxify === undefined) { - /* eslint-disable no-invalid-this */ - this.skip(); - /* eslint-enable no-invalid-this */ - } - }); - - it("should guard against invalid property following `.should`", () => { - shouldGuard(() => number.should.pizza, "pizza"); - }); - - it("should guard against invalid property following overwritten language chain", () => { - shouldGuard(() => number.should.to.pizza, "pizza"); - }); - - it("should guard against invalid property following overwritten property assertion", () => { - shouldGuard(() => number.should.ok.pizza, "pizza"); - }); - - it("should guard against invalid property following uncalled overwritten method assertion", () => { - shouldGuard(() => number.should.equal.pizza, "equal.pizza. See docs"); - }); - - it("should guard against invalid property following called overwritten method assertion", () => { - shouldGuard(() => number.should.equal(number).pizza, "pizza"); - }); - - it("should guard against invalid property following uncalled overwritten chainable method assertion", () => { - shouldGuard(() => number.should.a.pizza, "pizza"); - }); - - it("should guard against invalid property following called overwritten chainable method assertion", () => { - shouldGuard(() => number.should.a("number").pizza, "pizza"); - }); - - it("should guard against invalid property following `.eventually`", () => { - shouldGuard(() => promise.should.eventually.pizza, "pizza"); - }); - - it("should guard against invalid property following `.fulfilled`", () => { - shouldGuard(() => promise.should.fulfilled.pizza, "pizza"); - }); - - it("should guard against invalid property following `.rejected`", () => { - shouldGuard(() => promise.should.rejected.pizza, "pizza"); - }); - - it("should guard against invalid property following called `.rejectedWith`", () => { - shouldGuard(() => promise.should.rejectedWith(42).pizza, "pizza"); - }); - - it("should guard against invalid property following uncalled `.rejectedWith`", () => { - shouldGuard(() => promise.should.rejectedWith.pizza, "rejectedWith.pizza. See docs"); - }); - - it("should guard against invalid property following called `.become`", () => { - shouldGuard(() => promise.should.become(42).pizza, "pizza"); - }); - - it("should guard against invalid property following uncalled `.become`", () => { - shouldGuard(() => promise.should.become.pizza, "become.pizza. See docs"); - }); +describe('Proxy guard', () => { + const number = 42; + const promise = Promise.resolve(42); + + before(function () { + if ( + typeof Proxy === 'undefined' || + typeof Reflect === 'undefined' || + chai.util.proxify === undefined + ) { + /* eslint-disable no-invalid-this */ + this.skip(); + /* eslint-enable no-invalid-this */ + } + }); + + it('should guard against invalid property following `.should`', () => { + shouldGuard(() => number.should.pizza, 'pizza'); + }); + + it('should guard against invalid property following overwritten language chain', () => { + shouldGuard(() => number.should.to.pizza, 'pizza'); + }); + + it('should guard against invalid property following overwritten property assertion', () => { + shouldGuard(() => number.should.ok.pizza, 'pizza'); + }); + + it('should guard against invalid property following uncalled overwritten method assertion', () => { + shouldGuard(() => number.should.equal.pizza, 'equal.pizza. See docs'); + }); + + it('should guard against invalid property following called overwritten method assertion', () => { + shouldGuard(() => number.should.equal(number).pizza, 'pizza'); + }); + + it('should guard against invalid property following uncalled overwritten chainable method assertion', () => { + shouldGuard(() => number.should.a.pizza, 'pizza'); + }); + + it('should guard against invalid property following called overwritten chainable method assertion', () => { + shouldGuard(() => number.should.a('number').pizza, 'pizza'); + }); + + it('should guard against invalid property following `.eventually`', () => { + shouldGuard(() => promise.should.eventually.pizza, 'pizza'); + }); + + it('should guard against invalid property following `.fulfilled`', () => { + shouldGuard(() => promise.should.fulfilled.pizza, 'pizza'); + }); + + it('should guard against invalid property following `.rejected`', () => { + shouldGuard(() => promise.should.rejected.pizza, 'pizza'); + }); + + it('should guard against invalid property following called `.rejectedWith`', () => { + shouldGuard(() => promise.should.rejectedWith(42).pizza, 'pizza'); + }); + + it('should guard against invalid property following uncalled `.rejectedWith`', () => { + shouldGuard( + () => promise.should.rejectedWith.pizza, + 'rejectedWith.pizza. See docs' + ); + }); + + it('should guard against invalid property following called `.become`', () => { + shouldGuard(() => promise.should.become(42).pizza, 'pizza'); + }); + + it('should guard against invalid property following uncalled `.become`', () => { + shouldGuard(() => promise.should.become.pizza, 'become.pizza. See docs'); + }); }); diff --git a/test/should-eventually.js b/test/should-eventually.js index ccd4294..7ffa1dc 100644 --- a/test/should-eventually.js +++ b/test/should-eventually.js @@ -1,220 +1,243 @@ -"use strict"; -require("./support/setup.js"); -const shouldPass = require("./support/common.js").shouldPass; -const shouldFail = require("./support/common.js").shouldFail; +'use strict'; +require('./support/setup.js'); +const shouldPass = require('./support/common.js').shouldPass; +const shouldFail = require('./support/common.js').shouldFail; -describe("Fulfillment value assertions:", () => { - let promise = null; +describe('Fulfillment value assertions:', () => { + let promise = null; - describe("Direct tests of fulfilled promises:", () => { - describe("Basics:", () => { - it(".eventually.equal(42)", done => { - Promise.resolve(42).should.eventually.equal(42).notify(done); - }); - it(".eventually.be.arguments", function (done) { - Promise.resolve(arguments).should.eventually.be.arguments.notify(done); - }); - it(".eventually.be.empty", done => { - Promise.resolve([]).should.eventually.be.empty.notify(done); - }); - it(".eventually.exist", done => { - Promise.resolve(true).should.eventually.exist.notify(done); - }); - it(".eventually.be.false", done => { - Promise.resolve(false).should.eventually.be.false.notify(done); - }); - it(".eventually.be.ok", done => { - Promise.resolve({}).should.eventually.be.ok.notify(done); - }); - it(".eventually.be.true", done => { - Promise.resolve(true).should.eventually.be.true.notify(done); - }); - it(".become(true)", done => { - Promise.resolve(true).should.become(true).notify(done); - }); - }); + describe('Direct tests of fulfilled promises:', () => { + describe('Basics:', () => { + it('.eventually.equal(42)', (done) => { + Promise.resolve(42).should.eventually.equal(42).notify(done); + }); + it('.eventually.be.arguments', function (done) { + Promise.resolve(arguments).should.eventually.be.arguments.notify(done); + }); + it('.eventually.be.empty', (done) => { + Promise.resolve([]).should.eventually.be.empty.notify(done); + }); + it('.eventually.exist', (done) => { + Promise.resolve(true).should.eventually.exist.notify(done); + }); + it('.eventually.be.false', (done) => { + Promise.resolve(false).should.eventually.be.false.notify(done); + }); + it('.eventually.be.ok', (done) => { + Promise.resolve({}).should.eventually.be.ok.notify(done); + }); + it('.eventually.be.true', (done) => { + Promise.resolve(true).should.eventually.be.true.notify(done); + }); + it('.become(true)', (done) => { + Promise.resolve(true).should.become(true).notify(done); + }); + }); - describe("With flags and chainable methods involved:", () => { - it(".not.eventually.be.ok", done => { - Promise.resolve(false).should.not.eventually.be.ok.notify(done); - }); - it(".eventually.not.be.ok", done => { - Promise.resolve(false).should.eventually.not.be.ok.notify(done); - }); - it(".eventually.deep.equal({ foo: 'bar' })", done => { - Promise.resolve({ foo: "bar" }).should.eventually.deep.equal({ foo: "bar" }).notify(done); - }); - it(".not.eventually.deep.equal({ foo: 'bar' })", done => { - Promise.resolve({ foo: "baz" }).should.not.eventually.deep.equal({ foo: "bar" }).notify(done); - }); - it(".eventually.not.deep.equal({ foo: 'bar' })", done => { - Promise.resolve({ foo: "baz" }).should.eventually.not.deep.equal({ foo: "bar" }).notify(done); - }); - it(".eventually.contain('foo')", done => { - Promise.resolve(["foo", "bar"]).should.eventually.contain("foo").notify(done); - }); - it(".not.eventually.contain('foo')", done => { - Promise.resolve(["bar", "baz"]).should.not.eventually.contain("foo").notify(done); - }); - it(".eventually.not.contain('foo')", done => { - Promise.resolve(["bar", "baz"]).should.eventually.not.contain("foo").notify(done); - }); - it(".eventually.contain.keys('foo')", done => { - Promise.resolve({ foo: "bar", baz: "quux" }).should.eventually.contain.keys("foo").notify(done); - }); - it(".not.eventually.contain.keys('foo')", done => { - Promise.resolve({ baz: "quux" }).should.not.eventually.contain.keys("foo").notify(done); - }); - it(".eventually.not.contain.keys('foo')", done => { - Promise.resolve({ baz: "quux" }).should.eventually.not.contain.keys("foo").notify(done); - }); - it(".eventually.be.an.instanceOf(Array)", done => { - Promise.resolve([]).should.eventually.be.an.instanceOf(Array).notify(done); - }); + describe('With flags and chainable methods involved:', () => { + it('.not.eventually.be.ok', (done) => { + Promise.resolve(false).should.not.eventually.be.ok.notify(done); + }); + it('.eventually.not.be.ok', (done) => { + Promise.resolve(false).should.eventually.not.be.ok.notify(done); + }); + it(".eventually.deep.equal({ foo: 'bar' })", (done) => { + Promise.resolve({foo: 'bar'}) + .should.eventually.deep.equal({foo: 'bar'}) + .notify(done); + }); + it(".not.eventually.deep.equal({ foo: 'bar' })", (done) => { + Promise.resolve({foo: 'baz'}) + .should.not.eventually.deep.equal({foo: 'bar'}) + .notify(done); + }); + it(".eventually.not.deep.equal({ foo: 'bar' })", (done) => { + Promise.resolve({foo: 'baz'}) + .should.eventually.not.deep.equal({foo: 'bar'}) + .notify(done); + }); + it(".eventually.contain('foo')", (done) => { + Promise.resolve(['foo', 'bar']) + .should.eventually.contain('foo') + .notify(done); + }); + it(".not.eventually.contain('foo')", (done) => { + Promise.resolve(['bar', 'baz']) + .should.not.eventually.contain('foo') + .notify(done); + }); + it(".eventually.not.contain('foo')", (done) => { + Promise.resolve(['bar', 'baz']) + .should.eventually.not.contain('foo') + .notify(done); + }); + it(".eventually.contain.keys('foo')", (done) => { + Promise.resolve({foo: 'bar', baz: 'quux'}) + .should.eventually.contain.keys('foo') + .notify(done); + }); + it(".not.eventually.contain.keys('foo')", (done) => { + Promise.resolve({baz: 'quux'}) + .should.not.eventually.contain.keys('foo') + .notify(done); + }); + it(".eventually.not.contain.keys('foo')", (done) => { + Promise.resolve({baz: 'quux'}) + .should.eventually.not.contain.keys('foo') + .notify(done); + }); + it('.eventually.be.an.instanceOf(Array)', (done) => { + Promise.resolve([]) + .should.eventually.be.an.instanceOf(Array) + .notify(done); + }); - if (Object.prototype.should.nested) { - it(".eventually.have.nested.property('foo.bar')", done => { - Promise.resolve({ foo: { bar: "baz" } }).should.eventually.have.nested.property("foo.bar", "baz") - .notify(done); - }); - } + if (Object.prototype.should.nested) { + it(".eventually.have.nested.property('foo.bar')", (done) => { + Promise.resolve({foo: {bar: 'baz'}}) + .should.eventually.have.nested.property('foo.bar', 'baz') + .notify(done); }); + } }); + }); - describe("Chaining:", () => { - it(".eventually.be.ok.and.equal(42)", done => { - Promise.resolve(42).should.eventually.be.ok.and.equal(42).notify(done); - }); - it(".rejected.and.notify(done)", done => { - Promise.reject().should.be.rejected.and.notify(done); - }); - it(".fulfilled.and.notify(done)", done => { - Promise.resolve().should.be.fulfilled.and.notify(done); - }); + describe('Chaining:', () => { + it('.eventually.be.ok.and.equal(42)', (done) => { + Promise.resolve(42).should.eventually.be.ok.and.equal(42).notify(done); + }); + it('.rejected.and.notify(done)', (done) => { + Promise.reject().should.be.rejected.and.notify(done); }); + it('.fulfilled.and.notify(done)', (done) => { + Promise.resolve().should.be.fulfilled.and.notify(done); + }); + }); - describe("On a promise fulfilled with the number 42:", () => { - beforeEach(() => { - promise = Promise.resolve(42); - }); + describe('On a promise fulfilled with the number 42:', () => { + beforeEach(() => { + promise = Promise.resolve(42); + }); - describe(".eventually.equal(42)", () => { - shouldPass(() => promise.should.eventually.equal(42)); - }); - describe(".eventually.eql(42)", () => { - shouldPass(() => promise.should.eventually.eql(42)); - }); - describe(".eventually.be.below(9000)", () => { - shouldPass(() => promise.should.eventually.be.below(9000)); - }); - describe(".eventually.be.a('number')", () => { - shouldPass(() => promise.should.eventually.be.a("number")); - }); + describe('.eventually.equal(42)', () => { + shouldPass(() => promise.should.eventually.equal(42)); + }); + describe('.eventually.eql(42)', () => { + shouldPass(() => promise.should.eventually.eql(42)); + }); + describe('.eventually.be.below(9000)', () => { + shouldPass(() => promise.should.eventually.be.below(9000)); + }); + describe(".eventually.be.a('number')", () => { + shouldPass(() => promise.should.eventually.be.a('number')); + }); - describe(".eventually.be.an.instanceOf(String)", () => { - shouldFail({ - op: () => promise.should.eventually.be.an.instanceOf(String), - message: "42 to be an instance of String" - }); - }); - describe(".eventually.be.false", () => { - shouldFail({ - op: () => promise.should.eventually.be.false, - message: "to be false" - }); - }); - describe(".eventually.be.an('object')", () => { - shouldFail({ - op: () => promise.should.eventually.be.an("object"), - message: "to be an object" - }); - }); + describe('.eventually.be.an.instanceOf(String)', () => { + shouldFail({ + op: () => promise.should.eventually.be.an.instanceOf(String), + message: '42 to be an instance of String' + }); + }); + describe('.eventually.be.false', () => { + shouldFail({ + op: () => promise.should.eventually.be.false, + message: 'to be false' + }); + }); + describe(".eventually.be.an('object')", () => { + shouldFail({ + op: () => promise.should.eventually.be.an('object'), + message: 'to be an object' + }); + }); - describe(".eventually.not.equal(52)", () => { - shouldPass(() => promise.should.eventually.not.equal(52)); - }); - describe(".not.eventually.equal(52)", () => { - shouldPass(() => promise.should.not.eventually.equal(52)); - }); + describe('.eventually.not.equal(52)', () => { + shouldPass(() => promise.should.eventually.not.equal(52)); + }); + describe('.not.eventually.equal(52)', () => { + shouldPass(() => promise.should.not.eventually.equal(52)); + }); - describe(".eventually.not.equal(42)", () => { - shouldFail({ - op: () => promise.should.eventually.not.equal(42), - message: "not equal 42" - }); - }); - describe(".not.eventually.equal(42)", () => { - shouldFail({ - op: () => promise.should.not.eventually.equal(42), - message: "not equal 42" - }); - }); + describe('.eventually.not.equal(42)', () => { + shouldFail({ + op: () => promise.should.eventually.not.equal(42), + message: 'not equal 42' + }); + }); + describe('.not.eventually.equal(42)', () => { + shouldFail({ + op: () => promise.should.not.eventually.equal(42), + message: 'not equal 42' + }); + }); - describe(".become(42)", () => { - shouldPass(() => promise.should.become(42)); - }); - describe(".become(52)", () => { - shouldFail({ - op: () => promise.should.become(52), - message: "deeply equal 52" - }); - }); + describe('.become(42)', () => { + shouldPass(() => promise.should.become(42)); + }); + describe('.become(52)', () => { + shouldFail({ + op: () => promise.should.become(52), + message: 'deeply equal 52' + }); + }); - describe(".not.become(42)", () => { - shouldFail({ - op: () => promise.should.not.become(42), - message: "not deeply equal 42" - }); - }); - describe(".not.become(52)", () => { - shouldPass(() => promise.should.not.become(52)); - }); + describe('.not.become(42)', () => { + shouldFail({ + op: () => promise.should.not.become(42), + message: 'not deeply equal 42' + }); + }); + describe('.not.become(52)', () => { + shouldPass(() => promise.should.not.become(52)); }); + }); - describe("On a promise fulfilled with { foo: 'bar' }:", () => { - beforeEach(() => { - promise = Promise.resolve({ foo: "bar" }); - }); + describe("On a promise fulfilled with { foo: 'bar' }:", () => { + beforeEach(() => { + promise = Promise.resolve({foo: 'bar'}); + }); - describe(".eventually.equal({ foo: 'bar' })", () => { - shouldFail({ - op: () => promise.should.eventually.equal({ foo: "bar" }), - message: "to equal { foo: 'bar' }" - }); - }); - describe(".eventually.eql({ foo: 'bar' })", () => { - shouldPass(() => promise.should.eventually.eql({ foo: "bar" })); - }); - describe(".eventually.deep.equal({ foo: 'bar' })", () => { - shouldPass(() => promise.should.eventually.deep.equal({ foo: "bar" })); - }); - describe(".eventually.not.deep.equal({ foo: 'bar' })", () => { - shouldFail({ - op: () => promise.should.eventually.not.deep.equal({ foo: "bar" }), - message: "not deeply equal { foo: 'bar' }" - }); - }); - describe(".eventually.deep.equal({ baz: 'quux' })", () => { - shouldFail({ - op: () => promise.should.eventually.deep.equal({ baz: "quux" }), - message: "deeply equal { baz: 'quux' }" - }); - }); - describe(".eventually.not.deep.equal({ baz: 'quux' })", () => { - shouldPass(() => promise.should.eventually.not.deep.equal({ baz: "quux" })); - }); - describe(".become({ foo: 'bar' })", () => { - shouldPass(() => promise.should.become({ foo: "bar" })); - }); - describe(".not.become({ foo: 'bar' })", () => { - shouldFail({ - op: () => promise.should.not.become({ foo: "bar" }), - message: "deeply equal { foo: 'bar' }" - }); - }); + describe(".eventually.equal({ foo: 'bar' })", () => { + shouldFail({ + op: () => promise.should.eventually.equal({foo: 'bar'}), + message: "to equal { foo: 'bar' }" + }); + }); + describe(".eventually.eql({ foo: 'bar' })", () => { + shouldPass(() => promise.should.eventually.eql({foo: 'bar'})); + }); + describe(".eventually.deep.equal({ foo: 'bar' })", () => { + shouldPass(() => promise.should.eventually.deep.equal({foo: 'bar'})); + }); + describe(".eventually.not.deep.equal({ foo: 'bar' })", () => { + shouldFail({ + op: () => promise.should.eventually.not.deep.equal({foo: 'bar'}), + message: "not deeply equal { foo: 'bar' }" + }); + }); + describe(".eventually.deep.equal({ baz: 'quux' })", () => { + shouldFail({ + op: () => promise.should.eventually.deep.equal({baz: 'quux'}), + message: "deeply equal { baz: 'quux' }" + }); + }); + describe(".eventually.not.deep.equal({ baz: 'quux' })", () => { + shouldPass(() => promise.should.eventually.not.deep.equal({baz: 'quux'})); + }); + describe(".become({ foo: 'bar' })", () => { + shouldPass(() => promise.should.become({foo: 'bar'})); + }); + describe(".not.become({ foo: 'bar' })", () => { + shouldFail({ + op: () => promise.should.not.become({foo: 'bar'}), + message: "deeply equal { foo: 'bar' }" + }); + }); - describe(".eventually.have.property('foo').that.equals('bar')", () => { - shouldPass(() => promise.should.eventually.have.property("foo").that.equals("bar")); - }); + describe(".eventually.have.property('foo').that.equals('bar')", () => { + shouldPass(() => + promise.should.eventually.have.property('foo').that.equals('bar') + ); }); + }); }); diff --git a/test/should-promise-specific.js b/test/should-promise-specific.js index 2653100..b3d18d7 100644 --- a/test/should-promise-specific.js +++ b/test/should-promise-specific.js @@ -1,639 +1,730 @@ -"use strict"; -require("./support/setup.js"); -const shouldPass = require("./support/common.js").shouldPass; -const shouldFail = require("./support/common.js").shouldFail; -const expect = require("chai").expect; - -describe("Promise-specific extensions:", () => { - let promise = null; - const error = new Error("boo"); - error.myProp = ["myProp value"]; - const custom = "No. I am your father."; - - function assertingDoneFactory(done) { - return result => { - try { - expect(result).to.equal(error); - } catch (assertionError) { - done(assertionError); - } - - done(); - }; - } - - describe("when the promise is fulfilled", () => { - beforeEach(() => { - promise = Promise.resolve(42); - }); +'use strict'; +require('./support/setup.js'); +const shouldPass = require('./support/common.js').shouldPass; +const shouldFail = require('./support/common.js').shouldFail; +const expect = require('chai').expect; + +describe('Promise-specific extensions:', () => { + let promise = null; + const error = new Error('boo'); + error.myProp = ['myProp value']; + const custom = 'No. I am your father.'; + + function assertingDoneFactory(done) { + return (result) => { + try { + expect(result).to.equal(error); + } catch (assertionError) { + done(assertionError); + } + + done(); + }; + } + + describe('when the promise is fulfilled', () => { + beforeEach(() => { + promise = Promise.resolve(42); + }); - describe(".fulfilled", () => { - shouldPass(() => promise.should.be.fulfilled); - }); + describe('.fulfilled', () => { + shouldPass(() => promise.should.be.fulfilled); + }); - describe(".fulfilled passes the fulfilled value", () => { - shouldPass(() => promise.should.be.fulfilled.then(passedValue => { - passedValue.should.equal(42); - })); - }); + describe('.fulfilled passes the fulfilled value', () => { + shouldPass(() => + promise.should.be.fulfilled.then((passedValue) => { + passedValue.should.equal(42); + }) + ); + }); - describe(".fulfilled allows chaining", () => { - shouldPass(() => promise.should.be.fulfilled.and.eventually.equal(42)); - }); + describe('.fulfilled allows chaining', () => { + shouldPass(() => promise.should.be.fulfilled.and.eventually.equal(42)); + }); - describe(".not.fulfilled", () => { - shouldFail({ - op: () => promise.should.not.be.fulfilled, - message: "not to be fulfilled but it was fulfilled with 42" - }); - }); + describe('.not.fulfilled', () => { + shouldFail({ + op: () => promise.should.not.be.fulfilled, + message: 'not to be fulfilled but it was fulfilled with 42' + }); + }); - describe(".rejected", () => { - shouldFail({ - op: () => promise.should.be.rejected, - message: "to be rejected but it was fulfilled with 42" - }); - }); + describe('.rejected', () => { + shouldFail({ + op: () => promise.should.be.rejected, + message: 'to be rejected but it was fulfilled with 42' + }); + }); - describe(".not.rejected passes the fulfilled value", () => { - shouldPass(() => promise.should.not.be.rejected.then(passedValue => { - passedValue.should.equal(42); - })); - }); + describe('.not.rejected passes the fulfilled value', () => { + shouldPass(() => + promise.should.not.be.rejected.then((passedValue) => { + passedValue.should.equal(42); + }) + ); + }); - // .not inverts all following assertions so the following test is - // equivalent to promise.should.eventually.not.equal(31) - describe(".not.rejected allows chaining", () => { - shouldPass(() => promise.should.not.be.rejected.and.eventually.equal(31)); - }); + // .not inverts all following assertions so the following test is + // equivalent to promise.should.eventually.not.equal(31) + describe('.not.rejected allows chaining', () => { + shouldPass(() => promise.should.not.be.rejected.and.eventually.equal(31)); + }); - describe(".rejectedWith(TypeError)", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(TypeError), - message: "to be rejected with 'TypeError' but it was fulfilled with 42" - }); - }); - describe(".not.rejectedWith(TypeError) passes the fulfilled value", () => { - shouldPass(() => promise.should.not.be.rejectedWith(TypeError).then(passedValue => { - passedValue.should.equal(42); - })); - }); + describe('.rejectedWith(TypeError)', () => { + shouldFail({ + op: () => promise.should.be.rejectedWith(TypeError), + message: "to be rejected with 'TypeError' but it was fulfilled with 42" + }); + }); + describe('.not.rejectedWith(TypeError) passes the fulfilled value', () => { + shouldPass(() => + promise.should.not.be.rejectedWith(TypeError).then((passedValue) => { + passedValue.should.equal(42); + }) + ); + }); - describe(".not.rejectedWith(TypeError) allows chaining", () => { - shouldPass(() => promise.should.not.be.rejectedWith(TypeError).and.eventually.equal(31)); - }); + describe('.not.rejectedWith(TypeError) allows chaining', () => { + shouldPass(() => + promise.should.not.be.rejectedWith(TypeError).and.eventually.equal(31) + ); + }); - describe(".rejectedWith('message substring')", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith("message substring"), - message: "to be rejected with an error including 'message substring' but it was fulfilled with " + - "42" - }); - }); - describe(".rejectedWith(/regexp/)", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(/regexp/), - message: "to be rejected with an error matching /regexp/ but it was fulfilled with 42" - }); - }); - describe(".rejectedWith(TypeError, 'message substring')", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(TypeError, "message substring"), - message: "to be rejected with 'TypeError' but it was fulfilled with 42" - }); - }); - describe(".rejectedWith(TypeError, /regexp/)", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(TypeError, /regexp/), - message: "to be rejected with 'TypeError' but it was fulfilled with 42" - }); - }); - describe(".rejectedWith(errorInstance)", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(error), - message: "to be rejected with 'Error: boo' but it was fulfilled with 42" - }); - }); + describe(".rejectedWith('message substring')", () => { + shouldFail({ + op: () => promise.should.be.rejectedWith('message substring'), + message: + "to be rejected with an error including 'message substring' but it was fulfilled with " + + '42' + }); + }); + describe('.rejectedWith(/regexp/)', () => { + shouldFail({ + op: () => promise.should.be.rejectedWith(/regexp/), + message: + 'to be rejected with an error matching /regexp/ but it was fulfilled with 42' + }); + }); + describe(".rejectedWith(TypeError, 'message substring')", () => { + shouldFail({ + op: () => + promise.should.be.rejectedWith(TypeError, 'message substring'), + message: "to be rejected with 'TypeError' but it was fulfilled with 42" + }); + }); + describe('.rejectedWith(TypeError, /regexp/)', () => { + shouldFail({ + op: () => promise.should.be.rejectedWith(TypeError, /regexp/), + message: "to be rejected with 'TypeError' but it was fulfilled with 42" + }); + }); + describe('.rejectedWith(errorInstance)', () => { + shouldFail({ + op: () => promise.should.be.rejectedWith(error), + message: "to be rejected with 'Error: boo' but it was fulfilled with 42" + }); + }); - describe(".not.rejected", () => { - shouldPass(() => promise.should.not.be.rejected); - }); - describe(".not.rejectedWith(TypeError)", () => { - shouldPass(() => promise.should.not.be.rejectedWith(TypeError)); - }); - describe(".not.rejectedWith('message substring')", () => { - shouldPass(() => promise.should.not.be.rejectedWith("message substring")); - }); - describe(".not.rejectedWith(/regexp/)", () => { - shouldPass(() => promise.should.not.be.rejectedWith(/regexp/)); - }); - describe(".not.rejectedWith(TypeError, 'message substring')", () => { - shouldPass(() => promise.should.not.be.rejectedWith(TypeError, "message substring")); - }); - describe(".not.rejectedWith(TypeError, /regexp/)", () => { - shouldPass(() => promise.should.not.be.rejectedWith(TypeError, /regexp/)); - }); - describe(".not.rejectedWith(errorInstance)", () => { - shouldPass(() => promise.should.not.be.rejectedWith(error)); - }); + describe('.not.rejected', () => { + shouldPass(() => promise.should.not.be.rejected); + }); + describe('.not.rejectedWith(TypeError)', () => { + shouldPass(() => promise.should.not.be.rejectedWith(TypeError)); + }); + describe(".not.rejectedWith('message substring')", () => { + shouldPass(() => promise.should.not.be.rejectedWith('message substring')); + }); + describe('.not.rejectedWith(/regexp/)', () => { + shouldPass(() => promise.should.not.be.rejectedWith(/regexp/)); + }); + describe(".not.rejectedWith(TypeError, 'message substring')", () => { + shouldPass(() => + promise.should.not.be.rejectedWith(TypeError, 'message substring') + ); + }); + describe('.not.rejectedWith(TypeError, /regexp/)', () => { + shouldPass(() => promise.should.not.be.rejectedWith(TypeError, /regexp/)); + }); + describe('.not.rejectedWith(errorInstance)', () => { + shouldPass(() => promise.should.not.be.rejectedWith(error)); + }); - describe(".should.notify(done)", () => { - it("should pass the test", done => { - promise.should.notify(done); - }); - }); + describe('.should.notify(done)', () => { + it('should pass the test', (done) => { + promise.should.notify(done); + }); }); + }); - describe("when the promise is rejected", () => { - beforeEach(() => { - promise = Promise.reject(error); - }); + describe('when the promise is rejected', () => { + beforeEach(() => { + promise = Promise.reject(error); + }); - describe(".fulfilled", () => { - shouldFail({ - op: () => promise.should.be.fulfilled, - message: "to be fulfilled but it was rejected with 'Error: boo'" - }); - }); + describe('.fulfilled', () => { + shouldFail({ + op: () => promise.should.be.fulfilled, + message: "to be fulfilled but it was rejected with 'Error: boo'" + }); + }); - describe(".not.fulfilled", () => { - shouldPass(() => promise.should.not.be.fulfilled); - }); + describe('.not.fulfilled', () => { + shouldPass(() => promise.should.not.be.fulfilled); + }); - describe(".not.fulfilled should allow chaining", () => { - shouldPass(() => promise.should.not.be.fulfilled.and.eventually.have.property("nonexistent")); - }); + describe('.not.fulfilled should allow chaining', () => { + shouldPass(() => + promise.should.not.be.fulfilled.and.eventually.have.property( + 'nonexistent' + ) + ); + }); - describe(".not.fulfilled should pass the rejection reason", () => { - shouldPass(() => promise.should.not.be.fulfilled.then(passedError => { - passedError.should.equal(error); - })); - }); + describe('.not.fulfilled should pass the rejection reason', () => { + shouldPass(() => + promise.should.not.be.fulfilled.then((passedError) => { + passedError.should.equal(error); + }) + ); + }); - describe(".rejected", () => { - shouldPass(() => promise.should.be.rejected); - }); + describe('.rejected', () => { + shouldPass(() => promise.should.be.rejected); + }); - describe(".not.rejected", () => { - shouldFail({ - op: () => promise.should.not.be.rejected, - message: "not to be rejected but it was rejected with 'Error: boo'" - }); - }); - describe(".rejected should allow chaining", () => { - shouldPass(() => promise.should.be.rejected.and.eventually.have.property("myProp")); - }); + describe('.not.rejected', () => { + shouldFail({ + op: () => promise.should.not.be.rejected, + message: "not to be rejected but it was rejected with 'Error: boo'" + }); + }); + describe('.rejected should allow chaining', () => { + shouldPass(() => + promise.should.be.rejected.and.eventually.have.property('myProp') + ); + }); + + describe('.rejected passes the rejection reason', () => { + shouldPass(() => + promise.should.be.rejected.then((passedError) => { + passedError.should.equal(error); + }) + ); + }); + + describe('.rejectedWith(theError)', () => { + shouldPass(() => promise.should.be.rejectedWith(error)); + }); + + describe('.not.rejectedWith(theError)', () => { + shouldFail({ + op: () => promise.should.not.be.rejectedWith(error), + message: "not to be rejected with 'Error: boo'" + }); + }); + + describe('.rejectedWith(theError) should allow chaining', () => { + shouldPass(() => + promise.should.be + .rejectedWith(error) + .and.eventually.have.property('myProp') + ); + }); + + describe('.rejectedWith(theError) passes the rejection reason', () => { + shouldPass(() => + promise.should.be.rejectedWith(error).then((passedError) => { + passedError.should.equal(error); + }) + ); + }); + + describe('.rejectedWith(differentError)', () => { + shouldFail({ + op: () => promise.should.be.rejectedWith(new Error()), + message: + "to be rejected with 'Error' but it was rejected with 'Error: boo'" + }); + }); + + describe('.not.rejectedWith(differentError)', () => { + shouldPass(() => promise.should.not.be.rejectedWith(new Error())); + }); + + // Chai 3.5.0 never interprets the 2nd paramter to + // expect(fn).to.throw(a, b) as a custom error message. This is + // what we are testing here. + describe('.rejectedWith(differentError, custom)', () => { + shouldFail({ + op: () => promise.should.be.rejectedWith(new Error(), custom), + message: + "to be rejected with 'Error' but it was rejected with 'Error: boo'", + notMessage: custom + }); + }); + + describe('.not.rejectedWith(differentError, custom)', () => { + shouldPass(() => promise.should.not.be.rejectedWith(new Error(), custom)); + }); + + describe("with an Error having message 'foo bar'", () => { + beforeEach(() => { + promise = Promise.reject(new Error('foo bar')); + }); + + describe(".rejectedWith('foo')", () => { + shouldPass(() => promise.should.be.rejectedWith('foo')); + }); - describe(".rejected passes the rejection reason", () => { - shouldPass(() => promise.should.be.rejected.then(passedError => { - passedError.should.equal(error); - })); + describe(".not.rejectedWith('foo')", () => { + shouldFail({ + op: () => promise.should.not.be.rejectedWith('foo'), + message: "not to be rejected with an error including 'foo'" }); + }); - describe(".rejectedWith(theError)", () => { - shouldPass(() => promise.should.be.rejectedWith(error)); + describe('.rejectedWith(/bar/)', () => { + shouldPass(() => promise.should.be.rejectedWith(/bar/)); + }); + + describe('.not.rejectedWith(/bar/)', () => { + shouldFail({ + op: () => promise.should.not.be.rejectedWith(/bar/), + message: 'not to be rejected with an error matching /bar/' }); + }); - describe(".not.rejectedWith(theError)", () => { - shouldFail({ - op: () => promise.should.not.be.rejectedWith(error), - message: "not to be rejected with 'Error: boo'" - }); + describe(".rejectedWith('quux')", () => { + shouldFail({ + op: () => promise.should.be.rejectedWith('quux'), + message: + "to be rejected with an error including 'quux' but got 'foo bar'" }); + }); + + describe(".not.rejectedWith('quux')", () => { + shouldPass(() => promise.should.be.not.rejectedWith('quux')); + }); - describe(".rejectedWith(theError) should allow chaining", () => { - shouldPass(() => promise.should.be.rejectedWith(error).and.eventually.have.property("myProp")); + describe('.rejectedWith(/quux/)', () => { + shouldFail({ + op: () => promise.should.be.rejectedWith(/quux/), + message: + "to be rejected with an error matching /quux/ but got 'foo bar'" }); + }); + + describe('.not.rejectedWith(/quux/)', () => { + shouldPass(() => promise.should.not.be.rejectedWith(/quux/)); + }); + + // Chai 3.5.0 never interprets the 2nd paramter to + // expect(fn).to.throw(a, b) as a custom error + // message. This is what we are testing here. + describe(".rejectedWith('foo', custom)", () => { + shouldPass(() => promise.should.be.rejectedWith('foo', custom)); + }); - describe(".rejectedWith(theError) passes the rejection reason", () => { - shouldPass(() => promise.should.be.rejectedWith(error).then(passedError => { - passedError.should.equal(error); - })); + describe(".not.rejectedWith('foo', custom)", () => { + shouldFail({ + op: () => promise.should.not.be.rejectedWith('foo', custom), + message: "not to be rejected with an error including 'foo'", + notMessage: custom }); + }); - describe(".rejectedWith(differentError)", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(new Error()), - message: "to be rejected with 'Error' but it was rejected with 'Error: boo'" - }); + describe('.rejectedWith(/bar/, custom)', () => { + shouldPass(() => promise.should.be.rejectedWith(/bar/, custom)); + }); + + describe('.not.rejectedWith(/bar/, custom)', () => { + shouldFail({ + op: () => promise.should.not.be.rejectedWith(/bar/), + message: 'not to be rejected with an error matching /bar/', + notMessage: custom }); + }); + }); + + describe('with a RangeError', () => { + beforeEach(() => { + promise = Promise.reject(new RangeError()); + }); - describe(".not.rejectedWith(differentError)", () => { - shouldPass(() => promise.should.not.be.rejectedWith(new Error())); + describe('.rejectedWith(RangeError)', () => { + shouldPass(() => promise.should.be.rejectedWith(RangeError)); + }); + + describe('.not.rejectedWith(RangeError)', () => { + shouldFail({ + op: () => promise.should.not.be.rejectedWith(RangeError), + message: + "not to be rejected with 'RangeError' but it was rejected with 'RangeError'" }); + }); - // Chai 3.5.0 never interprets the 2nd paramter to - // expect(fn).to.throw(a, b) as a custom error message. This is - // what we are testing here. - describe(".rejectedWith(differentError, custom)", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(new Error(), custom), - message: "to be rejected with 'Error' but it was rejected with 'Error: boo'", - notMessage: custom - }); + describe('.rejectedWith(TypeError)', () => { + shouldFail({ + op: () => promise.should.be.rejectedWith(TypeError), + message: + "to be rejected with 'TypeError' but it was rejected with 'RangeError'" }); + }); - describe(".not.rejectedWith(differentError, custom)", () => { - shouldPass(() => promise.should.not.be.rejectedWith(new Error(), custom)); + // Case for issue #64. + describe('.rejectedWith(Array)', () => { + shouldFail({ + op: () => promise.should.be.rejectedWith(Array), + message: + "to be rejected with 'Array' but it was rejected with 'RangeError'" }); + }); - describe("with an Error having message 'foo bar'", () => { - beforeEach(() => { - promise = Promise.reject(new Error("foo bar")); - }); + describe('.not.rejectedWith(TypeError)', () => { + shouldPass(() => promise.should.not.be.rejectedWith(TypeError)); + }); + }); - describe(".rejectedWith('foo')", () => { - shouldPass(() => promise.should.be.rejectedWith("foo")); - }); + describe("with a RangeError having a message 'foo bar'", () => { + beforeEach(() => { + promise = Promise.reject(new RangeError('foo bar')); + }); + + describe(".rejectedWith(RangeError, 'foo')", () => { + shouldPass(() => promise.should.be.rejectedWith(RangeError, 'foo')); + }); + + describe(".not.rejectedWith(RangeError, 'foo')", () => { + shouldFail({ + op: () => promise.should.not.be.rejectedWith(RangeError, 'foo'), + message: + "not to be rejected with 'RangeError' but it was rejected with 'RangeError: foo bar'" + }); + }); + + describe('.rejectedWith(RangeError, /bar/)', () => { + shouldPass(() => promise.should.be.rejectedWith(RangeError, /bar/)); + }); + + describe('.not.rejectedWith(RangeError, /bar/)', () => { + shouldFail({ + op: () => promise.should.not.be.rejectedWith(RangeError, /bar/), + message: + "not to be rejected with 'RangeError' but it was rejected with 'RangeError: foo bar'" + }); + }); + + describe(".rejectedWith(RangeError, 'quux')", () => { + shouldFail({ + op: () => promise.should.be.rejectedWith(RangeError, 'quux'), + message: + "to be rejected with an error including 'quux' but got 'foo bar'" + }); + }); + describe('.rejectedWith(RangeError, /quux/)', () => { + shouldFail({ + op: () => promise.should.be.rejectedWith(RangeError, /quux/), + message: + "to be rejected with an error matching /quux/ but got 'foo bar'" + }); + }); + + describe(".rejectedWith(TypeError, 'foo')", () => { + shouldFail({ + op: () => promise.should.be.rejectedWith(TypeError, 'foo'), + message: + "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'" + }); + }); + describe('.rejectedWith(TypeError, /bar/)', () => { + shouldFail({ + op: () => promise.should.be.rejectedWith(TypeError, /bar/), + message: + "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'" + }); + }); + + describe(".rejectedWith(TypeError, 'quux')", () => { + shouldFail({ + op: () => promise.should.be.rejectedWith(TypeError, 'quux'), + message: + "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'" + }); + }); + describe('.rejectedWith(TypeError, /quux/)', () => { + shouldFail({ + op: () => promise.should.be.rejectedWith(TypeError, /quux/), + message: + "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'" + }); + }); + + describe(".not.rejectedWith(RangeError, 'foo')", () => { + shouldFail({ + op: () => promise.should.not.be.rejectedWith(RangeError, 'foo'), + message: + "not to be rejected with 'RangeError' but it was rejected with 'RangeError: foo bar'" + }); + }); + describe('.not.rejectedWith(RangeError, /bar/)', () => { + shouldFail({ + op: () => promise.should.not.be.rejectedWith(RangeError, /bar/), + message: + "not to be rejected with 'RangeError' but it was rejected with 'RangeError: foo bar'" + }); + }); + + describe(".not.rejectedWith(RangeError, 'quux')", () => { + shouldPass(() => + promise.should.not.be.rejectedWith(RangeError, 'quux') + ); + }); + describe('.not.rejectedWith(RangeError, /quux/)', () => { + shouldPass(() => + promise.should.not.be.rejectedWith(RangeError, /quux/) + ); + }); + describe(".not.rejectedWith(TypeError, 'foo')", () => { + shouldPass(() => promise.should.not.be.rejectedWith(TypeError, 'foo')); + }); + describe('.not.rejectedWith(TypeError, /bar/)', () => { + shouldPass(() => promise.should.not.be.rejectedWith(TypeError, /bar/)); + }); + describe(".not.rejectedWith(TypeError, 'quux')", () => { + shouldPass(() => promise.should.not.be.rejectedWith(TypeError, 'quux')); + }); + describe('.not.rejectedWith(TypeError, /quux/)', () => { + shouldPass(() => promise.should.not.be.rejectedWith(TypeError, /quux/)); + }); + describe(".rejectedWith(RangeError, 'foo', custom)", () => { + shouldPass(() => + promise.should.be.rejectedWith(RangeError, 'foo', custom) + ); + }); + + describe(".not.rejectedWith(RangeError, 'foo', custom)", () => { + shouldFail({ + op: () => + promise.should.not.be.rejectedWith(RangeError, 'foo', custom), + message: custom + }); + }); + + describe('.rejectedWith(RangeError, /bar/, custom)', () => { + shouldPass(() => + promise.should.be.rejectedWith(RangeError, /bar/, custom) + ); + }); + + describe('.not.rejectedWith(RangeError, /bar/, custom)', () => { + shouldFail({ + op: () => + promise.should.not.be.rejectedWith(RangeError, /bar/, custom), + message: custom + }); + }); + + describe(".rejectedWith(RangeError, 'quux', custom)", () => { + shouldFail({ + op: () => promise.should.be.rejectedWith(RangeError, 'quux', custom), + message: custom + }); + }); + + describe(".not.rejectedWith(TypeError, 'quux', custom)", () => { + shouldPass(() => + promise.should.not.be.rejectedWith(TypeError, 'quux', custom) + ); + }); + + describe('.rejectedWith(RangeError, /quux/, custom)', () => { + shouldFail({ + op: () => promise.should.be.rejectedWith(RangeError, /quux/, custom), + message: custom + }); + }); + + describe('.not.rejectedWith(TypeError, /quux/, custom)', () => { + shouldPass(() => + promise.should.not.be.rejectedWith(TypeError, /quux/, custom) + ); + }); + + describe('.rejectedWith(RangeError, undefined, custom)', () => { + shouldPass(() => + promise.should.be.rejectedWith(RangeError, undefined, custom) + ); + }); + + describe('.not.rejectedWith(RangeError, undefined, custom)', () => { + shouldFail({ + op: () => + promise.should.not.be.rejectedWith(RangeError, undefined, custom), + message: custom + }); + }); + + describe('.rejectedWith(TypeError, undefined, custom)', () => { + shouldFail({ + op: () => + promise.should.be.rejectedWith(TypeError, undefined, custom), + message: custom + }); + }); + + describe('.not.rejectedWith(TypeError, undefined, custom)', () => { + shouldPass(() => + promise.should.not.be.rejectedWith(TypeError, undefined, custom) + ); + }); + }); - describe(".not.rejectedWith('foo')", () => { - shouldFail({ - op: () => promise.should.not.be.rejectedWith("foo"), - message: "not to be rejected with an error including 'foo'" - }); - }); + describe('.should.notify(done)', () => { + it('should fail the test with the original error', (done) => { + promise.should.notify(assertingDoneFactory(done)); + }); + }); + }); - describe(".rejectedWith(/bar/)", () => { - shouldPass(() => promise.should.be.rejectedWith(/bar/)); - }); + describe('.should.notify with chaining (GH-3)', () => { + describe('the original promise is fulfilled', () => { + beforeEach(() => { + promise = Promise.resolve(); + }); - describe(".not.rejectedWith(/bar/)", () => { - shouldFail({ - op: () => promise.should.not.be.rejectedWith(/bar/), - message: "not to be rejected with an error matching /bar/" - }); - }); - - describe(".rejectedWith('quux')", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith("quux"), - message: "to be rejected with an error including 'quux' but got 'foo bar'" - }); - }); - - describe(".not.rejectedWith('quux')", () => { - shouldPass(() => promise.should.be.not.rejectedWith("quux")); - }); - - describe(".rejectedWith(/quux/)", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(/quux/), - message: "to be rejected with an error matching /quux/ but got 'foo bar'" - }); - }); - - describe(".not.rejectedWith(/quux/)", () => { - shouldPass(() => promise.should.not.be.rejectedWith(/quux/)); - }); - - // Chai 3.5.0 never interprets the 2nd paramter to - // expect(fn).to.throw(a, b) as a custom error - // message. This is what we are testing here. - describe(".rejectedWith('foo', custom)", () => { - shouldPass(() => promise.should.be.rejectedWith("foo", custom)); - }); - - describe(".not.rejectedWith('foo', custom)", () => { - shouldFail({ - op: () => promise.should.not.be.rejectedWith("foo", custom), - message: "not to be rejected with an error including 'foo'", - notMessage: custom - }); - }); - - describe(".rejectedWith(/bar/, custom)", () => { - shouldPass(() => promise.should.be.rejectedWith(/bar/, custom)); - }); - - describe(".not.rejectedWith(/bar/, custom)", () => { - shouldFail({ - op: () => promise.should.not.be.rejectedWith(/bar/), - message: "not to be rejected with an error matching /bar/", - notMessage: custom - }); - }); - }); - - describe("with a RangeError", () => { - beforeEach(() => { - promise = Promise.reject(new RangeError()); - }); - - describe(".rejectedWith(RangeError)", () => { - shouldPass(() => promise.should.be.rejectedWith(RangeError)); - }); - - describe(".not.rejectedWith(RangeError)", () => { - shouldFail({ - op: () => promise.should.not.be.rejectedWith(RangeError), - message: "not to be rejected with 'RangeError' but it was rejected with 'RangeError'" - }); - }); - - describe(".rejectedWith(TypeError)", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(TypeError), - message: "to be rejected with 'TypeError' but it was rejected with 'RangeError'" - }); - }); - - // Case for issue #64. - describe(".rejectedWith(Array)", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(Array), - message: "to be rejected with 'Array' but it was rejected with 'RangeError'" - }); - }); - - describe(".not.rejectedWith(TypeError)", () => { - shouldPass(() => promise.should.not.be.rejectedWith(TypeError)); - }); - }); - - describe("with a RangeError having a message 'foo bar'", () => { - beforeEach(() => { - promise = Promise.reject(new RangeError("foo bar")); - }); - - describe(".rejectedWith(RangeError, 'foo')", () => { - shouldPass(() => promise.should.be.rejectedWith(RangeError, "foo")); - }); - - describe(".not.rejectedWith(RangeError, 'foo')", () => { - shouldFail({ - op: () => promise.should.not.be.rejectedWith(RangeError, "foo"), - message: "not to be rejected with 'RangeError' but it was rejected with 'RangeError: foo bar'" - }); - }); - - describe(".rejectedWith(RangeError, /bar/)", () => { - shouldPass(() => promise.should.be.rejectedWith(RangeError, /bar/)); - }); - - describe(".not.rejectedWith(RangeError, /bar/)", () => { - shouldFail({ - op: () => promise.should.not.be.rejectedWith(RangeError, /bar/), - message: "not to be rejected with 'RangeError' but it was rejected with 'RangeError: foo bar'" - }); - }); - - describe(".rejectedWith(RangeError, 'quux')", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(RangeError, "quux"), - message: "to be rejected with an error including 'quux' but got 'foo bar'" - }); - }); - describe(".rejectedWith(RangeError, /quux/)", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(RangeError, /quux/), - message: "to be rejected with an error matching /quux/ but got 'foo bar'" - }); - }); - - describe(".rejectedWith(TypeError, 'foo')", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(TypeError, "foo"), - message: "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'" - }); - }); - describe(".rejectedWith(TypeError, /bar/)", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(TypeError, /bar/), - message: "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'" - }); - }); - - describe(".rejectedWith(TypeError, 'quux')", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(TypeError, "quux"), - message: "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'" - }); - }); - describe(".rejectedWith(TypeError, /quux/)", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(TypeError, /quux/), - message: "to be rejected with 'TypeError' but it was rejected with 'RangeError: foo bar'" - }); - }); - - describe(".not.rejectedWith(RangeError, 'foo')", () => { - shouldFail({ - op: () => promise.should.not.be.rejectedWith(RangeError, "foo"), - message: "not to be rejected with 'RangeError' but it was rejected with 'RangeError: foo bar'" - }); - }); - describe(".not.rejectedWith(RangeError, /bar/)", () => { - shouldFail({ - op: () => promise.should.not.be.rejectedWith(RangeError, /bar/), - message: "not to be rejected with 'RangeError' but it was rejected with 'RangeError: foo bar'" - }); - }); - - describe(".not.rejectedWith(RangeError, 'quux')", () => { - shouldPass(() => promise.should.not.be.rejectedWith(RangeError, "quux")); - }); - describe(".not.rejectedWith(RangeError, /quux/)", () => { - shouldPass(() => promise.should.not.be.rejectedWith(RangeError, /quux/)); - }); - describe(".not.rejectedWith(TypeError, 'foo')", () => { - shouldPass(() => promise.should.not.be.rejectedWith(TypeError, "foo")); - }); - describe(".not.rejectedWith(TypeError, /bar/)", () => { - shouldPass(() => promise.should.not.be.rejectedWith(TypeError, /bar/)); - }); - describe(".not.rejectedWith(TypeError, 'quux')", () => { - shouldPass(() => promise.should.not.be.rejectedWith(TypeError, "quux")); - }); - describe(".not.rejectedWith(TypeError, /quux/)", () => { - shouldPass(() => promise.should.not.be.rejectedWith(TypeError, /quux/)); - }); - describe(".rejectedWith(RangeError, 'foo', custom)", () => { - shouldPass(() => promise.should.be.rejectedWith(RangeError, "foo", custom)); - }); - - describe(".not.rejectedWith(RangeError, 'foo', custom)", () => { - shouldFail({ - op: () => promise.should.not.be.rejectedWith(RangeError, "foo", custom), - message: custom - }); - }); - - describe(".rejectedWith(RangeError, /bar/, custom)", () => { - shouldPass(() => promise.should.be.rejectedWith(RangeError, /bar/, custom)); - }); - - describe(".not.rejectedWith(RangeError, /bar/, custom)", () => { - shouldFail({ - op: () => promise.should.not.be.rejectedWith(RangeError, /bar/, custom), - message: custom - }); - }); - - describe(".rejectedWith(RangeError, 'quux', custom)", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(RangeError, "quux", custom), - message: custom - }); - }); - - describe(".not.rejectedWith(TypeError, 'quux', custom)", () => { - shouldPass(() => promise.should.not.be.rejectedWith(TypeError, "quux", custom)); - }); - - describe(".rejectedWith(RangeError, /quux/, custom)", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(RangeError, /quux/, custom), - message: custom - }); - }); - - describe(".not.rejectedWith(TypeError, /quux/, custom)", () => { - shouldPass(() => promise.should.not.be.rejectedWith(TypeError, /quux/, custom)); - }); - - describe(".rejectedWith(RangeError, undefined, custom)", () => { - shouldPass(() => promise.should.be.rejectedWith(RangeError, undefined, custom)); - }); - - describe(".not.rejectedWith(RangeError, undefined, custom)", () => { - shouldFail({ - op: () => promise.should.not.be.rejectedWith(RangeError, undefined, custom), - message: custom - }); - }); - - describe(".rejectedWith(TypeError, undefined, custom)", () => { - shouldFail({ - op: () => promise.should.be.rejectedWith(TypeError, undefined, custom), - message: custom - }); - }); - - describe(".not.rejectedWith(TypeError, undefined, custom)", () => { - shouldPass(() => promise.should.not.be.rejectedWith(TypeError, undefined, custom)); - }); - }); - - describe(".should.notify(done)", () => { - it("should fail the test with the original error", done => { - promise.should.notify(assertingDoneFactory(done)); - }); - }); - }); - - describe(".should.notify with chaining (GH-3)", () => { - describe("the original promise is fulfilled", () => { - beforeEach(() => { - promise = Promise.resolve(); - }); - - describe("and the follow-up promise is fulfilled", () => { - beforeEach(() => { - promise = promise.then(() => { /* Do nothing */ }); - }); - - it("should pass the test", done => { - promise.should.notify(done); - }); - }); - - describe("but the follow-up promise is rejected", () => { - beforeEach(() => { - promise = promise.then(() => { - throw error; - }); - }); - - it("should fail the test with the error from the follow-up promise", done => { - promise.should.notify(assertingDoneFactory(done)); - }); - }); - }); - - describe("the original promise is rejected", () => { - beforeEach(() => { - promise = Promise.reject(error); - }); - - describe("but the follow-up promise is fulfilled", () => { - beforeEach(() => { - promise = promise.then(() => { /* Do nothing */ }); - }); - - it("should fail the test with the error from the original promise", done => { - promise.should.notify(assertingDoneFactory(done)); - }); - }); - - describe("and the follow-up promise is rejected", () => { - beforeEach(() => { - promise = promise.then(() => { - throw new Error("follow up"); - }); - }); + describe('and the follow-up promise is fulfilled', () => { + beforeEach(() => { + promise = promise.then(() => { + /* Do nothing */ + }); + }); - it("should fail the test with the error from the original promise", done => { - promise.should.notify(assertingDoneFactory(done)); - }); - }); + it('should pass the test', (done) => { + promise.should.notify(done); }); - }); + }); - describe("Using with non-thenables", () => { - describe("A number", () => { - const number = 5; + describe('but the follow-up promise is rejected', () => { + beforeEach(() => { + promise = promise.then(() => { + throw error; + }); + }); - it("should fail for .fulfilled", () => { - expect(() => number.should.be.fulfilled).to.throw(TypeError, "not a thenable"); - }); - it("should fail for .rejected", () => { - expect(() => number.should.be.rejected).to.throw(TypeError, "not a thenable"); - }); - it("should fail for .become", () => { - expect(() => number.should.become(5)).to.throw(TypeError, "not a thenable"); - }); - it("should fail for .eventually", () => { - expect(() => number.should.eventually.equal(5)).to.throw(TypeError, "not a thenable"); - }); - it("should fail for .notify", () => { - expect(() => number.should.notify(() => { /* Doesn't matter */ })) - .to.throw(TypeError, "not a thenable"); - }); + it('should fail the test with the error from the follow-up promise', (done) => { + promise.should.notify(assertingDoneFactory(done)); }); + }); }); - describe("Using together with other Chai as Promised asserters", () => { - describe(".fulfilled.and.eventually.equal(42)", () => { - shouldPass(() => Promise.resolve(42).should.be.fulfilled.and.eventually.equal(42)); + describe('the original promise is rejected', () => { + beforeEach(() => { + promise = Promise.reject(error); + }); + + describe('but the follow-up promise is fulfilled', () => { + beforeEach(() => { + promise = promise.then(() => { + /* Do nothing */ + }); }); - describe(".fulfilled.and.rejected", () => { - shouldFail({ - op: () => Promise.resolve(42).should.be.fulfilled.and.rejected, - message: "to be rejected but it was fulfilled with 42" - }); + + it('should fail the test with the error from the original promise', (done) => { + promise.should.notify(assertingDoneFactory(done)); }); + }); - describe(".rejected.and.eventually.equal(42)", () => { - shouldPass(() => Promise.reject(42).should.be.rejected.and.eventually.equal(42)); + describe('and the follow-up promise is rejected', () => { + beforeEach(() => { + promise = promise.then(() => { + throw new Error('follow up'); + }); }); - describe(".rejected.and.become(42)", () => { - shouldPass(() => Promise.reject(42).should.be.rejected.and.become(42)); + + it('should fail the test with the error from the original promise', (done) => { + promise.should.notify(assertingDoneFactory(done)); }); + }); }); + }); + + describe('Using with non-thenables', () => { + describe('A number', () => { + const number = 5; + + it('should fail for .fulfilled', () => { + expect(() => number.should.be.fulfilled).to.throw( + TypeError, + 'not a thenable' + ); + }); + it('should fail for .rejected', () => { + expect(() => number.should.be.rejected).to.throw( + TypeError, + 'not a thenable' + ); + }); + it('should fail for .become', () => { + expect(() => number.should.become(5)).to.throw( + TypeError, + 'not a thenable' + ); + }); + it('should fail for .eventually', () => { + expect(() => number.should.eventually.equal(5)).to.throw( + TypeError, + 'not a thenable' + ); + }); + it('should fail for .notify', () => { + expect(() => + number.should.notify(() => { + /* Doesn't matter */ + }) + ).to.throw(TypeError, 'not a thenable'); + }); + }); + }); - describe("With promises that only become rejected later (GH-24)", () => { - it("should wait for them", done => { - let reject; - const rejectedLaterPromise = new Promise((_, r) => { - reject = r; - }); - rejectedLaterPromise.should.be.rejectedWith("error message").and.notify(done); - - setTimeout(() => reject(new Error("error message")), 100); - }); + describe('Using together with other Chai as Promised asserters', () => { + describe('.fulfilled.and.eventually.equal(42)', () => { + shouldPass(() => + Promise.resolve(42).should.be.fulfilled.and.eventually.equal(42) + ); + }); + describe('.fulfilled.and.rejected', () => { + shouldFail({ + op: () => Promise.resolve(42).should.be.fulfilled.and.rejected, + message: 'to be rejected but it was fulfilled with 42' + }); }); - describe("`rejectedWith` with non-`Error` rejection reasons (GH-33)", () => { - shouldPass(() => Promise.reject(42).should.be.rejectedWith(42)); + describe('.rejected.and.eventually.equal(42)', () => { + shouldPass(() => + Promise.reject(42).should.be.rejected.and.eventually.equal(42) + ); }); + describe('.rejected.and.become(42)', () => { + shouldPass(() => Promise.reject(42).should.be.rejected.and.become(42)); + }); + }); + + describe('With promises that only become rejected later (GH-24)', () => { + it('should wait for them', (done) => { + let reject; + const rejectedLaterPromise = new Promise((_, r) => { + reject = r; + }); + rejectedLaterPromise.should.be + .rejectedWith('error message') + .and.notify(done); + + setTimeout(() => reject(new Error('error message')), 100); + }); + }); + + describe('`rejectedWith` with non-`Error` rejection reasons (GH-33)', () => { + shouldPass(() => Promise.reject(42).should.be.rejectedWith(42)); + }); }); diff --git a/test/support/common.js b/test/support/common.js index 6b0862b..beb5dea 100644 --- a/test/support/common.js +++ b/test/support/common.js @@ -1,40 +1,64 @@ -"use strict"; +'use strict'; -exports.shouldPass = promiseProducer => { - it("should return a fulfilled promise", done => { - promiseProducer().then( - () => done(), - reason => done(new Error(`Expected promise to be fulfilled but it was rejected with ${reason.stack}`)) - ); - }); +exports.shouldPass = (promiseProducer) => { + it('should return a fulfilled promise', (done) => { + promiseProducer().then( + () => done(), + (reason) => + done( + new Error( + `Expected promise to be fulfilled but it was rejected with ${reason.stack}` + ) + ) + ); + }); }; -exports.shouldFail = options => { - const promiseProducer = options.op; - const desiredMessageSubstring = options.message; - const nonDesiredMessageSubstring = options.notMessage; +exports.shouldFail = (options) => { + const promiseProducer = options.op; + const desiredMessageSubstring = options.message; + const nonDesiredMessageSubstring = options.notMessage; - it("should return a promise rejected with an assertion error", done => { - promiseProducer().then( - () => { - throw new Error("Expected promise to be rejected with an assertion error, but it was fulfilled"); - }, - reason => { - if (Object(reason) !== reason || reason.constructor.name !== "AssertionError") { - throw new Error(`Expected promise to be rejected with an AssertionError but it was rejected ` + - `with ${reason}`); - } + it('should return a promise rejected with an assertion error', (done) => { + promiseProducer() + .then( + () => { + throw new Error( + 'Expected promise to be rejected with an assertion error, but it was fulfilled' + ); + }, + (reason) => { + if ( + Object(reason) !== reason || + reason.constructor.name !== 'AssertionError' + ) { + throw new Error( + `Expected promise to be rejected with an AssertionError but it was rejected ` + + `with ${reason}` + ); + } - if (desiredMessageSubstring && !reason.message.includes(desiredMessageSubstring)) { - throw new Error(`Expected promise to be rejected with an AssertionError containing ` + - `"${desiredMessageSubstring}" but it was rejected with ${reason}`); - } + if ( + desiredMessageSubstring && + !reason.message.includes(desiredMessageSubstring) + ) { + throw new Error( + `Expected promise to be rejected with an AssertionError containing ` + + `"${desiredMessageSubstring}" but it was rejected with ${reason}` + ); + } - if (nonDesiredMessageSubstring && reason.message.includes(nonDesiredMessageSubstring)) { - throw new Error(`Expected promise to be rejected with an AssertionError not containing ` + - `"${nonDesiredMessageSubstring}" but it was rejected with ${reason}`); - } - } - ).then(done, done); - }); + if ( + nonDesiredMessageSubstring && + reason.message.includes(nonDesiredMessageSubstring) + ) { + throw new Error( + `Expected promise to be rejected with an AssertionError not containing ` + + `"${nonDesiredMessageSubstring}" but it was rejected with ${reason}` + ); + } + } + ) + .then(done, done); + }); }; diff --git a/test/support/setup.js b/test/support/setup.js index 1a7d5f6..87c99f4 100644 --- a/test/support/setup.js +++ b/test/support/setup.js @@ -1,12 +1,12 @@ -const chai = require("chai"); -const chaiAsPromised = require("../../lib/chai-as-promised.js"); +const chai = require('chai'); +const chaiAsPromised = require('../../lib/chai-as-promised.js'); chai.should(); chai.use(chaiAsPromised); -process.on("unhandledRejection", () => { - // Do nothing; we test these all the time. +process.on('unhandledRejection', () => { + // Do nothing; we test these all the time. }); -process.on("rejectionHandled", () => { - // Do nothing; we test these all the time. +process.on('rejectionHandled', () => { + // Do nothing; we test these all the time. });