square / svelte-store

528 stars 36 forks source link

enableStoreTestingMode always throws error, regardless of where it is located #65

Closed sureshjoshi closed 1 year ago

sureshjoshi commented 1 year ago

I've been trying to get enableStoreTestingMode running on my tests, however, I can't seem to figure out where it can be placed to not cause an exception.

I've tried to place it in my setup file, in various unit test files, or anywhere else. I'm wondering if it's related to when vitest runs its setupFiles. Maybe my application is being launched before setupFiles runs?

Working on a small reproduction repo.

Using vitest, in vite.config.ts, I have setup-dom-env.ts in setupFiles, so it should run before any of my tests:

test: {
      include: ["src/**/*.{test,spec}.{js,ts}"],
      environment: "jsdom",
      environmentMatchGlobs: [["**/*.dom.test.{js,ts}", "jsdom"]], // TODO: happy-dom not working with MSWjs
      globals: true,
      setupFiles: "./tests/setup-dom-env.ts",
    },

In setup-dom-test.ts:

// Enable Svelte Store testing mode (adds a "reset" method to all stores)
import { enableStoreTestingMode } from "@square/svelte-store";
enableStoreTestingMode();

---

Error: Testing mode MUST be enabled before store creation
 ❯ Module.enableStoreTestingMode node_modules/.pnpm/@square+svelte-store@1.0.15/node_modules/@square/svelte-store/src/config.ts:17:3
 ❯ tests/setup-dom-env.ts:10:1
      8| // Enable Svelte Store testing mode (adds a "reset" method to all stores)
      9| import { enableStoreTestingMode } from "@square/svelte-store";
     10| enableStoreTestingMode();

Alternate approach, using the beforeAll hook.

beforeAll(() => enableStoreTestingMode());

---

Error: Testing mode MUST be enabled before store creation
 ❯ Module.enableStoreTestingMode node_modules/.pnpm/@square+svelte-store@1.0.15/node_modules/@square/svelte-store/src/config.ts:17:3
 ❯ tests/setup-dom-env.ts:28:17
     28| beforeAll(() => enableStoreTestingMode());
sureshjoshi commented 1 year ago

I'm trying to figure out if it's related to vitest or something else, because creating a trivial reproduction case (e.g. Svelte skeleton app, upgraded to vitest 0.32) - everything works as expected.

Is there any sort of trace logs to flag where store creation occurs?

sureshjoshi commented 1 year ago

I started manually calling console.trace in the lib, and there seems to be some weird race condition, or maybe just an async condition:

// Enable Svelte Store testing mode (adds a "reset" method to all stores)
import { enableStoreTestingMode } from "@square/svelte-store";
enableStoreTestingMode()

// Import the i18n module and set the locale to "en"
import "$lib/i18n"; // Import to initialize.

Somehow the import of that library is beating out the call to enableStoreTestingMode...

I'm sure this comes down to how imports are handled, and how eager/lazily they're handled. Fortunately, the fix is easy (and more correct, to be honest) - which is to push my import of that i18n module (which uses a persisted store) into beforeAll.