Skip to content

Commit f8411ee

Browse files
committed
refactor(@schematics/angular): transform fakeAsync and tick only if they're Angular's and remove imports
1 parent 5d0ee77 commit f8411ee

File tree

7 files changed

+69
-22
lines changed

7 files changed

+69
-22
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ describe('Jasmine to Vitest Transformer - addImports option', () => {
119119

120120
it('should add imports for `onTestFinished` and `vi` when addImports is true', async () => {
121121
const input = `
122+
import { fakeAsync } from '@angular/core/testing';
123+
122124
it('works', fakeAsync(() => {
123125
expect(1).toBe(1);
124126
}));

packages/schematics/angular/refactor/jasmine-vitest/transformers/fake-async-flush.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88

99
import ts from '../../../third_party/github.com/Microsoft/TypeScript/lib/typescript';
1010
import { isNamedImportFrom } from '../utils/ast-helpers';
11+
import { ANGULAR_CORE_TESTING } from '../utils/constants';
1112
import { RefactorContext, addImportSpecifierRemoval } from '../utils/refactor-context';
1213

13-
const ANGULAR_CORE_TESTING = '@angular/core/testing';
14-
1514
export function transformFakeAsyncFlush(node: ts.Node, ctx: RefactorContext): ts.Node {
1615
if (
1716
ts.isCallExpression(node) &&

packages/schematics/angular/refactor/jasmine-vitest/transformers/fake-async-test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,21 @@
77
*/
88

99
import ts from '../../../third_party/github.com/Microsoft/TypeScript/lib/typescript';
10-
import { RefactorContext } from '../utils/refactor-context';
10+
import { isNamedImportFrom } from '../utils/ast-helpers';
11+
import { ANGULAR_CORE_TESTING } from '../utils/constants';
12+
import { RefactorContext, addImportSpecifierRemoval } from '../utils/refactor-context';
1113

1214
export function transformFakeAsyncTest(node: ts.Node, ctx: RefactorContext): ts.Node {
13-
if (!isFakeAsyncCallExpression(node)) {
15+
if (
16+
!(
17+
ts.isCallExpression(node) &&
18+
ts.isIdentifier(node.expression) &&
19+
node.expression.text === 'fakeAsync' &&
20+
node.arguments.length >= 1 &&
21+
(ts.isArrowFunction(node.arguments[0]) || ts.isFunctionExpression(node.arguments[0])) &&
22+
isNamedImportFrom(ctx.sourceFile, 'fakeAsync', ANGULAR_CORE_TESTING)
23+
)
24+
) {
1425
return node;
1526
}
1627

@@ -20,6 +31,8 @@ export function transformFakeAsyncTest(node: ts.Node, ctx: RefactorContext): ts.
2031
`Transformed \`fakeAsync\` to \`vi.useFakeTimers\`.`,
2132
);
2233

34+
addImportSpecifierRemoval(ctx, 'fakeAsync', ANGULAR_CORE_TESTING);
35+
2336
ctx.pendingVitestValueImports.add('onTestFinished');
2437
ctx.pendingVitestValueImports.add('vi');
2538

@@ -79,16 +92,3 @@ export function transformFakeAsyncTest(node: ts.Node, ctx: RefactorContext): ts.
7992
ts.factory.createBlock(setupStatements),
8093
);
8194
}
82-
83-
export function isFakeAsyncCallExpression(node: ts.Node): node is ts.CallExpression & {
84-
expression: ts.Identifier;
85-
arguments: [ts.ArrowFunction | ts.FunctionExpression];
86-
} {
87-
return (
88-
ts.isCallExpression(node) &&
89-
ts.isIdentifier(node.expression) &&
90-
node.expression.text === 'fakeAsync' &&
91-
node.arguments.length >= 1 &&
92-
(ts.isArrowFunction(node.arguments[0]) || ts.isFunctionExpression(node.arguments[0]))
93-
);
94-
}

packages/schematics/angular/refactor/jasmine-vitest/transformers/fake-async-test_spec.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88

99
import { expectTransformation } from '../test-helpers';
1010

11-
describe('transformFakeTimersTest', () => {
11+
describe('transformFakeAsyncTest', () => {
1212
const testCases = [
1313
{
1414
description: 'should transform fakeAsync test to `vi.useFakeTimers()`',
1515
input: `
16+
import { fakeAsync } from '@angular/core/testing';
17+
1618
it('works', fakeAsync(() => {
1719
expect(1).toBe(1);
1820
}));
@@ -27,6 +29,23 @@ describe('transformFakeTimersTest', () => {
2729
});
2830
`,
2931
},
32+
{
33+
description: 'should not replace `fakeAsync` if not imported from `@angular/core/testing`',
34+
input: `
35+
import { fakeAsync } from './my-fake-async';
36+
37+
it('works', fakeAsync(() => {
38+
expect(1).toBe(1);
39+
}));
40+
`,
41+
expected: `
42+
import { fakeAsync } from './my-fake-async';
43+
44+
it('works', fakeAsync(() => {
45+
expect(1).toBe(1);
46+
}));
47+
`,
48+
},
3049
];
3150

3251
testCases.forEach(({ description, input, expected }) => {

packages/schematics/angular/refactor/jasmine-vitest/transformers/fake-async-tick.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,25 @@
77
*/
88

99
import ts from '../../../third_party/github.com/Microsoft/TypeScript/lib/typescript';
10-
import { RefactorContext } from '../utils/refactor-context';
10+
import { isNamedImportFrom } from '../utils/ast-helpers';
11+
import { ANGULAR_CORE_TESTING } from '../utils/constants';
12+
import { RefactorContext, addImportSpecifierRemoval } from '../utils/refactor-context';
1113

1214
export function transformFakeAsyncTick(node: ts.Node, ctx: RefactorContext): ts.Node {
1315
if (
1416
ts.isCallExpression(node) &&
1517
ts.isIdentifier(node.expression) &&
16-
node.expression.text === 'tick'
18+
node.expression.text === 'tick' &&
19+
isNamedImportFrom(ctx.sourceFile, 'tick', ANGULAR_CORE_TESTING)
1720
) {
1821
ctx.reporter.reportTransformation(
1922
ctx.sourceFile,
2023
node,
2124
`Transformed \`tick\` to \`await vi.advanceTimersByTimeAsync()\`.`,
2225
);
2326

27+
addImportSpecifierRemoval(ctx, 'tick', ANGULAR_CORE_TESTING);
28+
2429
const durationNumericLiteral =
2530
node.arguments.length > 0 && ts.isNumericLiteral(node.arguments[0])
2631
? node.arguments[0]

packages/schematics/angular/refactor/jasmine-vitest/transformers/fake-async-tick_spec.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,35 @@ describe('transformFakeAsyncTick', () => {
1212
const testCases = [
1313
{
1414
description: 'should replace `tick` with `vi.advanceTimersByTimeAsync`',
15-
input: `tick(100);`,
15+
input: `
16+
import { tick } from '@angular/core/testing';
17+
18+
tick(100);
19+
`,
1620
expected: `await vi.advanceTimersByTimeAsync(100);`,
1721
},
1822
{
1923
description: 'should replace `tick()` with `vi.advanceTimersByTimeAsync(0)`',
20-
input: `tick();`,
24+
input: `
25+
import { tick } from '@angular/core/testing';
26+
27+
tick();
28+
`,
2129
expected: `await vi.advanceTimersByTimeAsync(0);`,
2230
},
31+
{
32+
description: 'should not replace `tick` if not imported from `@angular/core/testing`',
33+
input: `
34+
import { tick } from './my-tick';
35+
36+
tick(100);
37+
`,
38+
expected: `
39+
import { tick } from './my-tick';
40+
41+
tick(100);
42+
`,
43+
},
2344
];
2445

2546
testCases.forEach(({ description, input, expected }) => {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const ANGULAR_CORE_TESTING = '@angular/core/testing';

0 commit comments

Comments
 (0)