chai-as-promised
Advanced tools
Comparing version 4.2.0 to 4.3.0
@@ -29,2 +29,6 @@ (function () { | ||
chaiAsPromised.transformAsserterArgs = function (values) { | ||
return values; | ||
}; | ||
function chaiAsPromised(chai, utils) { | ||
@@ -304,2 +308,5 @@ var Assertion = chai.Assertion; | ||
utils.flag(assertion, "eventually", false); | ||
return args ? chaiAsPromised.transformAsserterArgs(args) : args; | ||
}).then(function (args) { | ||
asserter.apply(assertion, args); | ||
@@ -306,0 +313,0 @@ |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "4.2.0", | ||
"version": "4.3.0", | ||
"author": "Domenic Denicola <d@domenic.me> (https://domenic.me)", | ||
@@ -14,0 +14,0 @@ "license": "WTFPL", |
@@ -48,3 +48,3 @@ <a href="http://promisesaplus.com/"> | ||
// becomes | ||
return promiseFor(2 + 2).should.eventually.equal(4); | ||
return Promise.resolve(2 + 2).should.eventually.equal(4); | ||
@@ -55,3 +55,3 @@ | ||
// becomes | ||
return expect(promiseFor({ foo: "bar" })).to.eventually.have.property("foo"); | ||
return expect(Promise.resolve({ foo: "bar" })).to.eventually.have.property("foo"); | ||
``` | ||
@@ -78,3 +78,3 @@ | ||
// becomes | ||
return assert.eventually.equal(promiseFor(2 + 2), 4, "This had better be true, eventually"); | ||
return assert.eventually.equal(Promise.resolve(2 + 2), 4, "This had better be true, eventually"); | ||
``` | ||
@@ -126,2 +126,33 @@ | ||
### Transforming Arguments to the Asserters | ||
Another advanced customization hook Chai as Promised allows is if you want to transform the arguments to the | ||
asserters, possibly asynchronously. Here is a toy example: | ||
```js | ||
chaiAsPromised.transformAsserterArgs = function (args) { | ||
return args.map(function (x) { return x + 1; }); | ||
} | ||
Promise.resolve(2).should.eventually.equal(2); // will now fail! | ||
Promise.resolve(2).should.eventually.equal(3); // will now pass! | ||
``` | ||
The transform can even be asynchronous, returning a promise for an array instead of an array directly. An example | ||
of that might be using `Promise.all` so that an array of promises becomes a promise for an array. If you do that, | ||
then you can compare promises against other promises using the asserters: | ||
```js | ||
// This will normally fail, since within() only works on numbers. | ||
Promise.resolve(2).should.eventually.be.within(Promise.resolve(1), Promise.resolve(6)); | ||
chaiAsPromised.transformAsserterArgs = function (args) { | ||
return Promise.all(args); | ||
}; | ||
// But now it will pass, since we transformed the array of promises for numbers into | ||
// (a promise for) an array of numbers | ||
Promise.resolve(2).should.eventually.be.within(Promise.resolve(1), Promise.resolve(6)); | ||
``` | ||
### Compatibility | ||
@@ -128,0 +159,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28653
4
319
272