Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ import { RefactorReporter } from './utils/refactor-reporter';
*/
const BLANK_LINE_PLACEHOLDER = '// __PRESERVE_BLANK_LINE__';

/**
* Vitest function names that should be imported when using the --add-imports option.
*/
const VITEST_FUNCTION_NAMES = new Set([
'describe',
'it',
'expect',
'beforeEach',
'afterEach',
'beforeAll',
'afterAll',
]);

/**
* Replaces blank lines in the content with a placeholder to prevent TypeScript's printer
* from removing them. This ensures that the original formatting of blank lines is preserved.
Expand Down Expand Up @@ -183,7 +196,7 @@ export function transformJasmineToVitest(
if (ts.isCallExpression(transformedNode)) {
if (options.addImports && ts.isIdentifier(transformedNode.expression)) {
const name = transformedNode.expression.text;
if (name === 'describe' || name === 'it' || name === 'expect') {
if (VITEST_FUNCTION_NAMES.has(name)) {
addVitestValueImport(pendingVitestValueImports, name);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,41 @@ describe('Jasmine to Vitest Transformer', () => {
`;
await expectTransformation(input, expected, true);
});

it('should add imports for beforeEach and afterEach when addImports is true', async () => {
const input = `
describe('My Suite', () => {
beforeEach(() => {});
afterEach(() => {});
});
`;
const expected = `
import { afterEach, beforeEach, describe } from 'vitest';

describe('My Suite', () => {
beforeEach(() => {});
afterEach(() => {});
});
`;
await expectTransformation(input, expected, true);
});

it('should add imports for beforeAll and afterAll when addImports is true', async () => {
const input = `
describe('My Suite', () => {
beforeAll(() => {});
afterAll(() => {});
});
`;
const expected = `
import { afterAll, beforeAll, describe } from 'vitest';

describe('My Suite', () => {
beforeAll(() => {});
afterAll(() => {});
});
`;
await expectTransformation(input, expected, true);
});
});
});