-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #340 from unexpectedjs/feature/to-be-fulfilled-wit…
…h-a-value-exhaustively-satisfying Add 'to be fulfilled with a value satisfying' and 'to be rejected with error satisfying'
- Loading branch information
Showing
14 changed files
with
1,002 additions
and
345 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
documentation/assertions/Promise/to-be-fulfilled-with-value-satisfying.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
Asserts that a promise is fulfilled with a specific value: | ||
|
||
```javascript#async:true | ||
var promiseThatWillBeFulfilledWithAValue = expect.promise(function (resolve, reject) { | ||
setTimeout(function () { | ||
resolve({ | ||
foo: 'bar' | ||
}); | ||
}, 1); | ||
}); | ||
return expect( | ||
promiseThatWillBeFulfilledWithAValue, | ||
'to be fulfilled with value satisfying', | ||
{ foo: 'bar' } | ||
); | ||
``` | ||
|
||
The expected value will be matched against the value with | ||
[to satisfy](/assertions/any/to-satisfy/) semantics, so you can pass any of the | ||
values supported by `to satisfy`: | ||
|
||
```javascript#async:true | ||
return expect( | ||
expect.promise.resolve('abc'), | ||
'to be fulfilled with value satisfying', | ||
/b/ | ||
); | ||
``` | ||
|
||
You get a nice diff if the assertion fails: | ||
|
||
```javascript#async:true | ||
return expect( | ||
expect.promise.resolve('abc'), | ||
'to be fulfilled with value satisfying', | ||
'def' | ||
); | ||
``` | ||
|
||
```output | ||
expected Promise (fulfilled) => 'abc' to be fulfilled with value satisfying 'def' | ||
expected 'abc' to equal 'def' | ||
-abc | ||
+def | ||
``` | ||
|
||
You can use the `exhaustively` flag to use strict | ||
[to satisfy](/assertions/any/to-satisfy/) semantics: | ||
|
||
```javascript#async:true | ||
return expect( | ||
expect.promise.resolve({ | ||
foo: 'foo', | ||
bar: 'bar' | ||
}), | ||
'to be fulfilled with value exhaustively satisfying', | ||
{ | ||
foo: 'foo' | ||
} | ||
); | ||
``` | ||
|
||
```output | ||
expected Promise (fulfilled) => { foo: 'foo', bar: 'bar' } | ||
to be fulfilled with value exhaustively satisfying { foo: 'foo' } | ||
expected { foo: 'foo', bar: 'bar' } to exhaustively satisfy { foo: 'foo' } | ||
{ | ||
foo: 'foo', | ||
bar: 'bar' // should be removed | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
Asserts that a promise is fulfilled with a specific value: | ||
|
||
```javascript#async:true | ||
var promiseThatWillBeFulfilledWithAValue = expect.promise(function (resolve, reject) { | ||
setTimeout(function () { | ||
resolve('abc'); | ||
}, 1); | ||
}); | ||
return expect(promiseThatWillBeFulfilledWithAValue, 'to be fulfilled with', 'abc'); | ||
``` | ||
|
||
The expected value will be matched against the value with | ||
[to satisfy](/assertions/any/to-satisfy/) semantics, so you can pass any of the | ||
values supported by `to satisfy`: | ||
|
||
```javascript#async:true | ||
return expect(expect.promise.resolve('abc'), 'to be fulfilled with', /b/); | ||
``` | ||
|
||
You get a nice diff if the assertion fails: | ||
|
||
```javascript#async:true | ||
return expect(expect.promise.resolve('abc'), 'to be fulfilled with', 'def'); | ||
``` | ||
|
||
```output | ||
expected Promise (fulfilled) => 'abc' to be fulfilled with 'def' | ||
expected 'abc' to equal 'def' | ||
-abc | ||
+def | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
documentation/assertions/Promise/to-be-rejected-with-error-satisfying.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
Asserts that a promise is rejected with a specific reason (error): | ||
|
||
```javascript#async:true | ||
var promiseThatWillBeRejectedWithAReason = expect.promise(function (resolve, reject) { | ||
setTimeout(function () { | ||
reject(new Error('Oh dear')); | ||
}, 10); | ||
}); | ||
return expect( | ||
promiseThatWillBeRejectedWithAReason, | ||
'to be rejected with error satisfying', | ||
new Error('Oh dear') | ||
); | ||
``` | ||
|
||
The expected reason will be matched against the rejection reason with | ||
[to satisfy](/assertions/any/to-satisfy/) semantics, so you can pass any of the | ||
values supported by `to satisfy`: | ||
|
||
|
||
```javascript#async:true | ||
var promiseThatWillBeRejectedWithAReason = expect.promise(function (resolve, reject) { | ||
setTimeout(function () { | ||
reject(new Error('Oh dear')); | ||
}, 10); | ||
}); | ||
return expect( | ||
promiseThatWillBeRejectedWithAReason, | ||
'to be rejected with error satisfying', | ||
/dear/ | ||
); | ||
``` | ||
|
||
You get a nice diff if the assertion fails: | ||
|
||
```javascript#async:true | ||
var promiseThatWillBeRejectedWithAReason = expect.promise(function (resolve, reject) { | ||
setTimeout(function () { | ||
reject(new Error('Oh dear')); | ||
}, 10); | ||
}); | ||
return expect( | ||
promiseThatWillBeRejectedWithAReason, | ||
'to be rejected with error satisfying', | ||
new Error('bugger') | ||
); | ||
``` | ||
|
||
```output | ||
expected Promise (rejected) => Error('Oh dear') | ||
to be rejected with error satisfying Error('bugger') | ||
expected Error('Oh dear') to satisfy Error('bugger') | ||
Error({ | ||
message: 'Oh dear' // should equal 'bugger' | ||
// | ||
// -Oh dear | ||
// +bugger | ||
}) | ||
``` | ||
|
||
You can use the `exhaustively` flag to use strict | ||
[to satisfy](/assertions/any/to-satisfy/) semantics: | ||
|
||
```javascript#async:true | ||
var error = new Error('Oh dear'); | ||
error.data = { foo: 'bar' }; | ||
return expect( | ||
expect.promise.reject(error), | ||
'to be rejected with error exhaustively satisfying', | ||
new Error('Oh dear') | ||
); | ||
``` | ||
|
||
```output | ||
expected Promise (rejected) => Error({ message: 'Oh dear', data: { foo: 'bar' } }) | ||
to be rejected with error exhaustively satisfying Error('Oh dear') | ||
expected Error({ message: 'Oh dear', data: { foo: 'bar' } }) | ||
to exhaustively satisfy Error('Oh dear') | ||
Error({ | ||
message: 'Oh dear', | ||
data: { foo: 'bar' } // should be removed | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
Asserts that a promise is rejected with a specific reason (error): | ||
|
||
```javascript#async:true | ||
var promiseThatWillBeRejectedWithAReason = expect.promise(function (resolve, reject) { | ||
setTimeout(function () { | ||
reject(new Error('Oh dear')); | ||
}, 10); | ||
}); | ||
return expect(promiseThatWillBeRejectedWithAReason, 'to be rejected with', new Error('Oh dear')); | ||
``` | ||
|
||
The expected reason will be matched against the rejection reason with | ||
[to satisfy](/assertions/any/to-satisfy/) semantics, so you can pass any of the | ||
values supported by `to satisfy`: | ||
|
||
|
||
```javascript#async:true | ||
var promiseThatWillBeRejectedWithAReason = expect.promise(function (resolve, reject) { | ||
setTimeout(function () { | ||
reject(new Error('Oh dear')); | ||
}, 10); | ||
}); | ||
return expect(promiseThatWillBeRejectedWithAReason, 'to be rejected with', /dear/); | ||
``` | ||
|
||
You get a nice diff if the assertion fails: | ||
|
||
```javascript#async:true | ||
var promiseThatWillBeRejectedWithAReason = expect.promise(function (resolve, reject) { | ||
setTimeout(function () { | ||
reject(new Error('Oh dear')); | ||
}, 10); | ||
}); | ||
return expect(promiseThatWillBeRejectedWithAReason, 'to be rejected with', new Error('bugger')); | ||
``` | ||
|
||
```output | ||
expected Promise (rejected) => Error('Oh dear') to be rejected with Error('bugger') | ||
expected Error('Oh dear') to satisfy Error('bugger') | ||
Error({ | ||
message: 'Oh dear' // should equal 'bugger' | ||
// | ||
// -Oh dear | ||
// +bugger | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.