forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasymmetric_matchers.test.js
More file actions
170 lines (147 loc) · 4.9 KB
/
asymmetric_matchers.test.js
File metadata and controls
170 lines (147 loc) · 4.9 KB
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
/**
* 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.
*
*/
'use strict';
const jestExpect = require('../');
const {
any,
anything,
arrayContaining,
objectContaining,
stringContaining,
stringMatching,
} = require('../asymmetric_matchers');
test('Any.asymmetricMatch()', () => {
const Thing = function() {};
[
any(String).asymmetricMatch('jest'),
any(Number).asymmetricMatch(1),
any(Function).asymmetricMatch(() => {}),
any(Boolean).asymmetricMatch(true),
any(Object).asymmetricMatch({}),
any(Array).asymmetricMatch([]),
any(Thing).asymmetricMatch(new Thing()),
].forEach(test => {
jestExpect(test).toBe(true);
});
});
test('Any.toAsymmetricMatcher()', () => {
jestExpect(any(Number).toAsymmetricMatcher()).toBe('Any<Number>');
});
test('Any throws when called with empty constructor', () => {
jestExpect(() => any()).toThrow();
});
test('Anything matches any type', () => {
[
anything().asymmetricMatch('jest'),
anything().asymmetricMatch(1),
anything().asymmetricMatch(() => {}),
anything().asymmetricMatch(true),
anything().asymmetricMatch({}),
anything().asymmetricMatch([]),
].forEach(test => {
jestExpect(test).toBe(true);
});
});
test('Anything does not match null and undefined', () => {
[
anything().asymmetricMatch(null),
anything().asymmetricMatch(undefined),
].forEach(test => {
jestExpect(test).toBe(false);
});
});
test('Anything.toAsymmetricMatcher()', () => {
jestExpect(anything().toAsymmetricMatcher()).toBe('Anything');
});
test('ArrayContaining matches', () => {
[
arrayContaining([]).asymmetricMatch('jest'),
arrayContaining(['foo']).asymmetricMatch(['foo']),
arrayContaining(['foo']).asymmetricMatch(['foo', 'bar']),
arrayContaining([]).asymmetricMatch({}),
].forEach(test => {
jestExpect(test).toEqual(true);
});
});
test('ArrayContaining does not match', () => {
jestExpect(arrayContaining(['foo']).asymmetricMatch(['bar'])).toBe(false);
});
test('ArrayContaining throws for non-arrays', () => {
jestExpect(() => {
arrayContaining('foo').asymmetricMatch([]);
}).toThrow();
});
test('ObjectContaining matches', () => {
[
objectContaining({}).asymmetricMatch('jest'),
objectContaining({foo: 'foo'}).asymmetricMatch({foo: 'foo', jest: 'jest'}),
objectContaining({foo: undefined}).asymmetricMatch({foo: undefined}),
objectContaining({first: objectContaining({second: {}})}).asymmetricMatch({
first: {second: {}},
}),
].forEach(test => {
jestExpect(test).toEqual(true);
});
});
test('ObjectContaining does not match', () => {
[
objectContaining({foo: 'foo'}).asymmetricMatch({bar: 'bar'}),
objectContaining({foo: 'foo'}).asymmetricMatch({foo: 'foox'}),
objectContaining({foo: undefined}).asymmetricMatch({}),
].forEach(test => {
jestExpect(test).toEqual(false);
});
});
test('ObjectContaining matches defined properties', () => {
const definedPropertyObject = {};
Object.defineProperty(definedPropertyObject, 'foo', {get: () => 'bar'});
jestExpect(
objectContaining({foo: 'bar'}).asymmetricMatch(definedPropertyObject),
).toBe(true);
});
test('ObjectContaining matches prototype properties', () => {
const prototypeObject = {foo: 'bar'};
let obj;
if (Object.create) {
obj = Object.create(prototypeObject);
} else {
function Foo() {}
Foo.prototype = prototypeObject;
Foo.prototype.constructor = Foo;
obj = new Foo();
}
jestExpect(objectContaining({foo: 'bar'}).asymmetricMatch(obj)).toBe(true);
});
test('ObjectContaining throws for non-objects', () => {
jestExpect(() => objectContaining(1337).asymmetricMatch()).toThrow();
});
test('StringContaining matches string against string', () => {
jestExpect(stringContaining('en*').asymmetricMatch('queen*')).toBe(true);
jestExpect(stringContaining('en').asymmetricMatch('queue')).toBe(false);
jestExpect(stringContaining('en').asymmetricMatch({})).toBe(false);
});
test('StringContaining throws for non-strings', () => {
jestExpect(() => {
stringContaining([1]).asymmetricMatch('queen');
}).toThrow();
});
test('StringMatching matches string against regexp', () => {
jestExpect(stringMatching(/en/).asymmetricMatch('queen')).toBe(true);
jestExpect(stringMatching(/en/).asymmetricMatch('queue')).toBe(false);
jestExpect(stringMatching(/en/).asymmetricMatch({})).toBe(false);
});
test('StringMatching matches string against string', () => {
jestExpect(stringMatching('en').asymmetricMatch('queen')).toBe(true);
jestExpect(stringMatching('en').asymmetricMatch('queue')).toBe(false);
jestExpect(stringMatching('en').asymmetricMatch({})).toBe(false);
});
test('StringMatching throws for non-strings and non-regexps', () => {
jestExpect(() => {
stringMatching([1]).asymmetricMatch('queen');
}).toThrow();
});