Skip to content

Commit 43d5dfa

Browse files
cexbrayatclydin
authored andcommitted
fix(@schematics/angular): add missing imports for lifecycle hooks in jasmine-vitest migration
1 parent 48d942a commit 43d5dfa

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ import { RefactorReporter } from './utils/refactor-reporter';
5656
*/
5757
const BLANK_LINE_PLACEHOLDER = '// __PRESERVE_BLANK_LINE__';
5858

59+
/**
60+
* Vitest function names that should be imported when using the --add-imports option.
61+
*/
62+
const VITEST_FUNCTION_NAMES = new Set([
63+
'describe',
64+
'it',
65+
'expect',
66+
'beforeEach',
67+
'afterEach',
68+
'beforeAll',
69+
'afterAll',
70+
]);
71+
5972
/**
6073
* Replaces blank lines in the content with a placeholder to prevent TypeScript's printer
6174
* from removing them. This ensures that the original formatting of blank lines is preserved.
@@ -183,7 +196,7 @@ export function transformJasmineToVitest(
183196
if (ts.isCallExpression(transformedNode)) {
184197
if (options.addImports && ts.isIdentifier(transformedNode.expression)) {
185198
const name = transformedNode.expression.text;
186-
if (name === 'describe' || name === 'it' || name === 'expect') {
199+
if (VITEST_FUNCTION_NAMES.has(name)) {
187200
addVitestValueImport(pendingVitestValueImports, name);
188201
}
189202
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,41 @@ describe('Jasmine to Vitest Transformer', () => {
8181
`;
8282
await expectTransformation(input, expected, true);
8383
});
84+
85+
it('should add imports for beforeEach and afterEach when addImports is true', async () => {
86+
const input = `
87+
describe('My Suite', () => {
88+
beforeEach(() => {});
89+
afterEach(() => {});
90+
});
91+
`;
92+
const expected = `
93+
import { afterEach, beforeEach, describe } from 'vitest';
94+
95+
describe('My Suite', () => {
96+
beforeEach(() => {});
97+
afterEach(() => {});
98+
});
99+
`;
100+
await expectTransformation(input, expected, true);
101+
});
102+
103+
it('should add imports for beforeAll and afterAll when addImports is true', async () => {
104+
const input = `
105+
describe('My Suite', () => {
106+
beforeAll(() => {});
107+
afterAll(() => {});
108+
});
109+
`;
110+
const expected = `
111+
import { afterAll, beforeAll, describe } from 'vitest';
112+
113+
describe('My Suite', () => {
114+
beforeAll(() => {});
115+
afterAll(() => {});
116+
});
117+
`;
118+
await expectTransformation(input, expected, true);
119+
});
84120
});
85121
});

0 commit comments

Comments
 (0)