-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
sync.test.js
55 lines (48 loc) · 1.28 KB
/
sync.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
'use strict';
function toCustomMatch(callback, expectation) {
const actual = callback();
if (actual !== expectation) {
return {
message: () => `Expected "${expectation}" but got "${actual}"`,
pass: false,
};
} else {
return {pass: true};
}
}
expect.extend({
toCustomMatch,
});
describe('Custom matcher', () => {
it('passes', () => {
// This expectation should pass
expect(() => 'foo').toCustomMatch('foo');
});
it('fails', () => {
expect(() => {
// This expectation should fail,
// Which is why it's wrapped in a .toThrow() block.
expect(() => 'foo').toCustomMatch('bar');
}).toThrow();
});
it('preserves error stack', () => {
const foo = () => bar();
const bar = () => baz();
const baz = () => {
throw Error('qux');
};
// This expecation fails due to an error we throw (intentionally)
// The stack trace should point to the line that throws the error though,
// Not to the line that calls the matcher.
expect(() => {
foo();
}).toCustomMatch('test');
});
});