Skip to content

Commit cc4171c

Browse files
committed
refactor(@schematics/angular): transform flushMicrotasks
1 parent f8411ee commit cc4171c

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import ts from '../../third_party/github.com/Microsoft/TypeScript/lib/typescript';
1717
import { transformFakeAsyncFlush } from './transformers/fake-async-flush';
18+
import { transformFakeAsyncFlushMicrotasks } from './transformers/fake-async-flush-microtasks';
1819
import { transformFakeAsyncTest } from './transformers/fake-async-test';
1920
import { transformFakeAsyncTick } from './transformers/fake-async-tick';
2021
import {
@@ -131,6 +132,7 @@ const callExpressionTransformers = [
131132
transformFakeAsyncTest,
132133
transformFakeAsyncTick,
133134
transformFakeAsyncFlush,
135+
transformFakeAsyncFlushMicrotasks,
134136

135137
// **Stage 3: Global Functions & Cleanup**
136138
// These handle global Jasmine functions and catch-alls for unsupported APIs.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import ts from '../../../third_party/github.com/Microsoft/TypeScript/lib/typescript';
10+
import { isNamedImportFrom } from '../utils/ast-helpers';
11+
import { ANGULAR_CORE_TESTING } from '../utils/constants';
12+
import { RefactorContext, addImportSpecifierRemoval } from '../utils/refactor-context';
13+
14+
export function transformFakeAsyncFlushMicrotasks(node: ts.Node, ctx: RefactorContext): ts.Node {
15+
if (
16+
ts.isCallExpression(node) &&
17+
ts.isIdentifier(node.expression) &&
18+
node.expression.text === 'flushMicrotasks' &&
19+
isNamedImportFrom(ctx.sourceFile, 'flushMicrotasks', ANGULAR_CORE_TESTING)
20+
) {
21+
ctx.reporter.reportTransformation(
22+
ctx.sourceFile,
23+
node,
24+
`Transformed \`flushMicrotasks\` to \`await vi.advanceTimersByTimeAsync(0)\`.`,
25+
);
26+
27+
addImportSpecifierRemoval(ctx, 'flushMicrotasks', ANGULAR_CORE_TESTING);
28+
29+
return ts.factory.createAwaitExpression(
30+
ts.factory.createCallExpression(
31+
ts.factory.createPropertyAccessExpression(
32+
ts.factory.createIdentifier('vi'),
33+
'advanceTimersByTimeAsync',
34+
),
35+
undefined,
36+
[ts.factory.createNumericLiteral(0)],
37+
),
38+
);
39+
}
40+
41+
return node;
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import { expectTransformation } from '../test-helpers';
10+
11+
describe('transformFakeAsyncFlushMicrotasks', () => {
12+
const testCases = [
13+
{
14+
description: 'should replace `flushMicrotasks` with `await vi.advanceTimersByTimeAsync(0)`',
15+
input: `
16+
import { flushMicrotasks } from '@angular/core/testing';
17+
18+
flushMicrotasks();
19+
`,
20+
expected: `await vi.advanceTimersByTimeAsync(0);`,
21+
},
22+
{
23+
description:
24+
'should not replace `flushMicrotasks` if not imported from `@angular/core/testing`',
25+
input: `
26+
import { flushMicrotasks } from './my-flush-microtasks';
27+
28+
flushMicrotasks();
29+
`,
30+
expected: `
31+
import { flushMicrotasks } from './my-flush-microtasks';
32+
33+
flushMicrotasks();
34+
`,
35+
},
36+
];
37+
38+
testCases.forEach(({ description, input, expected }) => {
39+
it(description, async () => {
40+
await expectTransformation(input, expected);
41+
});
42+
});
43+
});

0 commit comments

Comments
 (0)