Solving the hallucination loop in AI generated unit tests A developer using Claude 3.5 Sonnet through Cursor spent four hours stuck in a "hallucination loop" generating Jest unit tests for a TypeScript payment gateway, because the model assumed standard jest.mock() behavior while the project used a custom ServiceLocator singleton. The tests failed repeatedly with "TypeError: Cannot read properties of undefined (reading 'processPayment')" until the developer supplied the dependency-container.ts file and explicitly mocked the singleton instance via ServiceLocator.getInstance before the beforeEach block. The developer's stated fix is to stop prompting for the solution and instead prompt for the AI's assumption about a variable's state at a given line. Solving the hallucination loop in AI generated unit tests I spent four hours last Thursday fighting a ghost in my test suite. I was using Claude 3.5 Sonnet via Cursor /en/tags/cursor/ to generate unit tests for a TypeScript payment gateway module. The AI kept generating tests that looked perfect—beautifully structured, covering edge cases, and using Jest mocks—but they failed every single time I ran them. The error was always some variation of: TypeError: Cannot read properties of undefined reading 'processPayment' The wild part? The AI looked at the error, apologized, and gave me the exact same code back with a different comment. I was stuck in a "hallucination loop" where the AI assumed my mock was being injected correctly when, in reality, the dependency injection in my actual project used a custom singleton pattern that the LLM didn't grasp from the provided context. Why the AI kept lying about the mocks The problem wasn't the logic; it was the context window. I had attached the service file, but not the dependency-container.ts file where the actual instantiation happened. The AI assumed I was using standard Jest jest.mock behavior, but my project required a manual override of the singleton instance. I tried the "just fix it" prompt three times. Waste of time. The breakthrough happened when I stopped asking the AI to "fix the error" and instead asked it to "explain how it thinks the PaymentService is being instantiated in the test environment." It admitted it was guessing based on common patterns. Here is the comparison of what it gave me versus what actually worked: | Attempt | AI's Assumption | Actual Reality | Result | | :--- | :--- | :--- | :--- | | 1 | jest.mock './service' works automatically | Project uses a custom ServiceLocator | TypeError | | 2 | Mock should be passed in constructor | Service is a singleton accessed via .getInstance | TypeError | | 3 | After I provided the container file Manual override of singleton | Manual override of singleton | Pass | The fix that actually stopped the loop Instead of letting the AI guess, I had to be explicit about the mocking strategy. I stopped using the generic "generate tests" prompt and switched to a specific implementation pattern. The solution was to explicitly mock the singleton instance before the beforeEach block. js // This is what actually worked after 4 hours of failure import { ServiceLocator } from '../container'; import { PaymentService } from './payment.service'; jest.mock '../container' ; describe 'PaymentGateway', = { let mockPaymentService: jest.Mocked