Skip to content

Commit a757ed5

Browse files
authored
feat(jasmine): add matcher transforms that have no direct equivalent in jest, but can be expressed in other ways (#614)
added support for toBePositiveInfinity, toBeNegativeInfinity, toHaveSize, toHaveBeenCalledOnceWith, toHaveBeenCalledBefore, toHaveSpyInteractions
1 parent 72dd0a8 commit a757ed5

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

src/transformers/expect.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,14 @@ test('maps expect matchers', () => {
9696
expect(stuff).toBeAn(Array);
9797
expect(new Stuff).toBeAn(Stuff, 'Message');
9898
99+
expect([1, 2]).toHaveSize(2);
100+
99101
expect(stuff).toNotBeA(Number);
100102
expect(stuff).toNotBeAn(Array);
101103
104+
expect(foo).toBePositiveInfinity();
105+
expect(foo).toBeNegativeInfinity();
106+
102107
expect(stuff).toMatch({foo: 'bar'});
103108
expect(stuff).toMatch({foo: 'bar'}, 'message');
104109
expect(stuff).toMatch('a string');
@@ -113,6 +118,10 @@ test('maps expect matchers', () => {
113118
expect(stuff).toNotHaveBeenCalled();
114119
expect(stuff).toNotHaveBeenCalled('msg');
115120
expect(stuff).toHaveBeenCalledWith('foo', 'bar');
121+
122+
expect(mySpy).toHaveBeenCalledOnceWith('foo', 'bar');
123+
expect(mySpy).toHaveBeenCalledBefore(myOtherSpy);
124+
expect(mySpyObj).toHaveSpyInteractions()
116125
});
117126
`,
118127
`
@@ -152,9 +161,14 @@ test('maps expect matchers', () => {
152161
expect(stuff).toBeInstanceOf(Array);
153162
expect(new Stuff).toBeInstanceOf(Stuff);
154163
164+
expect([1, 2]).toHaveLength(2);
165+
155166
expect(stuff).not.toBeInstanceOf(Number);
156167
expect(stuff).not.toBeInstanceOf(Array);
157168
169+
expect(foo).toBe(Infinity);
170+
expect(foo).toBe(-Infinity);
171+
158172
expect(stuff).toMatchObject({foo: 'bar'});
159173
expect(stuff).toMatchObject({foo: 'bar'});
160174
expect(stuff).toMatch('a string');
@@ -169,6 +183,10 @@ test('maps expect matchers', () => {
169183
expect(stuff).not.toHaveBeenCalled();
170184
expect(stuff).not.toHaveBeenCalled();
171185
expect(stuff).toHaveBeenCalledWith('foo', 'bar');
186+
187+
expect(mySpy.mock.calls).toEqual([['foo', 'bar']]);
188+
expect(Math.min(...mySpy.mock.invocationOrder)).toBeLessThan(Math.min(...myOtherSpy.mock.invocationOrder));
189+
expect(Object.values(mySpyObj).some(spy => spy.mock?.calls?.length)).toBe(true)
172190
});
173191
`
174192
)

src/transformers/expect.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,83 @@ ${keys}.forEach(e => {
219219
matcher.name = 'toBe'
220220
break
221221
}
222+
223+
case 'toBePositiveInfinity': {
224+
matcherArgs[0] = j.literal(Infinity)
225+
matcher.name = 'toBe'
226+
break
227+
}
228+
229+
case 'toBeNegativeInfinity': {
230+
matcherArgs[0] = j.literal(-Infinity)
231+
matcher.name = 'toBe'
232+
break
233+
}
234+
235+
case 'toHaveSize': {
236+
matcher.name = 'toHaveLength'
237+
break
238+
}
239+
240+
case 'toHaveBeenCalledOnceWith': {
241+
expectArgs[0] = j.memberExpression(
242+
j.memberExpression(expectArgs[0], j.identifier('mock')),
243+
j.identifier('calls')
244+
)
245+
matcher.name = 'toEqual'
246+
247+
matcherNode.arguments = [j.arrayExpression([j.arrayExpression(matcherArgs)])]
248+
break
249+
}
250+
251+
case 'toHaveBeenCalledBefore': {
252+
const getMinInvocationOrder = (spy) =>
253+
j.callExpression(
254+
j.memberExpression(j.identifier('Math'), j.identifier('min')),
255+
[
256+
j.spreadElement(
257+
j.memberExpression(
258+
j.memberExpression(spy, j.identifier('mock')),
259+
j.identifier('invocationOrder')
260+
)
261+
),
262+
]
263+
)
264+
265+
expectArgs[0] = getMinInvocationOrder(expectArgs[0])
266+
matcherArgs[0] = getMinInvocationOrder(matcherArgs[0])
267+
matcher.name = 'toBeLessThan'
268+
break
269+
}
270+
271+
case 'toHaveSpyInteractions': {
272+
expectArgs[0] = j.callExpression(
273+
j.memberExpression(
274+
j.callExpression(
275+
j.memberExpression(j.identifier('Object'), j.identifier('values')),
276+
[expectArgs[0]]
277+
),
278+
j.identifier('some')
279+
),
280+
[
281+
j.arrowFunctionExpression(
282+
[j.identifier('spy')],
283+
j.optionalMemberExpression(
284+
j.optionalMemberExpression(
285+
j.memberExpression(j.identifier('spy'), j.identifier('mock')),
286+
j.identifier('calls'),
287+
false,
288+
true
289+
),
290+
j.identifier('length')
291+
)
292+
),
293+
]
294+
)
295+
matcherArgs[0] = j.literal(true)
296+
matcher.name = 'toBe'
297+
break
298+
}
222299
}
223300

224301
balanceMatcherNodeArguments(matcherNode, matcher, path)

0 commit comments

Comments
 (0)