Skip to content

Commit 4cb7bb5

Browse files
MeAkibalan-agius4
authored andcommitted
fix(@schematics/angular): add a TODO for negated arrayWithExactContents in refactor-jasmine-vitest
`transformArrayWithExactContents` splits the assertion into a `.toHaveLength()` check and a `.toEqual(expect.arrayContaining())` check without looking for `.not`. A negated assertion is split into two negated checks, which asserts that neither condition holds instead of not both: an array with the right length but different contents passes in Jasmine and fails after the migration. Vitest has no single matcher for exact-contents-in-any-order, so a faithful negation cannot be expressed by splitting. Leave the negated form unchanged and add a TODO comment instead, matching the existing handling of a dynamic (non-literal) argument. The new spec runs the full transform pipeline on a negated assertion; it fails without this change and `//packages/schematics/angular:test` passes with it.
1 parent 5d837b5 commit 4cb7bb5

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,17 @@ export function transformArrayWithExactContents(
413413
return node;
414414
}
415415

416+
const expectCall = node.expression.expression.expression;
417+
418+
// Splitting a negated assertion would assert that neither condition holds instead of not both.
419+
if (ts.isPropertyAccessExpression(expectCall) && expectCall.name.text === 'not') {
420+
const category = 'arrayWithExactContents-negated';
421+
reporter.recordTodo(category, sourceFile, node);
422+
addTodoComment(node, category);
423+
424+
return node;
425+
}
426+
416427
if (!ts.isArrayLiteralExpression(argument.arguments[0])) {
417428
const category = 'arrayWithExactContents-dynamic-variable';
418429
reporter.recordTodo(category, sourceFile, node);
@@ -427,7 +438,6 @@ export function transformArrayWithExactContents(
427438
'Transformed `jasmine.arrayWithExactContents()` to `.toHaveLength()` and `.toEqual(expect.arrayContaining())`.',
428439
);
429440

430-
const expectCall = node.expression.expression.expression;
431441
const arrayLiteral = argument.arguments[0];
432442

433443
const lengthCall = ts.factory.createCallExpression(

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,16 @@ describe('transformArrayWithExactContents', () => {
256256
expect(myArray).toEqual(jasmine.arrayWithExactContents(someOtherArray));
257257
`,
258258
},
259+
{
260+
description: 'should add a TODO for a negated toEqual(jasmine.arrayWithExactContents())',
261+
input: `expect(myArray).not.toEqual(jasmine.arrayWithExactContents(['a', 'b']));`,
262+
/* eslint-disable max-len */
263+
expected: `
264+
// TODO: vitest-migration: Cannot transform a negated jasmine.arrayWithExactContents. Splitting it would assert that neither condition holds instead of not both. Please migrate this manually.
265+
expect(myArray).not.toEqual(jasmine.arrayWithExactContents(['a', 'b']));
266+
`,
267+
/* eslint-enable max-len */
268+
},
259269
];
260270

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

‎packages/schematics/angular/refactor/jasmine-vitest/utils/todo-notes.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ export const TODO_NOTES = {
5656
message:
5757
'Cannot transform jasmine.arrayWithExactContents with a dynamic variable. Please migrate this manually.',
5858
},
59+
'arrayWithExactContents-negated': {
60+
message:
61+
'Cannot transform a negated jasmine.arrayWithExactContents. ' +
62+
'Splitting it would assert that neither condition holds instead of not both. ' +
63+
'Please migrate this manually.',
64+
},
5965
'arrayWithExactContents-check': {
6066
message:
6167
"Verify this matches strict array content (multiset equality). Vitest's arrayContaining is a subset check.",

0 commit comments

Comments
 (0)