-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
extend.test.js
90 lines (76 loc) · 2.44 KB
/
extend.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
*
*/
const matcherUtils = require('jest-matcher-utils');
const {equals} = require('../jasmine_utils');
const jestExpect = require('../');
jestExpect.extend({
toBeDivisibleBy(actual, expected) {
const pass = actual % expected === 0;
const message = pass
? () => `expected ${actual} not to be divisible by ${expected}`
: () => `expected ${actual} to be divisible by ${expected}`;
return {message, pass};
},
});
it('is available globally', () => {
jestExpect(15).toBeDivisibleBy(5);
jestExpect(15).toBeDivisibleBy(3);
jestExpect(15).not.toBeDivisibleBy(6);
jestExpect(() =>
jestExpect(15).toBeDivisibleBy(2),
).toThrowErrorMatchingSnapshot();
});
it('exposes matcherUtils in context', () => {
jestExpect.extend({
_shouldNotError(actual, expected) {
const pass = this.utils === matcherUtils;
const message = pass
? () => `expected this.utils to be defined in an extend call`
: () => `expected this.utils not to be defined in an extend call`;
return {message, pass};
},
});
jestExpect()._shouldNotError();
});
it('is ok if there is no message specified', () => {
jestExpect.extend({
toFailWithoutMessage(expected) {
return {pass: false};
},
});
expect(() =>
jestExpect(true).toFailWithoutMessage(),
).toThrowErrorMatchingSnapshot();
});
it('exposes an equality function to custom matchers', () => {
// jestExpect and expect share the same global state
expect.assertions(3);
jestExpect.extend({
toBeOne() {
expect(this.equals).toBe(equals);
return {pass: !!this.equals(1, 1)};
},
});
expect(() => jestExpect().toBeOne()).not.toThrow();
});
it('defines asymmetric matchers', () => {
expect(() =>
jestExpect({value: 2}).toEqual({value: jestExpect.toBeDivisibleBy(2)}),
).not.toThrow();
expect(() =>
jestExpect({value: 3}).toEqual({value: jestExpect.toBeDivisibleBy(2)}),
).toThrowErrorMatchingSnapshot();
});
it('defines asymmetric matchers that can be prefixed by not', () => {
expect(() =>
jestExpect({value: 2}).toEqual({value: jestExpect.not.toBeDivisibleBy(2)}),
).toThrowErrorMatchingSnapshot();
expect(() =>
jestExpect({value: 3}).toEqual({value: jestExpect.not.toBeDivisibleBy(2)}),
).not.toThrow();
});