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 @@ -50,23 +50,30 @@ export function transformFocusedAndSkippedTests(
return node;
}

/**
* Checks whether a call expression target is `it`, `it.only`, or `it.skip`.
* `fit`/`xit` have already become `it.only`/`it.skip` when `transformPending`
* and `transformDoneCallback` run, as `transformFocusedAndSkippedTests` is
* applied earlier in the pipeline.
*/
function isItCallExpression(expression: ts.Expression): boolean {
if (ts.isIdentifier(expression) && expression.text === 'it') {
return true;
}

return (
ts.isPropertyAccessExpression(expression) &&
ts.isIdentifier(expression.expression) &&
expression.expression.text === 'it' &&
(expression.name.text === 'only' || expression.name.text === 'skip')
);
}

export function transformPending(
node: ts.Node,
{ sourceFile, reporter, tsContext }: RefactorContext,
): ts.Node {
if (!ts.isCallExpression(node)) {
return node;
}

// `fit`/`xit` have already become `it.only`/`it.skip` when this transformer runs,
// as `transformFocusedAndSkippedTests` is applied earlier in the pipeline.
const isBareIt = ts.isIdentifier(node.expression) && node.expression.text === 'it';
const isFocusedOrSkippedIt =
ts.isPropertyAccessExpression(node.expression) &&
ts.isIdentifier(node.expression.expression) &&
node.expression.expression.text === 'it' &&
(node.expression.name.text === 'only' || node.expression.name.text === 'skip');
if (!isBareIt && !isFocusedOrSkippedIt) {
if (!ts.isCallExpression(node) || !isItCallExpression(node.expression)) {
return node;
}

Expand Down Expand Up @@ -301,11 +308,17 @@ function countDoneUsages(node: ts.Node, doneIdentifier: ts.Identifier): number {

export function transformDoneCallback(node: ts.Node, refactorCtx: RefactorContext): ts.Node {
const { sourceFile, reporter, tsContext } = refactorCtx;
if (
!ts.isCallExpression(node) ||
!ts.isIdentifier(node.expression) ||
!['it', 'beforeEach', 'afterEach', 'beforeAll', 'afterAll'].includes(node.expression.text)
) {
if (!ts.isCallExpression(node)) {
return node;
}

const isLifecycleHook =
ts.isIdentifier(node.expression) &&
(node.expression.text === 'beforeEach' ||
node.expression.text === 'afterEach' ||
node.expression.text === 'beforeAll' ||
node.expression.text === 'afterAll');
if (!isItCallExpression(node.expression) && !isLifecycleHook) {
return node;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,32 @@ describe('Jasmine to Vitest Transformer - transformDoneCallback', () => {
});
`,
},
{
description: 'should transform a focused test (fit) with a done callback',
input: `
fit('should do something async', (done) => {
service.init().then(() => done());
});
`,
expected: `
it.only('should do something async', async () => {
await service.init().then(() => {});
});
`,
},
{
description: 'should transform a skipped test (xit) with a done callback',
input: `
xit('should do something async', (done) => {
done.fail('it failed');
});
`,
expected: `
it.skip('should do something async', async () => {
throw new Error('it failed');
});
`,
},
];

testCases.forEach(({ description, input, expected }) => {
Expand Down
Loading