Skip to content

Instantly share code, notes, and snippets.

@svemat01
Created March 27, 2025 17:52
Show Gist options
  • Select an option

  • Save svemat01/df99bb4a5ae88359eac52b6cface8b0e to your computer and use it in GitHub Desktop.

Select an option

Save svemat01/df99bb4a5ae88359eac52b6cface8b0e to your computer and use it in GitHub Desktop.
The Great Vitest Mocking Mystery: A Tale of Pain and Redemption

The Great Vitest Mocking Mystery: A Tale of Pain and Redemption

The Problem (aka "Why Am I Even a Developer?")

Picture this: Your tests were working perfectly fine. Life was good. Then you innocently add ONE import from cloudflare:test to your setup file, and suddenly ALL YOUR MOCKS ARE LYING TO YOU. They exist, but they do nothing. They're like ghost mocks, haunting your codebase with their useless presence.

// The import that ruined everything
import { env, applyD1Migrations } from "cloudflare:test";

The Symptoms (aka "I'm Losing My Mind")

  • All your mocks suddenly decide to go on vacation
  • Hours of your life wasted staring at perfectly valid mock code that just... doesn't... work
  • That growing feeling of imposter syndrome as you question every life decision that led you to this moment
  • Stack Overflow has no answers (the true mark of despair)
  • Your console.log debugging is starting to look like a cry for help

The Investigation (aka "The Descent Into Madness")

After sacrificing several cups of coffee and what felt like years of your life, you stumble upon this innocent little note in the Vitest wiki:

Vitest will not mock modules that were imported inside a setup file because they are cached by the time a test file is running.

WHAT. DO. YOU. MEAN.

Apparently, that single cloudflare:test import in your setup file decided to invite all its module friends over for a caching party, and now they're all too drunk on memory to be mocked properly.

The Solution (aka "Are You Kidding Me?")

Add THREE LINES OF CODE to your setup file:

// test/setup/setup.ts
import { env, applyD1Migrations } from "cloudflare:test";

// Your setup code here
await applyD1Migrations(env.DB, env["TEST_MIGRATIONS"]);

// The magic incantation that fixes everything
vi.resetModules(); // <-- THIS. THIS IS ALL IT NEEDED.

The Emotional Damage Report

  • Time lost: Several hours of your life you'll never get back
  • Hair pulled: Approximately 47 strands
  • Coffee consumed: [REDACTED] cups
  • Times questioned career choice: 23
  • Slack messages deleted before sending: 12 (they were not professional)

Lessons Learned

  1. Module caching is a silent killer
  2. The solution is always simpler than your 47 failed attempts to fix it
  3. When in doubt, vi.resetModules()
  4. Document everything because future you will thank past you for saving them from this particular circle of hell
  5. The Cloudflare Workers testing documentation is now your bedtime reading

In Conclusion

If you're reading this because you're experiencing the same issue, congratulations! You've found the solution much faster than the poor soul who wrote this documentation. If you're reading this out of curiosity, consider yourself warned. And remember, somewhere out there, a developer spent hours of their life so you wouldn't have to.

Dedicated to all the developers who have ever yelled "BUT IT SHOULD WORK!" at their screens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment