Skip to content

Commit c40aefa

Browse files
committed
fixup! fix(@schematics/angular): add a TODO for negated expectAsync matchers in refactor-jasmine-vitest
1 parent 4c69f0c commit c40aefa

2 files changed

Lines changed: 46 additions & 19 deletions

File tree

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

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -263,36 +263,47 @@ export function transformExpectAsync(
263263
return node;
264264
}
265265

266-
// Negated async matchers have no exact equivalent (e.g. `.not.toBeResolvedTo()` also passes on rejection).
267-
const notPae = node.expression.expression;
266+
// When the chain continues past this call (e.g. the `withContext('msg')` link itself),
267+
// the outermost matcher call handles the whole statement.
268268
if (
269-
ts.isPropertyAccessExpression(notPae) &&
270-
notPae.name.text === 'not' &&
271-
ts.isCallExpression(notPae.expression) &&
272-
ts.isIdentifier(notPae.expression.expression) &&
273-
notPae.expression.expression.text === 'expectAsync'
269+
node.parent &&
270+
ts.isPropertyAccessExpression(node.parent) &&
271+
node.parent.expression === node
274272
) {
275-
const category = 'unsupported-expect-async-matcher';
276-
reporter.recordTodo(category, sourceFile, node);
277-
addTodoComment(node, category, { name: `not.${node.expression.name.text}` });
278-
279-
return node;
280-
}
281-
282-
if (!ts.isCallExpression(node.expression.expression)) {
283273
return node;
284274
}
285275

286276
const matcherCall = node;
287277
const matcherPae = node.expression;
288-
const expectCall = node.expression.expression;
278+
279+
// Walk down any modifier chain (`.not`, `.already`, `.withContext(...)`) to the root call.
280+
// Modifiers have no direct vitest equivalent, so a modified matcher falls through to the
281+
// TODO handling below.
282+
const modifierNames: string[] = [];
283+
let expectCall: ts.CallExpression | undefined;
284+
let current: ts.Expression = matcherPae.expression;
285+
while (!expectCall) {
286+
if (ts.isCallExpression(current)) {
287+
if (ts.isIdentifier(current.expression)) {
288+
expectCall = current;
289+
} else {
290+
current = current.expression;
291+
}
292+
} else if (ts.isPropertyAccessExpression(current) && ts.isIdentifier(current.name)) {
293+
modifierNames.unshift(current.name.text);
294+
current = current.expression;
295+
} else {
296+
return node;
297+
}
298+
}
289299

290300
if (!ts.isIdentifier(expectCall.expression) || expectCall.expression.text !== 'expectAsync') {
291301
return node;
292302
}
293303

294304
const matcherName = ts.isIdentifier(matcherPae.name) ? matcherPae.name.text : undefined;
295-
const mapping = matcherName ? ASYNC_MATCHER_CHANGES.get(matcherName) : undefined;
305+
const mapping =
306+
matcherName && modifierNames.length === 0 ? ASYNC_MATCHER_CHANGES.get(matcherName) : undefined;
296307

297308
if (mapping) {
298309
reporter.reportTransformation(
@@ -314,14 +325,14 @@ export function transformExpectAsync(
314325
}
315326

316327
if (matcherName) {
317-
if (matcherName === 'toBePending') {
328+
if (matcherName === 'toBePending' && modifierNames.length === 0) {
318329
const category = 'toBePending';
319330
reporter.recordTodo(category, sourceFile, node);
320331
addTodoComment(node, category);
321332
} else {
322333
const category = 'unsupported-expect-async-matcher';
323334
reporter.recordTodo(category, sourceFile, node);
324-
addTodoComment(node, category, { name: matcherName });
335+
addTodoComment(node, category, { name: [...modifierNames, matcherName].join('.') });
325336
}
326337
}
327338

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ describe('transformExpectAsync', () => {
9292
await expectAsync(myPromise).not.toBeResolved();
9393
`,
9494
},
95+
{
96+
description: 'should add a TODO for an expectAsync matcher modified with already',
97+
input: `await expectAsync(myPromise).already.toBeResolved();`,
98+
expected: `
99+
// TODO: vitest-migration: Unsupported expectAsync matcher ".already.toBeResolved()" found. Please migrate this manually.
100+
await expectAsync(myPromise).already.toBeResolved();
101+
`,
102+
},
103+
{
104+
description: 'should add a TODO for an expectAsync matcher modified with withContext',
105+
input: `await expectAsync(myPromise).withContext('msg').toBeResolved();`,
106+
expected: `
107+
// TODO: vitest-migration: Unsupported expectAsync matcher ".withContext.toBeResolved()" found. Please migrate this manually.
108+
await expectAsync(myPromise).withContext('msg').toBeResolved();
109+
`,
110+
},
95111
{
96112
description: 'should add a specific TODO for toBePending',
97113
input: `await expectAsync(myPromise).toBePending();`,

0 commit comments

Comments
 (0)