-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Support Vitest 4 in @cloudflare/vitest-pool-workers
#11632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f649413
3afc4f6
964012e
e1f2a8b
cfc5f70
704466b
99a88c9
3e60beb
9dec2c5
3037a20
c6af16d
64982d4
c4ec1a8
adc5e54
1149696
1625d85
e80bd62
9f80b75
d317d15
20eb0a6
2f511be
7143d5d
de5f57f
7870094
3f6fef8
131244a
1c67bfe
858ed07
79a1932
d46a09b
1f1072a
a925f45
36bdd7b
522b117
6fdaa56
177e94c
2e76e3c
a63af5a
ccaf85f
ebb56c4
b5561cf
5764732
6324643
8791f8b
4bb7043
a8a1a8c
8ca9dad
d3ef674
c4e2a7c
ef2ba23
ac94357
989f559
8003a66
fe26fd9
31a64e5
3c40844
017ad0c
755f295
6cce159
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| "@cloudflare/vitest-pool-workers": minor | ||
| --- | ||
|
|
||
| Support Vitest 4 in `@cloudflare/vitest-pool-workers`. | ||
|
|
||
| This a breaking change to the `@cloudflare/vitest-pool-workers` integration in order to support Vitest v4. Along with supporting Vitest v4 (and dropping support for Vitest v2 and v3), we've made a number of changes that may require changes to your tests. Our aim has been to improve stability & the foundations of `@cloudflare/vitest-pool-workers` as we move towards a v1 release of the package. | ||
|
|
||
| We've made a codemod to make the migration easier: `wrangler codemod vitest-pool-v3-to-v4`, which will make the required changes to your config file. | ||
|
|
||
| - **`isolatedStorage` & `singleWorker`:** These have been removed in favour of a simpler isolation model that more closely matches Vitest. Storage isolation is now on a per test file basis, and you can make your test files share the same storage by using the Vitest flags `--max-workers=1 --no-isolate` | ||
| - **`import { env, SELF } from "cloudflare:test"`:** These have been removed in favour of `import { env, exports } from "cloudflare:workers"`. `exports.default.fetch()` has the same behaviour as `SELF.fetch()`, except that it doesn't expose Assets. To test your assets, use the `env.ASSETS` bindings or write an integration test using [`startDevWorker()` (recommended)](https://developers.cloudflare.com/workers/testing/unstable_startworker/) | ||
| - **`import { fetchMock } from "cloudflare:test"`:** This has been removed. Instead, [mock `globalThis.fetch`](https://github.com/cloudflare/workers-sdk/blob/main/fixtures/vitest-pool-workers-examples/request-mocking/test/imperative.test.ts) or use ecosystem libraries like [MSW (recommended)](https://mswjs.io/). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "wrangler": minor | ||
| --- | ||
|
|
||
| Add `wrangler codemod vitest-pool-v3-to-v4` command to help migrate `@cloudflare/vitest-pool-workers` config files from v3 to v4. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| declare module "cloudflare:test" { | ||
| interface ProvidedEnv extends Env {} | ||
| declare namespace Cloudflare { | ||
| interface Env {} | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,15 @@ | ||
| import { defineWorkersProject } from "@cloudflare/vitest-pool-workers/config"; | ||
| import { cloudflareTest } from "@cloudflare/vitest-pool-workers"; | ||
| import { defineConfig } from "vitest/config"; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [ | ||
| cloudflareTest({ | ||
| remoteBindings: false, | ||
| wrangler: { configPath: "./wrangler.jsonc" }, | ||
| }), | ||
| ], | ||
|
|
||
| export default defineWorkersProject({ | ||
| test: { | ||
| globalSetup: ["./global-setup.ts"], | ||
| poolOptions: { | ||
| workers: { | ||
| singleWorker: true, | ||
| remoteBindings: false, | ||
| wrangler: { configPath: "./wrangler.jsonc" }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| declare module "cloudflare:test" { | ||
| // Controls the type of `import("cloudflare:test").env` | ||
| interface ProvidedEnv { | ||
| declare namespace Cloudflare { | ||
| interface Env { | ||
| WORKER: Fetcher; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,43 @@ | ||
| import { defineWorkersProject } from "@cloudflare/vitest-pool-workers/config"; | ||
| import { cloudflareTest } from "@cloudflare/vitest-pool-workers"; | ||
| import { defineConfig } from "vitest/config"; | ||
|
|
||
| export default defineWorkersProject({ | ||
| test: { | ||
| globalSetup: ["./global-setup.ts"], | ||
| poolOptions: { | ||
| workers: { | ||
| singleWorker: true, | ||
| miniflare: { | ||
| // Configuration for the test runner Worker | ||
| compatibilityDate: "2024-01-01", | ||
| compatibilityFlags: [ | ||
| // This illustrates a Worker that in production only wants v1 of Node.js compatibility. | ||
| // The Vitest pool integration will need to remove this flag since the `MockAgent` requires v2. | ||
| "no_nodejs_compat_v2", | ||
| "nodejs_compat", | ||
| // Required to use `WORKER.scheduled()`. This is an experimental | ||
| // compatibility flag, and cannot be enabled in production. | ||
| "service_binding_extra_handlers", | ||
| ], | ||
| serviceBindings: { | ||
| WORKER: "worker-under-test", | ||
| }, | ||
|
|
||
| workers: [ | ||
| // Configuration for the "auxiliary" Worker under test. | ||
| // Unfortunately, auxiliary Workers cannot load their configuration | ||
| // from `wrangler.toml` files, and must be configured with Miniflare | ||
| // `WorkerOptions`. | ||
| { | ||
| name: "worker-under-test", | ||
| modules: true, | ||
| scriptPath: "./dist/index.js", // Built by `global-setup.ts` | ||
| compatibilityDate: "2024-01-01", | ||
| compatibilityFlags: ["nodejs_compat"], | ||
| }, | ||
| ], | ||
| export default defineConfig({ | ||
| plugins: [ | ||
| cloudflareTest({ | ||
| miniflare: { | ||
| // Configuration for the test runner Worker | ||
| compatibilityDate: "2024-01-01", | ||
| compatibilityFlags: [ | ||
| // This illustrates a Worker that in production only wants v1 of Node.js compatibility. | ||
| // The Vitest pool integration will need to remove this flag since the `MockAgent` requires v2. | ||
| "no_nodejs_compat_v2", | ||
| "nodejs_compat", | ||
| // Required to use `WORKER.scheduled()`. This is an experimental | ||
| // compatibility flag, and cannot be enabled in production. | ||
| "service_binding_extra_handlers", | ||
| ], | ||
| serviceBindings: { | ||
| WORKER: "worker-under-test", | ||
| }, | ||
|
|
||
| workers: [ | ||
| // Configuration for the "auxiliary" Worker under test. | ||
| // Unfortunately, auxiliary Workers cannot load their configuration | ||
| // from `wrangler.toml` files, and must be configured with Miniflare | ||
| // `WorkerOptions`. | ||
| { | ||
| name: "worker-under-test", | ||
| modules: true, | ||
| scriptPath: "./dist/index.js", // Built by `global-setup.ts` | ||
| compatibilityDate: "2024-01-01", | ||
| compatibilityFlags: ["nodejs_compat"], | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| }), | ||
| ], | ||
|
|
||
| test: { | ||
| globalSetup: ["./global-setup.ts"], | ||
| }, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,19 @@ | ||
| import { defineWorkersProject } from "@cloudflare/vitest-pool-workers/config"; | ||
| import { cloudflareTest } from "@cloudflare/vitest-pool-workers"; | ||
| import { defineConfig } from "vitest/config"; | ||
|
|
||
| export default defineWorkersProject({ | ||
| test: { | ||
| poolOptions: { | ||
| workers: { | ||
| singleWorker: true, | ||
| miniflare: { | ||
| // Required to use `SELF.scheduled()`. This is an experimental | ||
| // compatibility flag, and cannot be enabled in production. | ||
| compatibilityFlags: ["service_binding_extra_handlers"], | ||
| }, | ||
| wrangler: { | ||
| configPath: "./wrangler.jsonc", | ||
| }, | ||
| export default defineConfig({ | ||
| plugins: [ | ||
| cloudflareTest({ | ||
| miniflare: { | ||
| // Required to use `SELF.scheduled()`. This is an experimental | ||
| // compatibility flag, and cannot be enabled in production. | ||
| compatibilityFlags: ["service_binding_extra_handlers"], | ||
| }, | ||
| }, | ||
| }, | ||
| wrangler: { | ||
| configPath: "./wrangler.jsonc", | ||
| }, | ||
| }), | ||
| ], | ||
|
|
||
| test: {}, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| declare module "cloudflare:test" { | ||
| // Controls the type of `import("cloudflare:test").env` | ||
| interface ProvidedEnv extends Env { | ||
| declare namespace Cloudflare { | ||
| interface Env { | ||
| MY_CONTAINER: DurableObjectNamespace<MyContainer>; | ||
| } | ||
| } | ||
|
Comment on lines
+1
to
5
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand this file was here to provide the Env type for If it's no longer necessary, it might be worth cleaning this up, especially in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was, yes. However, it now provided the |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| import { defineWorkersProject } from "@cloudflare/vitest-pool-workers/config"; | ||
| import { cloudflareTest } from "@cloudflare/vitest-pool-workers"; | ||
| import { defineConfig } from "vitest/config"; | ||
|
|
||
| export default defineWorkersProject({ | ||
| test: { | ||
| poolOptions: { | ||
| workers: { | ||
| singleWorker: true, | ||
| wrangler: { configPath: "./wrangler.jsonc" }, | ||
| }, | ||
| }, | ||
| }, | ||
| export default defineConfig({ | ||
| plugins: [ | ||
| cloudflareTest({ | ||
| wrangler: { configPath: "./wrangler.jsonc" }, | ||
| }), | ||
| ], | ||
|
|
||
| test: {}, | ||
| }); |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.