Skip to content

Commit 9e7e7ee

Browse files
committed
fix(@schematics/angular): add missing imports for lifecycle hooks in jasmine-vitest migration
1 parent 59319b8 commit 9e7e7ee

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,15 @@ export function transformJasmineToVitest(
183183
if (ts.isCallExpression(transformedNode)) {
184184
if (options.addImports && ts.isIdentifier(transformedNode.expression)) {
185185
const name = transformedNode.expression.text;
186-
if (name === 'describe' || name === 'it' || name === 'expect') {
186+
if (
187+
name === 'describe' ||
188+
name === 'it' ||
189+
name === 'expect' ||
190+
name === 'beforeEach' ||
191+
name === 'afterEach' ||
192+
name === 'beforeAll' ||
193+
name === 'afterAll'
194+
) {
187195
addVitestValueImport(pendingVitestValueImports, name);
188196
}
189197
}

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)