forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjasmine-spy_spec.ts
More file actions
257 lines (248 loc) · 11 KB
/
Copy pathjasmine-spy_spec.ts
File metadata and controls
257 lines (248 loc) · 11 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
/**
* @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', () => {
describe('transformSpies', () => {
const testCases = [
{
description: 'should transform spyOn(object, "method") to vi.spyOn(object, "method")',
input: `spyOn(service, 'myMethod');`,
expected: `vi.spyOn(service, 'myMethod');`,
},
{
description: 'should transform .and.returnValue(...) to .mockReturnValue(...)',
input: `spyOn(service, 'myMethod').and.returnValue(42);`,
expected: `vi.spyOn(service, 'myMethod').mockReturnValue(42);`,
},
{
description: 'should transform .and.returnValues() to chained .mockReturnValueOnce() calls',
input: `spyOn(service, 'myMethod').and.returnValues('a', 'b', 'c');`,
expected: `vi.spyOn(service, 'myMethod').mockReturnValueOnce('a').mockReturnValueOnce('b').mockReturnValueOnce('c');`,
},
{
description: 'should transform .and.callFake(...) to .mockImplementation(...)',
input: `spyOn(service, 'myMethod').and.callFake(() => 'fake');`,
expected: `vi.spyOn(service, 'myMethod').mockImplementation(() => 'fake');`,
},
{
description: 'should remove .and.callThrough()',
input: `spyOn(service, 'myMethod').and.callThrough();`,
expected: `vi.spyOn(service, 'myMethod');`,
},
{
description: 'should transform jasmine.createSpy("name") to vi.fn()',
input: `const mySpy = jasmine.createSpy('mySpy');`,
expected: `const mySpy = vi.fn();`,
},
{
description: 'should transform jasmine.createSpy("name", fn) to vi.fn(fn)',
input: `const mySpy = jasmine.createSpy('mySpy', () => 'foo');`,
expected: `const mySpy = vi.fn(() => 'foo');`,
},
{
description: 'should transform spyOnProperty(object, "prop") to vi.spyOn(object, "prop")',
input: `spyOnProperty(service, 'myProp');`,
expected: `vi.spyOn(service, 'myProp');`,
},
{
description: 'should transform .and.stub() to .mockImplementation(() => {})',
input: `spyOn(service, 'myMethod').and.stub();`,
expected: `vi.spyOn(service, 'myMethod').mockImplementation(() => {});`,
},
{
description: 'should add a TODO for jasmine.spyOnAllFunctions(object)',
input: `jasmine.spyOnAllFunctions(myObject);`,
// eslint-disable-next-line max-len
expected: `// TODO: vitest-migration: Vitest does not have a direct equivalent for jasmine.spyOnAllFunctions(). Please spy on individual methods manually using vi.spyOn(). See: https://vitest.dev/api/vi.html#vi-spyon
jasmine.spyOnAllFunctions(myObject);
`,
},
{
description: 'should handle chained calls on jasmine.createSpy()',
input: `const mySpy = jasmine.createSpy('mySpy').and.returnValue(true);`,
expected: `const mySpy = vi.fn().mockReturnValue(true);`,
},
{
description: 'should handle .and.returnValues() with no arguments',
input: `spyOn(service, 'myMethod').and.returnValues();`,
expected: `vi.spyOn(service, 'myMethod');`,
},
{
description:
'should transform .and.throwError("message") to .mockImplementation(() => { throw new Error("message") })',
input: `spyOn(service, 'myMethod').and.throwError('Something went wrong');`,
expected: `vi.spyOn(service, 'myMethod').mockImplementation(() => { throw new Error('Something went wrong') });`,
},
{
description:
'should transform .and.throwError(new Error("message")) to .mockImplementation(() => { throw new Error("message") })',
input: `spyOn(service, 'myMethod').and.throwError(new Error('Custom Error'));`,
expected: `vi.spyOn(service, 'myMethod').mockImplementation(() => { throw new Error('Custom Error') });`,
},
{
description: 'should transform .and.resolveTo(value) to .mockResolvedValue(value)',
input: `spyOn(service, 'myMethod').and.resolveTo('some value');`,
expected: `vi.spyOn(service, 'myMethod').mockResolvedValue('some value');`,
},
{
description: 'should transform .and.rejectWith(error) to .mockRejectedValue(error)',
input: `spyOn(service, 'myMethod').and.rejectWith('some error');`,
expected: `vi.spyOn(service, 'myMethod').mockRejectedValue('some error');`,
},
{
description: 'should add a TODO for an unsupported spy strategy',
input: `spyOn(service, 'myMethod').and.unknownStrategy();`,
// eslint-disable-next-line max-len
expected: `// TODO: vitest-migration: Unsupported spy strategy ".and.unknownStrategy()" found. Please migrate this manually. See: https://vitest.dev/api/mocked.html#mock
vi.spyOn(service, 'myMethod').and.unknownStrategy();`,
},
];
testCases.forEach(({ description, input, expected }) => {
it(description, async () => {
await expectTransformation(input, expected);
});
});
});
describe('transformCreateSpyObj', () => {
const testCases = [
{
description: 'should transform jasmine.createSpyObj with an array of methods',
input: `const myService = jasmine.createSpyObj('MyService', ['methodA', 'methodB']);`,
expected: `const myService = {
methodA: vi.fn().mockName("MyService.methodA"),
methodB: vi.fn().mockName("MyService.methodB"),
};`,
},
{
description: 'should add a TODO if the second argument is not a literal',
input: `const myService = jasmine.createSpyObj('MyService', methodNames);`,
expected: `
// TODO: vitest-migration: Cannot transform jasmine.createSpyObj with a dynamic variable. Please migrate this manually. See: https://vitest.dev/api/vi.html#vi-fn
const myService = jasmine.createSpyObj('MyService', methodNames);
`,
},
{
description: 'should transform jasmine.createSpyObj with an object of return values',
input: `const myService = jasmine.createSpyObj('MyService', { methodA: 'foo', methodB: 42 });`,
expected: `const myService = {
methodA: vi.fn().mockName("MyService.methodA").mockReturnValue('foo'),
methodB: vi.fn().mockName("MyService.methodB").mockReturnValue(42),
};`,
},
{
description:
'should transform jasmine.createSpyObj with an object of return values containing an asymmetric matcher',
input: `const myService = jasmine.createSpyObj('MyService', { methodA: jasmine.any(String) });`,
expected: `const myService = {
methodA: vi.fn().mockName("MyService.methodA").mockReturnValue(expect.any(String)),
};`,
},
{
description: 'should add a TODO for jasmine.createSpyObj with only one argument',
input: `const myService = jasmine.createSpyObj('MyService');`,
expected: `
// TODO: vitest-migration: jasmine.createSpyObj called with a single argument is not supported for transformation. See: https://vitest.dev/api/vi.html#vi-fn
const myService = jasmine.createSpyObj('MyService');
`,
},
{
description: 'should transform jasmine.createSpyObj with a property map',
input: `const myService = jasmine.createSpyObj('MyService', ['methodA'], { propA: 'valueA' });`,
expected: `const myService = {
methodA: vi.fn().mockName("MyService.methodA"),
propA: 'valueA',
};`,
},
{
description: 'should transform jasmine.createSpyObj with a method map and a property map',
input: `const myService = jasmine.createSpyObj('MyService', { methodA: 'foo' }, { propA: 'valueA' });`,
expected: `const myService = {
methodA: vi.fn().mockName("MyService.methodA").mockReturnValue('foo'),
propA: 'valueA',
};`,
},
{
description: 'should ignore non-string literals in the method array',
input: `const myService = jasmine.createSpyObj('MyService', ['methodA', 123, someVar]);`,
expected: `const myService = {
methodA: vi.fn().mockName("MyService.methodA"),
};`,
},
];
testCases.forEach(({ description, input, expected }) => {
it(description, async () => {
await expectTransformation(input, expected);
});
});
});
describe('transformSpyReset', () => {
const testCases = [
{
description: 'should transform spy.calls.reset() to spy.mockClear()',
input: `mySpy.calls.reset();`,
expected: `mySpy.mockClear();`,
},
];
testCases.forEach(({ description, input, expected }) => {
it(description, async () => {
await expectTransformation(input, expected);
});
});
});
describe('transformSpyCallInspection', () => {
const testCases = [
{
description: 'should transform spy.calls.any()',
input: `expect(mySpy.calls.any()).toBe(true);`,
expected: `expect(vi.mocked(mySpy).mock.calls.length > 0).toBe(true);`,
},
{
description: 'should transform spy.calls.count()',
input: `expect(mySpy.calls.count()).toBe(1);`,
expected: `expect(vi.mocked(mySpy).mock.calls.length).toBe(1);`,
},
{
description: 'should transform spy.calls.argsFor(0)',
input: `const args = mySpy.calls.argsFor(0);`,
expected: `const args = vi.mocked(mySpy).mock.calls[0];`,
},
{
description: 'should transform spy.calls.allArgs()',
input: `const allArgs = mySpy.calls.allArgs();`,
expected: `const allArgs = vi.mocked(mySpy).mock.calls;`,
},
{
description: 'should transform spy.calls.all()',
input: `const allCalls = mySpy.calls.all();`,
expected: `const allCalls = vi.mocked(mySpy).mock.calls;`,
},
{
description: 'should transform spy.calls.mostRecent().args',
input: `const recentArgs = mySpy.calls.mostRecent().args;`,
expected: `const recentArgs = vi.mocked(mySpy).mock.lastCall;`,
},
{
description: 'should transform spy.calls.first()',
input: `const firstCall = mySpy.calls.first();`,
expected: `const firstCall = vi.mocked(mySpy).mock.calls[0];`,
},
{
description: 'should add a TODO for spy.calls.mostRecent() without .args',
input: `const mostRecent = mySpy.calls.mostRecent();`,
// eslint-disable-next-line max-len
expected: `// TODO: vitest-migration: Direct usage of mostRecent() is not supported. Please refactor to access .args directly or use vi.mocked(spy).mock.lastCall. See: https://vitest.dev/api/mocked.html#mock-lastcall
const mostRecent = mySpy.calls.mostRecent();`,
},
];
testCases.forEach(({ description, input, expected }) => {
it(description, async () => {
await expectTransformation(input, expected);
});
});
});
});