/
extend.test.ts
186 lines (159 loc) · 5.13 KB
/
extend.test.ts
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* 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.
*
*/
import {equals, iterableEquality, subsetEquality} from '@jest/expect-utils';
import {alignedAnsiStyleSerializer} from '@jest/test-utils';
import * as matcherUtils from 'jest-matcher-utils';
import jestExpect from '../';
expect.addSnapshotSerializer(alignedAnsiStyleSerializer);
jestExpect.extend({
toBeDivisibleBy(actual: number, expected: number) {
const pass = actual % expected === 0;
const message = pass
? () =>
`expected ${this.utils.printReceived(
actual,
)} not to be divisible by ${expected}`
: () =>
`expected ${this.utils.printReceived(
actual,
)} to be divisible by ${expected}`;
return {message, pass};
},
toBeSymbol(actual: symbol, expected: symbol) {
const pass = actual === expected;
const message = () =>
`expected ${actual.toString()} to be Symbol ${expected.toString()}`;
return {message, pass};
},
toBeWithinRange(actual: number, floor: number, ceiling: number) {
const pass = actual >= floor && actual <= ceiling;
const message = pass
? () =>
`expected ${this.utils.printReceived(
actual,
)} not to be within range ${floor} - ${ceiling}`
: () =>
`expected ${this.utils.printReceived(
actual,
)} to be within range ${floor} - ${ceiling}`;
return {message, pass};
},
});
it('is available globally when matcher is unary', () => {
jestExpect(15).toBeDivisibleBy(5);
jestExpect(15).toBeDivisibleBy(3);
jestExpect(15).not.toBeDivisibleBy(6);
jestExpect(() =>
jestExpect(15).toBeDivisibleBy(2),
).toThrowErrorMatchingSnapshot();
});
it('is available globally when matcher is variadic', () => {
jestExpect(15).toBeWithinRange(10, 20);
jestExpect(15).not.toBeWithinRange(6);
jestExpect(() =>
jestExpect(15).toBeWithinRange(1, 3),
).toThrowErrorMatchingSnapshot();
});
it('exposes matcherUtils in context', () => {
jestExpect.extend({
_shouldNotError(_actual: unknown, _expected: unknown) {
const pass = this.equals(
this.utils,
Object.assign(matcherUtils, {
iterableEquality,
subsetEquality,
}),
);
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: unknown) {
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 unary 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 unary 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();
});
it('defines asymmetric variadic matchers', () => {
expect(() =>
jestExpect({value: 2}).toEqual({value: jestExpect.toBeWithinRange(1, 3)}),
).not.toThrow();
expect(() =>
jestExpect({value: 3}).toEqual({value: jestExpect.toBeWithinRange(4, 11)}),
).toThrowErrorMatchingSnapshot();
});
it('defines asymmetric variadic matchers that can be prefixed by not', () => {
expect(() =>
jestExpect({value: 2}).toEqual({
value: jestExpect.not.toBeWithinRange(1, 3),
}),
).toThrowErrorMatchingSnapshot();
expect(() =>
jestExpect({value: 3}).toEqual({
value: jestExpect.not.toBeWithinRange(5, 7),
}),
).not.toThrow();
});
it('prints the Symbol into the error message', () => {
const foo = Symbol('foo');
const bar = Symbol('bar');
expect(() =>
jestExpect({a: foo}).toEqual({
a: jestExpect.toBeSymbol(bar),
}),
).toThrowErrorMatchingSnapshot();
});
it('allows overriding existing extension', () => {
jestExpect.extend({
toAllowOverridingExistingMatcher(_expected: unknown) {
return {pass: _expected === 'bar'};
},
});
jestExpect('foo').not.toAllowOverridingExistingMatcher();
jestExpect.extend({
toAllowOverridingExistingMatcher(_expected: unknown) {
return {pass: _expected === 'foo'};
},
});
jestExpect('foo').toAllowOverridingExistingMatcher();
});