Skip to content

Commit 635604d

Browse files
edmundhungOxyjun
andauthored
[Vitest Integration] add recipe for resolving modules with Vite Dependency Pre-Bundling (#19835)
Co-authored-by: Jun Lee <[email protected]>
1 parent fc27f3f commit 635604d

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/content/docs/workers/testing/vitest-integration/known-issues.mdx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,31 @@ Using WebSockets with Durable Objects with the [`isolatedStorage`](/workers/test
3333
### Returning non-primitive values from RPC methods
3434

3535
In order to return non-primitive values (objects or classes extending `RpcTarget`, for instance) from RPC methods, you must use the `using` keyword. Refer to [https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle#explicit-resource-management](https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle#explicit-resource-management) for more details. An [example test](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/rpc/test/unit.test.ts#L155) is included in the `workers-sdk` repository.
36+
37+
### Module resolution
38+
39+
If you encounter module resolution issues such as: `Error: Cannot use require() to import an ES Module` or `Error: No such module`, you can bundle these dependencies using the [deps.optimizer](https://vitest.dev/config/#deps-optimizer) option:
40+
41+
```tsx
42+
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
43+
44+
export default defineWorkersConfig({
45+
test: {
46+
deps: {
47+
optimizer: {
48+
ssr: {
49+
enabled: true,
50+
include: ["your-package-name"],
51+
},
52+
},
53+
},
54+
poolOptions: {
55+
workers: {
56+
// ...
57+
},
58+
},
59+
},
60+
});
61+
```
62+
63+
You can find an example in the [Recipes](/workers/testing/vitest-integration/recipes) page.

src/content/docs/workers/testing/vitest-integration/recipes.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Recipes are examples that help demonstrate how to write unit tests and integrati
1212

1313
- [Basic unit and integration tests using `SELF`](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/basics-unit-integration-self)
1414
- [Basic integration tests using an auxiliary Worker](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/basics-integration-auxiliary)
15+
- [Basic integration test for a Worker with static assets](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/workers-assets)
1516
- [Isolated tests using KV, R2 and the Cache API](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/kv-r2-caches)
1617
- [Isolated tests using D1 with migrations](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/d1)
1718
- [Isolated tests using Durable Objects with direct access](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/durable-objects)
@@ -21,5 +22,5 @@ Recipes are examples that help demonstrate how to write unit tests and integrati
2122
- [Tests using multiple auxiliary Workers and request mocks](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/multiple-workers)
2223
- [Tests importing WebAssembly modules](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/web-assembly)
2324
- [Tests using JSRPC with entrypoints and Durable Objects](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/rpc)
24-
- [Basic integration test for a Worker with static assets](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/workers-assets)
2525
- [Integration test with static assets and Puppeteer](https://github.com/GregBrimble/puppeteer-vitest-workers-assets)
26+
- [Resolving modules with Vite Dependency Pre-Bundling](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/module-resolution)

0 commit comments

Comments
 (0)