Skip to content

Commit 96f96b9

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

2 files changed

Lines changed: 37 additions & 30 deletions

File tree

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

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

266-
// When the chain continues past this call (e.g. the `withContext('msg')` link itself),
267-
// the outermost matcher call handles the whole statement.
268-
if (
269-
node.parent &&
270-
ts.isPropertyAccessExpression(node.parent) &&
271-
node.parent.expression === node
272-
) {
273-
return node;
274-
}
275-
276266
const matcherCall = node;
277267
const matcherPae = node.expression;
268+
const matcherName = ts.isIdentifier(matcherPae.name) ? matcherPae.name.text : undefined;
269+
if (!matcherName || matcherName === 'withContext') {
270+
return node;
271+
}
278272

279273
// Walk down any modifier chain (`.not`, `.already`, `.withContext(...)`) to the root call.
280274
// Modifiers have no direct vitest equivalent, so a modified matcher falls through to the
281-
// TODO handling below.
275+
// TODO handling below. `withContext` is the only chainable modifier method, so any other
276+
// intermediate call (e.g. a `.then(...)` chained on a matcher) is not an `expectAsync` chain.
282277
const modifierNames: string[] = [];
283278
let expectCall: ts.CallExpression | undefined;
284279
let current: ts.Expression = matcherPae.expression;
285280
while (!expectCall) {
286281
if (ts.isCallExpression(current)) {
287-
if (ts.isIdentifier(current.expression)) {
282+
if (ts.isIdentifier(current.expression) && current.expression.text === 'expectAsync') {
288283
expectCall = current;
284+
} else if (
285+
ts.isPropertyAccessExpression(current.expression) &&
286+
ts.isIdentifier(current.expression.name) &&
287+
current.expression.name.text === 'withContext'
288+
) {
289+
modifierNames.unshift('withContext');
290+
current = current.expression.expression;
289291
} else {
290-
current = current.expression;
292+
return node;
291293
}
292294
} else if (ts.isPropertyAccessExpression(current) && ts.isIdentifier(current.name)) {
293295
modifierNames.unshift(current.name.text);
@@ -297,13 +299,7 @@ export function transformExpectAsync(
297299
}
298300
}
299301

300-
if (!ts.isIdentifier(expectCall.expression) || expectCall.expression.text !== 'expectAsync') {
301-
return node;
302-
}
303-
304-
const matcherName = ts.isIdentifier(matcherPae.name) ? matcherPae.name.text : undefined;
305-
const mapping =
306-
matcherName && modifierNames.length === 0 ? ASYNC_MATCHER_CHANGES.get(matcherName) : undefined;
302+
const mapping = modifierNames.length === 0 ? ASYNC_MATCHER_CHANGES.get(matcherName) : undefined;
307303

308304
if (mapping) {
309305
reporter.reportTransformation(
@@ -324,16 +320,14 @@ export function transformExpectAsync(
324320
return ts.factory.createCallExpression(newMatcherChain, undefined, newMatcherArgs);
325321
}
326322

327-
if (matcherName) {
328-
if (matcherName === 'toBePending' && modifierNames.length === 0) {
329-
const category = 'toBePending';
330-
reporter.recordTodo(category, sourceFile, node);
331-
addTodoComment(node, category);
332-
} else {
333-
const category = 'unsupported-expect-async-matcher';
334-
reporter.recordTodo(category, sourceFile, node);
335-
addTodoComment(node, category, { name: [...modifierNames, matcherName].join('.') });
336-
}
323+
if (matcherName === 'toBePending' && modifierNames.length === 0) {
324+
const category = 'toBePending';
325+
reporter.recordTodo(category, sourceFile, node);
326+
addTodoComment(node, category);
327+
} else {
328+
const category = 'unsupported-expect-async-matcher';
329+
reporter.recordTodo(category, sourceFile, node);
330+
addTodoComment(node, category, { name: [...modifierNames, matcherName].join('.') });
337331
}
338332

339333
return node;

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,19 @@ describe('transformExpectAsync', () => {
108108
await expectAsync(myPromise).withContext('msg').toBeResolved();
109109
`,
110110
},
111+
{
112+
description: 'should add a single TODO for an expectAsync matcher with combined modifiers',
113+
input: `await expectAsync(myPromise).withContext('msg').not.toBeResolved();`,
114+
expected: `
115+
// TODO: vitest-migration: Unsupported expectAsync matcher ".withContext.not.toBeResolved()" found. Please migrate this manually.
116+
await expectAsync(myPromise).withContext('msg').not.toBeResolved();
117+
`,
118+
},
119+
{
120+
description: 'should transform an expectAsync matcher with a chained .then()',
121+
input: `await expectAsync(myPromise).toBeResolved().then(() => {});`,
122+
expected: `await expect(myPromise).resolves.not.toThrow().then(() => {});`,
123+
},
111124
{
112125
description: 'should add a specific TODO for toBePending',
113126
input: `await expectAsync(myPromise).toBePending();`,

0 commit comments

Comments
 (0)