Skip to content

Commit 1ef1b17

Browse files
committed
feat(@schematics/angular): stabilize refactor-jasmine-vitest schematic
Stabilize `refactor-jasmine-vitest` schematic by covering the known remaining test patterns and cases.
1 parent 2678f5f commit 1ef1b17

4 files changed

Lines changed: 52 additions & 31 deletions

File tree

packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer.integration_spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ describe('Jasmine to Vitest Transformer - Integration Tests', () => {
288288
});
289289
290290
it('should handle spy call order', () => {
291-
const spyA = vi.fn();
292-
const spyB = vi.fn();
291+
const spyA = vi.fn().mockName('spyA');
292+
const spyB = vi.fn().mockName('spyB');
293293
spyA();
294294
spyB();
295295
expect(Math.min(...vi.mocked(spyA).mock.invocationCallOrder)).toBeLessThan(Math.min(...vi.mocked(spyB).mock.invocationCallOrder));
@@ -387,7 +387,7 @@ describe('Jasmine to Vitest Transformer - Integration Tests', () => {
387387
});
388388
389389
it('should handle spies throwing errors', () => {
390-
const spy = vi.fn().mockImplementation(() => { throw new Error('Test Error') });
390+
const spy = vi.fn().mockName('mySpy').mockImplementation(() => { throw new Error('Test Error') });
391391
expect(() => spy()).toThrowError('Test Error');
392392
});
393393
});

packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
transformUnsupportedJasmineCalls,
4141
} from './transformers/jasmine-misc';
4242
import {
43+
transformCreateSpy,
4344
transformCreateSpyObj,
4445
transformSpies,
4546
transformSpyCallInspection,
@@ -116,6 +117,7 @@ const callExpressionTransformers = [
116117
transformSyntacticSugarMatchers,
117118
transformComplexMatchers,
118119
transformSpies,
120+
transformCreateSpy,
119121
transformCreateSpyObj,
120122
transformSpyReset,
121123
transformSpyCallInspection,

packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy.ts

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -183,35 +183,54 @@ export function transformSpies(node: ts.Node, refactorCtx: RefactorContext): ts.
183183
}
184184
}
185185

186-
const jasmineMethodName = getJasmineMethodName(node);
187-
switch (jasmineMethodName) {
188-
case 'createSpy':
189-
addVitestValueImport(pendingVitestValueImports, 'vi');
190-
reporter.reportTransformation(
191-
sourceFile,
192-
node,
193-
'Transformed `jasmine.createSpy()` to `vi.fn()`.',
194-
);
195-
196-
// jasmine.createSpy(name, originalFn) -> vi.fn(originalFn)
197-
return createViCallExpression('fn', node.arguments.length > 1 ? [node.arguments[1]] : []);
198-
case 'spyOnAllFunctions': {
199-
reporter.reportTransformation(
200-
sourceFile,
201-
node,
202-
'Found unsupported `jasmine.spyOnAllFunctions()`.',
203-
);
204-
const category = 'spyOnAllFunctions';
205-
reporter.recordTodo(category, sourceFile, node);
206-
addTodoComment(node, category);
186+
if (getJasmineMethodName(node) === 'spyOnAllFunctions') {
187+
reporter.reportTransformation(
188+
sourceFile,
189+
node,
190+
'Found unsupported `jasmine.spyOnAllFunctions()`.',
191+
);
192+
const category = 'spyOnAllFunctions';
193+
reporter.recordTodo(category, sourceFile, node);
194+
addTodoComment(node, category);
207195

208-
return node;
209-
}
196+
return node;
210197
}
211198

212199
return node;
213200
}
214201

202+
export function transformCreateSpy(
203+
node: ts.Node,
204+
{ reporter, sourceFile, pendingVitestValueImports }: RefactorContext,
205+
): ts.Node {
206+
if (!isJasmineCallExpression(node, 'createSpy')) {
207+
return node;
208+
}
209+
210+
addVitestValueImport(pendingVitestValueImports, 'vi');
211+
reporter.reportTransformation(
212+
sourceFile,
213+
node,
214+
'Transformed `jasmine.createSpy()` to `vi.fn()`.',
215+
);
216+
217+
const spyName = node.arguments[0];
218+
const viFnCallExpression = createViCallExpression(
219+
'fn',
220+
node.arguments.length > 1 ? [node.arguments[1]] : [],
221+
);
222+
223+
// jasmine.createSpy() -> vi.fn()
224+
// jasmine.createSpy(name, originalFn) -> vi.fn(originalFn).mockName(name)
225+
return !spyName
226+
? viFnCallExpression
227+
: ts.factory.createCallExpression(
228+
createPropertyAccess(viFnCallExpression, 'mockName'),
229+
undefined,
230+
[node.arguments[0]],
231+
);
232+
}
233+
215234
export function transformCreateSpyObj(
216235
node: ts.Node,
217236
{ sourceFile, reporter, pendingVitestValueImports }: RefactorContext,

packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-spy_spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ describe('Jasmine to Vitest Transformer - transformSpies', () => {
3636
expected: `vi.spyOn(service, 'myMethod');`,
3737
},
3838
{
39-
description: 'should transform jasmine.createSpy("name") to vi.fn()',
39+
description: 'should transform jasmine.createSpy("name") to vi.fn().mockName("name")',
4040
input: `const mySpy = jasmine.createSpy('mySpy');`,
41-
expected: `const mySpy = vi.fn();`,
41+
expected: `const mySpy = vi.fn().mockName('mySpy');`,
4242
},
4343
{
44-
description: 'should transform jasmine.createSpy("name", fn) to vi.fn(fn)',
44+
description: 'should transform jasmine.createSpy("name", fn) to vi.fn(fn).mockName("name")',
4545
input: `const mySpy = jasmine.createSpy('mySpy', () => 'foo');`,
46-
expected: `const mySpy = vi.fn(() => 'foo');`,
46+
expected: `const mySpy = vi.fn(() => 'foo').mockName('mySpy');`,
4747
},
4848
{
4949
description: 'should transform spyOnProperty(object, "prop") to vi.spyOn(object, "prop")',
@@ -65,7 +65,7 @@ describe('Jasmine to Vitest Transformer - transformSpies', () => {
6565
{
6666
description: 'should handle chained calls on jasmine.createSpy()',
6767
input: `const mySpy = jasmine.createSpy('mySpy').and.returnValue(true);`,
68-
expected: `const mySpy = vi.fn().mockReturnValue(true);`,
68+
expected: `const mySpy = vi.fn().mockName('mySpy').mockReturnValue(true);`,
6969
},
7070
{
7171
description: 'should handle .and.returnValues() with no arguments',

0 commit comments

Comments
 (0)