cloudflare / miniflare

🔥 Fully-local simulator for Cloudflare Workers. For the latest version, see https://github.com/cloudflare/workers-sdk/tree/main/packages/miniflare.
https://miniflare.dev
MIT License
3.78k stars 205 forks source link

Vitest bug: "Error: ENOENT: no such file or directory" #763

Closed amorey closed 3 months ago

amorey commented 6 months ago

When I run vitest with the miniflare environment I get a missing file error due to one of the build files not being present. Here's a minimal vite project that reproduces the error: https://github.com/amorey/miniflare-vitest-bug

git clone git@github.com:amorey/miniflare-vitest-bug.git
cd miniflare-vitest-bug
pnpm install
pnpm test

In this particular example, the error is that the build artifact dist/main.cjs is missing. If I remove the "main" definition from package.json, the error goes away.

penalosa commented 3 months ago

It looks like your project's package.json file points to dist/main.cjs, but that file doesn't exist. Did you generate the project from a template? It might be that you need to run the build script first (pnpm run build in your case).

In Miniflare v3 we've added a new way to run unit tests against your Workers using the actual Workers runtime (workerd), and that's what we recommend for projects going forward. You can try it out at https://developers.cloudflare.com/workers/testing/vitest-integration/

amorey commented 3 months ago

It looks like your project's package.json file points to dist/main.cjs, but that file doesn't exist. Did you generate the project from a template? It might be that you need to run the build script first (pnpm run build in your case).

The tests don't depend on the build files and running the tests in a node environment doesn't throw an error so why would running them in a miniflare environment throw an error?

In Miniflare v3 we've added a new way to run unit tests against your Workers using the actual Workers runtime (workerd), and that's what we recommend for projects going forward. You can try it out at https://developers.cloudflare.com/workers/testing/vitest-integration/

I migrated the example to Miniflare v3 and the original error went away but now I can't see how to integrate Miniflare v3 into the main project (https://github.com/kubetail-org/edge-csrf). In the main project, I'm testing the code in different environments by switching the vitest environment flag. For example, here's the test-all script definition in package.json:

"test-all": "vitest run --environment node && vitest run --environment edge-runtime && vitest run --environment miniflare"

However, miniflare v3 doesn't use the environment flag anymore. How can I implement the same behavior in v3?