forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjasmine-matcher_spec.ts
More file actions
379 lines (356 loc) · 13.8 KB
/
jasmine-matcher_spec.ts
File metadata and controls
379 lines (356 loc) · 13.8 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { expectTransformation } from '../test-helpers';
describe('Jasmine to Vitest Transformer - transformAsymmetricMatchers', () => {
const testCases = [
{
description: 'should transform jasmine.any(String) to expect.any(String)',
input: `expect(foo).toEqual(jasmine.any(String));`,
expected: `expect(foo).toEqual(expect.any(String));`,
},
{
description: 'should transform jasmine.objectContaining(...) to expect.objectContaining(...)',
input: `expect(foo).toEqual(jasmine.objectContaining({ bar: 'baz' }));`,
expected: `expect(foo).toEqual(expect.objectContaining({ bar: 'baz' }));`,
},
{
description: 'should transform jasmine.anything() to expect.anything()',
input: `expect(foo).toEqual(jasmine.anything());`,
expected: `expect(foo).toEqual(expect.anything());`,
},
{
description: 'should transform jasmine.stringMatching(...) to expect.stringMatching(...)',
input: `expect(foo).toEqual(jasmine.stringMatching(/some-pattern/));`,
expected: `expect(foo).toEqual(expect.stringMatching(/some-pattern/));`,
},
{
description: 'should transform jasmine.arrayContaining(...) to expect.arrayContaining(...)',
input: `expect(foo).toEqual(jasmine.arrayContaining(['a']));`,
expected: `expect(foo).toEqual(expect.arrayContaining(['a']));`,
},
{
description: 'should transform jasmine.stringContaining(...) to expect.stringContaining(...)',
input: `expect(foo).toEqual(jasmine.stringContaining('substring'));`,
expected: `expect(foo).toEqual(expect.stringContaining('substring'));`,
},
];
testCases.forEach(({ description, input, expected }) => {
it(description, async () => {
await expectTransformation(input, expected);
});
});
});
describe('transformExpectAsync', () => {
const testCases = [
{
description: 'should transform expectAsync(...).toBeResolved()',
input: `await expectAsync(myPromise).toBeResolved();`,
expected: `await expect(myPromise).resolves.not.toThrow();`,
},
{
description: 'should transform expectAsync(...).toBeResolvedTo(value)',
input: `await expectAsync(myPromise).toBeResolvedTo(42);`,
expected: `await expect(myPromise).resolves.toEqual(42);`,
},
{
description: 'should transform expectAsync(...).toBeRejected()',
input: `await expectAsync(myPromise).toBeRejected();`,
expected: `await expect(myPromise).rejects.toThrow();`,
},
{
description: 'should transform expectAsync(...).toBeRejectedWith(error)',
input: `await expectAsync(myPromise).toBeRejectedWith('Error');`,
expected: `await expect(myPromise).rejects.toEqual('Error');`,
},
{
description: 'should transform expectAsync(...).toBeRejectedWithError(ErrorClass, message)',
input: `await expectAsync(myPromise).toBeRejectedWithError(TypeError, 'Failed');`,
expected: `await expect(myPromise).rejects.toThrowError(TypeError, 'Failed');`,
},
{
description: 'should add a TODO for an unknown expectAsync matcher',
input: `await expectAsync(myPromise).toBeSomethingElse();`,
expected: `
// TODO: vitest-migration: Unsupported expectAsync matcher ".toBeSomethingElse()" found. Please migrate this manually.
await expectAsync(myPromise).toBeSomethingElse();
`,
},
{
description: 'should add a specific TODO for toBePending',
input: `await expectAsync(myPromise).toBePending();`,
/* eslint-disable max-len */
expected: `
// TODO: vitest-migration: Unsupported matcher ".toBePending()" found. Vitest does not have a direct equivalent. Please migrate this manually, for example by using \`Promise.race\` to check if the promise settles within a short timeout.
await expectAsync(myPromise).toBePending();
`,
/* eslint-enable max-len */
},
];
testCases.forEach(({ description, input, expected }) => {
it(description, async () => {
await expectTransformation(input, expected);
});
});
});
describe('transformCalledOnceWith', () => {
const testCases = [
{
description: 'should transform toHaveBeenCalledOnceWith(...) into two separate calls',
input: `expect(mySpy).toHaveBeenCalledOnceWith('foo', 'bar');`,
expected: `
expect(mySpy).toHaveBeenCalledTimes(1);
expect(mySpy).toHaveBeenCalledWith('foo', 'bar');
`,
},
];
testCases.forEach(({ description, input, expected }) => {
it(description, async () => {
await expectTransformation(input, expected);
});
});
});
describe('transformSyntacticSugarMatchers', () => {
const testCases = [
{
description: 'should transform toBeTrue() to toBe(true)',
input: `expect(value).toBeTrue();`,
expected: `expect(value).toBe(true);`,
},
{
description: 'should transform toBeFalse() to toBe(false)',
input: `expect(value).toBeFalse();`,
expected: `expect(value).toBe(false);`,
},
{
description: 'should transform toBePositiveInfinity() to toBe(Infinity)',
input: `expect(value).toBePositiveInfinity();`,
expected: `expect(value).toBe(Infinity);`,
},
{
description: 'should transform toBeNegativeInfinity() to toBe(-Infinity)',
input: `expect(value).toBeNegativeInfinity();`,
expected: `expect(value).toBe(-Infinity);`,
},
{
description: 'should transform toHaveSize(number) to toHaveLength(number)',
input: `expect(myArray).toHaveSize(3);`,
expected: `expect(myArray).toHaveLength(3);`,
},
{
description: 'should add a TODO for toThrowMatching',
input: `expect(() => {}).toThrowMatching((e) => e.message === 'foo');`,
expected: `// TODO: vitest-migration: Unsupported matcher ".toThrowMatching()" found. Please migrate this manually. See: https://vitest.dev/api/expect.html#tothrowerror
expect(() => {}).toThrowMatching((e) => e.message === 'foo');`,
},
{
description: 'should add a TODO for toHaveSpyInteractions',
input: `expect(mySpyObj).toHaveSpyInteractions();`,
// eslint-disable-next-line max-len
expected: `// TODO: vitest-migration: Unsupported matcher ".toHaveSpyInteractions()" found. Please migrate this manually by checking the \`mock.calls.length\` of the individual spies.
expect(mySpyObj).toHaveSpyInteractions();`,
},
];
testCases.forEach(({ description, input, expected }) => {
it(description, async () => {
await expectTransformation(input, expected);
});
});
});
describe('transformComplexMatchers', () => {
const testCases = [
{
description: 'should transform toEqual(jasmine.truthy()) to toBeTruthy()',
input: `expect(value).toEqual(jasmine.truthy());`,
expected: `expect(value).toBeTruthy();`,
},
{
description: 'should transform toEqual(jasmine.falsy()) to toBeFalsy()',
input: `expect(value).toEqual(jasmine.falsy());`,
expected: `expect(value).toBeFalsy();`,
},
{
description: 'should transform toEqual(jasmine.empty()) to toHaveLength(0)',
input: `expect([]).toEqual(jasmine.empty());`,
expected: `expect([]).toHaveLength(0);`,
},
{
description: 'should transform not.toEqual(jasmine.empty()) to not.toHaveLength(0)',
input: `expect([1]).not.toEqual(jasmine.empty());`,
expected: `expect([1]).not.toHaveLength(0);`,
},
{
description: 'should transform toEqual(jasmine.notEmpty()) to not.toHaveLength(0)',
input: `expect([1]).toEqual(jasmine.notEmpty());`,
expected: `expect([1]).not.toHaveLength(0);`,
},
{
description: 'should transform not.toEqual(jasmine.notEmpty()) to toHaveLength(0)',
input: `expect([]).not.toEqual(jasmine.notEmpty());`,
expected: `expect([]).toHaveLength(0);`,
},
{
description: 'should transform toEqual(jasmine.is()) to toBe()',
input: `expect(value).toEqual(jasmine.is(otherValue));`,
expected: `expect(value).toBe(otherValue);`,
},
];
testCases.forEach(({ description, input, expected }) => {
it(description, async () => {
await expectTransformation(input, expected);
});
});
});
describe('transformArrayWithExactContents', () => {
const testCases = [
{
description: 'should transform toEqual(jasmine.arrayWithExactContents()) into two calls',
input: `expect(myArray).toEqual(jasmine.arrayWithExactContents(['a', 'b']));`,
/* eslint-disable max-len */
expected: `
// TODO: vitest-migration: Verify this matches strict array content (multiset equality). Vitest's arrayContaining is a subset check.
expect(myArray).toHaveLength(2);
expect(myArray).toEqual(expect.arrayContaining(['a', 'b']));
`,
/* eslint-enable max-len */
},
{
description:
'should transform toEqual(jasmine.arrayWithExactContents()) with asymmetric matchers',
input: `expect(myArray).toEqual(jasmine.arrayWithExactContents([jasmine.any(Number), 'a']));`,
/* eslint-disable max-len */
expected: `
// TODO: vitest-migration: Verify this matches strict array content (multiset equality). Vitest's arrayContaining is a subset check.
expect(myArray).toHaveLength(2);
expect(myArray).toEqual(expect.arrayContaining([expect.any(Number), 'a']));
`,
/* eslint-enable max-len */
},
{
description:
'should add a TODO for toEqual(jasmine.arrayWithExactContents()) with a variable',
input: `expect(myArray).toEqual(jasmine.arrayWithExactContents(someOtherArray));`,
expected: `
// TODO: vitest-migration: Cannot transform jasmine.arrayWithExactContents with a dynamic variable. Please migrate this manually.
expect(myArray).toEqual(jasmine.arrayWithExactContents(someOtherArray));
`,
},
];
testCases.forEach(({ description, input, expected }) => {
it(description, async () => {
await expectTransformation(input, expected);
});
});
});
describe('transformExpectNothing', () => {
const testCases = [
{
description: 'should remove expect().nothing() and add a comment',
input: `
it('should be a passing test', () => {
expect().nothing();
});
`,
/* eslint-disable max-len */
expected: `
it('should be a passing test', () => {
// TODO: vitest-migration: expect().nothing() has been removed because it is redundant in Vitest. Tests without assertions pass by default.
// expect().nothing();
});
`,
/* eslint-enable max-len */
},
];
testCases.forEach(({ description, input, expected }) => {
it(description, async () => {
await expectTransformation(input, expected);
});
});
});
describe('transformWithContext', () => {
const testCases = [
{
description: 'should transform .withContext() to an expect message',
input: `expect(value).withContext('It should be true').toBe(true);`,
expected: `expect(value, 'It should be true').toBe(true);`,
},
{
description: 'should handle chained matchers',
input: `expect(value).withContext('It should not be false').not.toBe(false);`,
expected: `expect(value, 'It should not be false').not.toBe(false);`,
},
{
description: 'should handle .withContext() with no arguments by removing it',
input: `expect(value).withContext().toBe(true);`,
expected: `expect(value).toBe(true);`,
},
];
testCases.forEach(({ description, input, expected }) => {
it(description, async () => {
await expectTransformation(input, expected);
});
});
});
describe('transformtoHaveBeenCalledBefore', () => {
const testCases = [
{
description: 'should transform toHaveBeenCalledBefore',
input: `expect(spyA).toHaveBeenCalledBefore(spyB);`,
// eslint-disable-next-line max-len
expected: `expect(Math.min(...vi.mocked(spyA).mock.invocationCallOrder)).toBeLessThan(Math.min(...vi.mocked(spyB).mock.invocationCallOrder));`,
},
{
description: 'should transform not.toHaveBeenCalledBefore',
input: `expect(spyA).not.toHaveBeenCalledBefore(spyB);`,
// eslint-disable-next-line max-len
expected: `expect(Math.min(...vi.mocked(spyA).mock.invocationCallOrder)).toBeGreaterThanOrEqual(Math.min(...vi.mocked(spyB).mock.invocationCallOrder));`,
},
];
testCases.forEach(({ description, input, expected }) => {
it(description, async () => {
await expectTransformation(input, expected);
});
});
});
describe('transformToHaveClass', () => {
const testCases = [
{
description: 'should transform toHaveClass',
input: `expect(element).toHaveClass('my-class');`,
expected: `expect(element.classList.contains('my-class')).toBe(true);`,
},
{
description: 'should transform not.toHaveClass',
input: `expect(element).not.toHaveClass('my-class');`,
expected: `expect(element.classList.contains('my-class')).toBe(false);`,
},
];
testCases.forEach(({ description, input, expected }) => {
it(description, async () => {
await expectTransformation(input, expected);
});
});
});
describe('transformToBeNullish', () => {
const testCases = [
{
description: 'should transform toBeNullish',
input: `expect(element).toBeNullish();`,
expected: `expect(element == null).toBe(true);`,
},
{
description: 'should transform not.toBeNullish',
input: `expect(element).not.toBeNullish();`,
expected: `expect(element == null).toBe(false);`,
},
];
testCases.forEach(({ description, input, expected }) => {
it(description, async () => {
await expectTransformation(input, expected);
});
});
});