Skip to content

Commit dfd033f

Browse files
docs: add ctx.exports documentation for vitest-pool-workers (#27037)
* docs: add ctx.exports documentation for vitest-pool-workers - Add link to context-exports fixture on recipes page - Add known issue about missing ctx.exports properties and additionalExports workaround Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com> * docs: add example of problematic code scenario for ctx.exports Address reviewer feedback by adding a brief example showing a wildcard re-export from a virtual module that cannot be automatically inferred. Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 3a40340 commit dfd033f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,40 @@ test('check if file exists', async () => {
6969
});
7070
```
7171

72+
### Missing properties on `ctx.exports`
73+
74+
The `ctx.exports` property provides access to the exports of the main (`SELF`) Worker. The Workers Vitest integration attempts to automatically infer these exports by statically analyzing the Worker source code using esbuild. However, complex build setups, such as those using virtual modules or wildcard re-exports that esbuild cannot follow, may result in missing properties on the `ctx.exports` object.
75+
76+
For example, consider a Worker that re-exports an entrypoint from a virtual module using a wildcard export:
77+
78+
```ts
79+
// index.ts
80+
export * from "@virtual-module";
81+
```
82+
83+
In this case, any exports from `@virtual-module` (such as `MyEntrypoint`) cannot be automatically inferred and will be missing from `ctx.exports`.
84+
85+
To work around this, add the `additionalExports` option to your Vitest configuration:
86+
87+
```ts
88+
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
89+
90+
export default defineWorkersConfig({
91+
test: {
92+
poolOptions: {
93+
workers: {
94+
wrangler: { configPath: "./wrangler.toml" },
95+
additionalExports: {
96+
MyEntrypoint: "WorkerEntrypoint",
97+
},
98+
},
99+
},
100+
},
101+
});
102+
```
103+
104+
The `additionalExports` option is a map where keys are the export names and values are the type of export (`"WorkerEntrypoint"`, `"DurableObject"`, or `"WorkflowEntrypoint"`).
105+
72106
### Module resolution
73107

74108
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:

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Recipes are examples that help demonstrate how to write unit tests and integrati
2424
- [Tests using multiple auxiliary Workers and request mocks](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/multiple-workers)
2525
- [Tests importing WebAssembly modules](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/web-assembly)
2626
- [Tests using JSRPC with entrypoints and Durable Objects](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/rpc)
27+
- [Tests using `ctx.exports` to access Worker exports](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/context-exports)
2728
- [Integration test with static assets and Puppeteer](https://github.com/GregBrimble/puppeteer-vitest-workers-assets)
2829
- [Resolving modules with Vite Dependency Pre-Bundling](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/module-resolution)
2930
- [Mocking Workers AI and Vectorize bindings in unit tests](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/ai-vectorize)

0 commit comments

Comments
 (0)