chai-as-promised 4.2.0 → 4.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +19 -0
- package/README.md +34 -3
- package/lib/chai-as-promised.js +7 -0
- package/package.json +1 -1
package/LICENSE.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright © 2012–2015 Domenic Denicola <d@domenic.me>
|
2
|
+
|
3
|
+
This work is free. You can redistribute it and/or modify it under the
|
4
|
+
terms of the Do What The Fuck You Want To Public License, Version 2,
|
5
|
+
as published by Sam Hocevar. See below for more details.
|
6
|
+
|
7
|
+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
8
|
+
Version 2, December 2004
|
9
|
+
|
10
|
+
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
|
11
|
+
|
12
|
+
Everyone is permitted to copy and distribute verbatim or modified
|
13
|
+
copies of this license document, and changing it is allowed as long
|
14
|
+
as the name is changed.
|
15
|
+
|
16
|
+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
17
|
+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
18
|
+
|
19
|
+
0. You just DO WHAT THE FUCK YOU WANT TO.
|
package/README.md
CHANGED
@@ -45,13 +45,13 @@ existing Chai assertion into one that acts on a promise:
|
|
45
45
|
(2 + 2).should.equal(4);
|
46
46
|
|
47
47
|
// becomes
|
48
|
-
return
|
48
|
+
return Promise.resolve(2 + 2).should.eventually.equal(4);
|
49
49
|
|
50
50
|
|
51
51
|
expect({ foo: "bar" }).to.have.property("foo");
|
52
52
|
|
53
53
|
// becomes
|
54
|
-
return expect(
|
54
|
+
return expect(Promise.resolve({ foo: "bar" })).to.eventually.have.property("foo");
|
55
55
|
```
|
56
56
|
|
57
57
|
There are also a few promise-specific extensions (with the usual `expect` equivalents also available):
|
@@ -73,7 +73,7 @@ any existing Chai assertion to be used on a promise:
|
|
73
73
|
assert.equal(2 + 2, 4, "This had better be true");
|
74
74
|
|
75
75
|
// becomes
|
76
|
-
return assert.eventually.equal(
|
76
|
+
return assert.eventually.equal(Promise.resolve(2 + 2), 4, "This had better be true, eventually");
|
77
77
|
```
|
78
78
|
|
79
79
|
And there are, of course, promise-specific extensions:
|
@@ -120,6 +120,37 @@ chaiAsPromised.transferPromiseness = function (assertion, promise) {
|
|
120
120
|
};
|
121
121
|
```
|
122
122
|
|
123
|
+
### Transforming Arguments to the Asserters
|
124
|
+
|
125
|
+
Another advanced customization hook Chai as Promised allows is if you want to transform the arguments to the
|
126
|
+
asserters, possibly asynchronously. Here is a toy example:
|
127
|
+
|
128
|
+
```js
|
129
|
+
chaiAsPromised.transformAsserterArgs = function (args) {
|
130
|
+
return args.map(function (x) { return x + 1; });
|
131
|
+
}
|
132
|
+
|
133
|
+
Promise.resolve(2).should.eventually.equal(2); // will now fail!
|
134
|
+
Promise.resolve(2).should.eventually.equal(3); // will now pass!
|
135
|
+
```
|
136
|
+
|
137
|
+
The transform can even be asynchronous, returning a promise for an array instead of an array directly. An example
|
138
|
+
of that might be using `Promise.all` so that an array of promises becomes a promise for an array. If you do that,
|
139
|
+
then you can compare promises against other promises using the asserters:
|
140
|
+
|
141
|
+
```js
|
142
|
+
// This will normally fail, since within() only works on numbers.
|
143
|
+
Promise.resolve(2).should.eventually.be.within(Promise.resolve(1), Promise.resolve(6));
|
144
|
+
|
145
|
+
chaiAsPromised.transformAsserterArgs = function (args) {
|
146
|
+
return Promise.all(args);
|
147
|
+
};
|
148
|
+
|
149
|
+
// But now it will pass, since we transformed the array of promises for numbers into
|
150
|
+
// (a promise for) an array of numbers
|
151
|
+
Promise.resolve(2).should.eventually.be.within(Promise.resolve(1), Promise.resolve(6));
|
152
|
+
```
|
153
|
+
|
123
154
|
### Compatibility
|
124
155
|
|
125
156
|
Chai as Promised is compatible with all promises following the [Promises/A+ specification][spec]. Notably, jQuery's
|
package/lib/chai-as-promised.js
CHANGED
@@ -26,6 +26,10 @@
|
|
26
26
|
assertion.then = promise.then.bind(promise);
|
27
27
|
};
|
28
28
|
|
29
|
+
chaiAsPromised.transformAsserterArgs = function (values) {
|
30
|
+
return values;
|
31
|
+
};
|
32
|
+
|
29
33
|
function chaiAsPromised(chai, utils) {
|
30
34
|
var Assertion = chai.Assertion;
|
31
35
|
var assert = chai.assert;
|
@@ -300,6 +304,9 @@
|
|
300
304
|
// just the base Chai code that we get to via the short-circuit above.
|
301
305
|
assertion._obj = value;
|
302
306
|
utils.flag(assertion, "eventually", false);
|
307
|
+
|
308
|
+
return args ? chaiAsPromised.transformAsserterArgs(args) : args;
|
309
|
+
}).then(function (args) {
|
303
310
|
asserter.apply(assertion, args);
|
304
311
|
|
305
312
|
// Because asserters, for example `property`, can change the value of `_obj` (i.e. change the "object"
|