Skip to content

Commit 7c6a643

Browse files
committed
fix(@schematics/angular): warn about loose matching in arrayWithExactContents
This commit improves the transformation of `jasmine.arrayWithExactContents` in the `jasmine-vitest` schematic. While Vitest does not have a direct equivalent for multiset equality, the schematic previously transformed this into a check for length and subset containment, which could lead to false positives (e.g., `[1, 1]` matches `[1, 2]`). Now, the schematic attaches a TODO comment to the generated code, explicitly warning the user that the transformation relies on `expect.arrayContaining` (a subset check) and advising them to verify strict content matching manually.
1 parent 2b99ce5 commit 7c6a643

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer.integration_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ describe('Jasmine to Vitest Transformer - Integration Tests', () => {
275275
276276
it('should handle array contents checking', () => {
277277
const arr = [1, 2, 3];
278+
// TODO: vitest-migration: Verify this matches strict array content (multiset equality). Vitest's arrayContaining is a subset check.
278279
expect(arr).toHaveLength(3);
279280
expect(arr).toEqual(expect.arrayContaining([3, 2, 1]));
280281
});

packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer_spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,13 @@ describe('Jasmine to Vitest Transformer', () => {
8585
{
8686
description: 'should handle arrayWithExactContents containing nested asymmetric matchers',
8787
input: `expect(myArray).toEqual(jasmine.arrayWithExactContents([jasmine.objectContaining({ id: 1 })]));`,
88+
/* eslint-disable max-len */
8889
expected: `
90+
// TODO: vitest-migration: Verify this matches strict array content (multiset equality). Vitest's arrayContaining is a subset check.
8991
expect(myArray).toHaveLength(1);
9092
expect(myArray).toEqual(expect.arrayContaining([expect.objectContaining({ id: 1 })]));
9193
`,
94+
/* eslint-enable max-len */
9295
},
9396
{
9497
description: 'should handle a spy rejecting with an asymmetric matcher',

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,14 @@ export function transformArrayWithExactContents(
451451
],
452452
);
453453

454-
return [
455-
ts.factory.createExpressionStatement(lengthCall),
456-
ts.factory.createExpressionStatement(containingCall),
457-
];
454+
const lengthStmt = ts.factory.createExpressionStatement(lengthCall);
455+
const containingStmt = ts.factory.createExpressionStatement(containingCall);
456+
457+
const category = 'arrayWithExactContents-check';
458+
reporter.recordTodo(category);
459+
addTodoComment(lengthStmt, category);
460+
461+
return [lengthStmt, containingStmt];
458462
}
459463

460464
export function transformCalledOnceWith(

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,25 @@ expect(mySpyObj).toHaveSpyInteractions();`,
225225
{
226226
description: 'should transform toEqual(jasmine.arrayWithExactContents()) into two calls',
227227
input: `expect(myArray).toEqual(jasmine.arrayWithExactContents(['a', 'b']));`,
228+
/* eslint-disable max-len */
228229
expected: `
230+
// TODO: vitest-migration: Verify this matches strict array content (multiset equality). Vitest's arrayContaining is a subset check.
229231
expect(myArray).toHaveLength(2);
230232
expect(myArray).toEqual(expect.arrayContaining(['a', 'b']));
231233
`,
234+
/* eslint-enable max-len */
232235
},
233236
{
234237
description:
235238
'should transform toEqual(jasmine.arrayWithExactContents()) with asymmetric matchers',
236239
input: `expect(myArray).toEqual(jasmine.arrayWithExactContents([jasmine.any(Number), 'a']));`,
240+
/* eslint-disable max-len */
237241
expected: `
242+
// TODO: vitest-migration: Verify this matches strict array content (multiset equality). Vitest's arrayContaining is a subset check.
238243
expect(myArray).toHaveLength(2);
239244
expect(myArray).toEqual(expect.arrayContaining([expect.any(Number), 'a']));
240245
`,
246+
/* eslint-enable max-len */
241247
},
242248
{
243249
description:

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export const TODO_NOTES = {
5656
message:
5757
'Cannot transform jasmine.arrayWithExactContents with a dynamic variable. Please migrate this manually.',
5858
},
59+
'arrayWithExactContents-check': {
60+
message:
61+
"Verify this matches strict array content (multiset equality). Vitest's arrayContaining is a subset check.",
62+
},
5963
'expect-nothing': {
6064
message:
6165
'expect().nothing() has been removed because it is redundant in Vitest. Tests without assertions pass by default.',

0 commit comments

Comments
 (0)