#How to setup Miniflare + Typescript + Vitest (Durable Object)?

1 messages · Page 1 of 1 (latest)

torpid comet
#

I've been trying to setup my project for tests, but I keep getting this error whenever I run my tests:

Class "Test" for Durable Object "TEST" not found

Does anyone have an example project that I can refer to? Many thanks.

Packages:
"typescript": "^5.0.4" "esbuild": "^0.17.19" "miniflare": "^3.0.1" "vitest": "^0.31.1" "vitest-environment-miniflare": "^2.14.0" "wrangler": "^2.20.0"

Scripts:
"test": "npm run build && NODE_OPTIONS=--experimental-vm-modules vitest" "build": "esbuild --bundle --sourcemap --outdir=dist ./src/index.js"

In package.json:
"main": "./dist/index.js"

vitest.config.ts:
import { defineConfig } from "vitest/config"; export default defineConfig({ test: { environment: "miniflare", environmentOptions: { modules: true, scriptPath: "./dist/index.js", durableObjects: { TEST: "Test", }, bindings: { TEST: "Test" }, }, }, });

Error:
Error: Class "Test" for Durable Object "TEST" not found ❯ DurableObjectsPlugin.reload node_modules/@miniflare/shared-test-environment/node_modules/@miniflare/durable-objects/src/plugin.ts:352:15 ❯ MiniflareCore.#runAllReloads node_modules/@miniflare/shared-test-environment/node_modules/@miniflare/core/src/index.ts:705:24 ❯ MiniflareCore.#reload node_modules/@miniflare/shared-test-environment/node_modules/@miniflare/core/src/index.ts:861:13 ❯ MiniflareCore.getGlobalScope node_modules/@miniflare/shared-test-environment/node_modules/@miniflare/core/src/index.ts:1041:5 ❯ Proxy.createMiniflareEnvironment node_modules/@miniflare/shared-test-environment/src/index.ts:79:25 ❯ Object.setup node_modules/vitest-environment-miniflare/src/index.ts:79:33 ❯ withEnv node_modules/vitest/dist/entry.js:182:15 ❯ run node_modules/vitest/dist/entry.js:268:3 ❯ run node_modules/vitest/dist/worker.js:69:5 ❯ node_modules/tinypool/dist/esm/worker.js:109:20

torpid comet
#

Is anyone here using the same setup? Would you mind sharing your experience?

torpid comet
golden portal
#

Hey! 👋 Thank you for the minimal reproduction! 😃 The problems here are:

  • esbuild needs to be configured to output an ES module as opposed to an IIFE, so the Durable Object class is actually exported
  • wrangler shouldn't be imported when using vitest-environment-miniflare
  • Miniflare doesn't understand the wrangler.toml main field (which may be a TypeScript file, something Miniflare can't handle on its own), so this needs to be configured explicitly using scriptPath/modules in vitest.config.ts
    I'm very aware this is kinda confusing, and not a great experience. We're going to be working on improving this soon. 🙂
torpid comet
#

oh it worked!