From 3186ef4e20de4ff131ec306caf9708871b97c6a7 Mon Sep 17 00:00:00 2001 From: MeAkib Date: Thu, 5 Nov 2026 19:21:44 +0600 Subject: [PATCH] fix(@schematics/angular): add the missing vi import for toHaveBeenCalledBefore in refactor-jasmine-vitest The `toHaveBeenCalledBefore` transformation rewrites the matcher to a comparison of `vi.mocked(spy).mock.invocationCallOrder` values but never registers `vi` as a pending value import. When the schematic runs with `--add-imports`, the migrated file only imports `expect` and fails to compile with "Cannot find name 'vi'" unless another transformation in the same file happens to use `vi`. Register the `vi` import when the matcher is transformed, as every other transformation that emits `vi.*` already does. The new spec fails without the fix: the migrated output contained `import { expect } from 'vitest'` instead of `import { expect, vi } from 'vitest'`. --- .../test-file-transformer_add-imports_spec.ts | 9 +++++++++ .../jasmine-vitest/transformers/jasmine-matcher.ts | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer_add-imports_spec.ts b/packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer_add-imports_spec.ts index cbe05226ef7b..feac0a927f27 100644 --- a/packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer_add-imports_spec.ts +++ b/packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer_add-imports_spec.ts @@ -81,6 +81,15 @@ describe('Jasmine to Vitest Transformer - addImports option', () => { await expectTransformation(input, expected, true); }); + it('should add the vi import for toHaveBeenCalledBefore when addImports is true', async () => { + const input = `expect(spyA).toHaveBeenCalledBefore(spyB);`; + const expected = ` + import { expect, vi } from 'vitest'; + expect(Math.min(...vi.mocked(spyA).mock.invocationCallOrder)).toBeLessThan(Math.min(...vi.mocked(spyB).mock.invocationCallOrder)); + `; + await expectTransformation(input, expected, true); + }); + it('should add imports for beforeEach and afterEach when addImports is true', async () => { const input = ` describe('My Suite', () => { 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 c8ba2baadefa..4c9309d21f5e 100644 --- a/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-matcher.ts +++ b/packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-matcher.ts @@ -123,7 +123,7 @@ export function transformAsymmetricMatchers( export function transformToHaveBeenCalledBefore( node: ts.Node, - { sourceFile, reporter }: RefactorContext, + { sourceFile, reporter, pendingVitestValueImports }: RefactorContext, ): ts.Node { if ( !ts.isCallExpression(node) || @@ -147,6 +147,7 @@ export function transformToHaveBeenCalledBefore( return node; } + addVitestValueImport(pendingVitestValueImports, 'vi'); reporter.reportTransformation( sourceFile, node,