Skip to content

Commit e50f182

Browse files
clydinalan-agius4
authored andcommitted
fix(@schematics/angular): handle done callbacks in focused and skipped tests
Similar to `transformPending`, `transformDoneCallback` previously only matched calls whose expression was the bare `it` identifier (or lifecycle hooks). Because `transformFocusedAndSkippedTests` runs earlier in the pipeline and rewrites `fit` to `it.only` and `xit` to `it.skip`, focused and skipped specs using a `done` callback were skipped by `transformDoneCallback`, leaving the `done` parameter and `done()`/`done.fail()` calls unmigrated. (cherry picked from commit 600c5d3)
1 parent a2d8a25 commit e50f182

2 files changed

Lines changed: 57 additions & 18 deletions

File tree

‎packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-lifecycle.ts‎

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,30 @@ export function transformFocusedAndSkippedTests(
5050
return node;
5151
}
5252

53+
/**
54+
* Checks whether a call expression target is `it`, `it.only`, or `it.skip`.
55+
* `fit`/`xit` have already become `it.only`/`it.skip` when `transformPending`
56+
* and `transformDoneCallback` run, as `transformFocusedAndSkippedTests` is
57+
* applied earlier in the pipeline.
58+
*/
59+
function isItCallExpression(expression: ts.Expression): boolean {
60+
if (ts.isIdentifier(expression) && expression.text === 'it') {
61+
return true;
62+
}
63+
64+
return (
65+
ts.isPropertyAccessExpression(expression) &&
66+
ts.isIdentifier(expression.expression) &&
67+
expression.expression.text === 'it' &&
68+
(expression.name.text === 'only' || expression.name.text === 'skip')
69+
);
70+
}
71+
5372
export function transformPending(
5473
node: ts.Node,
5574
{ sourceFile, reporter, tsContext }: RefactorContext,
5675
): ts.Node {
57-
if (!ts.isCallExpression(node)) {
58-
return node;
59-
}
60-
61-
// `fit`/`xit` have already become `it.only`/`it.skip` when this transformer runs,
62-
// as `transformFocusedAndSkippedTests` is applied earlier in the pipeline.
63-
const isBareIt = ts.isIdentifier(node.expression) && node.expression.text === 'it';
64-
const isFocusedOrSkippedIt =
65-
ts.isPropertyAccessExpression(node.expression) &&
66-
ts.isIdentifier(node.expression.expression) &&
67-
node.expression.expression.text === 'it' &&
68-
(node.expression.name.text === 'only' || node.expression.name.text === 'skip');
69-
if (!isBareIt && !isFocusedOrSkippedIt) {
76+
if (!ts.isCallExpression(node) || !isItCallExpression(node.expression)) {
7077
return node;
7178
}
7279

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

302309
export function transformDoneCallback(node: ts.Node, refactorCtx: RefactorContext): ts.Node {
303310
const { sourceFile, reporter, tsContext } = refactorCtx;
304-
if (
305-
!ts.isCallExpression(node) ||
306-
!ts.isIdentifier(node.expression) ||
307-
!['it', 'beforeEach', 'afterEach', 'beforeAll', 'afterAll'].includes(node.expression.text)
308-
) {
311+
if (!ts.isCallExpression(node)) {
312+
return node;
313+
}
314+
315+
const isLifecycleHook =
316+
ts.isIdentifier(node.expression) &&
317+
(node.expression.text === 'beforeEach' ||
318+
node.expression.text === 'afterEach' ||
319+
node.expression.text === 'beforeAll' ||
320+
node.expression.text === 'afterAll');
321+
if (!isItCallExpression(node.expression) && !isLifecycleHook) {
309322
return node;
310323
}
311324

‎packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-lifecycle_spec.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,32 @@ describe('Jasmine to Vitest Transformer - transformDoneCallback', () => {
171171
});
172172
`,
173173
},
174+
{
175+
description: 'should transform a focused test (fit) with a done callback',
176+
input: `
177+
fit('should do something async', (done) => {
178+
service.init().then(() => done());
179+
});
180+
`,
181+
expected: `
182+
it.only('should do something async', async () => {
183+
await service.init().then(() => {});
184+
});
185+
`,
186+
},
187+
{
188+
description: 'should transform a skipped test (xit) with a done callback',
189+
input: `
190+
xit('should do something async', (done) => {
191+
done.fail('it failed');
192+
});
193+
`,
194+
expected: `
195+
it.skip('should do something async', async () => {
196+
throw new Error('it failed');
197+
});
198+
`,
199+
},
174200
];
175201

176202
testCases.forEach(({ description, input, expected }) => {

0 commit comments

Comments
 (0)