From 9db466398f0562826b43359add34aff459960547 Mon Sep 17 00:00:00 2001 From: Shuaib Hasan Akib Date: Thu, 12 Nov 2026 08:18:37 +0600 Subject: [PATCH] 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. --- .../jasmine-vitest/transformers/jasmine-matcher.ts | 12 +++++++++++- .../transformers/jasmine-matcher_spec.ts | 10 ++++++++++ .../refactor/jasmine-vitest/utils/todo-notes.ts | 6 ++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-matcher.ts b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-matcher.ts index 4c9309d21f5e..c65265d081bb 100644 --- a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-matcher.ts +++ b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-matcher.ts @@ -413,6 +413,17 @@ export function transformArrayWithExactContents( return node; } + const expectCall = node.expression.expression.expression; + + // Splitting a negated assertion would assert that neither condition holds instead of not both. + if (ts.isPropertyAccessExpression(expectCall) && expectCall.name.text === 'not') { + const category = 'arrayWithExactContents-negated'; + reporter.recordTodo(category, sourceFile, node); + addTodoComment(node, category); + + return node; + } + if (!ts.isArrayLiteralExpression(argument.arguments[0])) { const category = 'arrayWithExactContents-dynamic-variable'; reporter.recordTodo(category, sourceFile, node); @@ -427,7 +438,6 @@ export function transformArrayWithExactContents( 'Transformed `jasmine.arrayWithExactContents()` to `.toHaveLength()` and `.toEqual(expect.arrayContaining())`.', ); - const expectCall = node.expression.expression.expression; const arrayLiteral = argument.arguments[0]; const lengthCall = ts.factory.createCallExpression( diff --git a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-matcher_spec.ts b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-matcher_spec.ts index f123ac3b84b3..8873aeee133f 100644 --- a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-matcher_spec.ts +++ b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-matcher_spec.ts @@ -256,6 +256,16 @@ describe('transformArrayWithExactContents', () => { expect(myArray).toEqual(jasmine.arrayWithExactContents(someOtherArray)); `, }, + { + description: 'should add a TODO for a negated toEqual(jasmine.arrayWithExactContents())', + input: `expect(myArray).not.toEqual(jasmine.arrayWithExactContents(['a', 'b']));`, + /* eslint-disable max-len */ + expected: ` + // 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. + expect(myArray).not.toEqual(jasmine.arrayWithExactContents(['a', 'b'])); + `, + /* eslint-enable max-len */ + }, ]; testCases.forEach(({ description, input, expected }) => { diff --git a/packages/schematics/angular/refactor/jasmine-vitest/utils/todo-notes.ts b/packages/schematics/angular/refactor/jasmine-vitest/utils/todo-notes.ts index 8b0db3cd45dc..e1fdb5e4ad5f 100644 --- a/packages/schematics/angular/refactor/jasmine-vitest/utils/todo-notes.ts +++ b/packages/schematics/angular/refactor/jasmine-vitest/utils/todo-notes.ts @@ -56,6 +56,12 @@ export const TODO_NOTES = { message: 'Cannot transform jasmine.arrayWithExactContents with a dynamic variable. Please migrate this manually.', }, + 'arrayWithExactContents-negated': { + message: + 'Cannot transform a negated jasmine.arrayWithExactContents. ' + + 'Splitting it would assert that neither condition holds instead of not both. ' + + 'Please migrate this manually.', + }, 'arrayWithExactContents-check': { message: "Verify this matches strict array content (multiset equality). Vitest's arrayContaining is a subset check.",