Skip to content
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

🚀 Feature Request: per-test environment #7339

Open
terinjokes opened this issue Nov 25, 2024 · 1 comment
Open

🚀 Feature Request: per-test environment #7339

terinjokes opened this issue Nov 25, 2024 · 1 comment
Labels
enhancement New feature or request vitest Relating to the Workers Vitest integration

Comments

@terinjokes
Copy link

Describe the solution

There are three arguments to fetch: the incoming Request, the environment, and an ExecutionContext. The request is usually constructed per-test (and is a primary input to the code under test), the context is also created per-test using the createExecutionContext function exported from cloudflare:test. The lifecycle of these are clear and understandable.

The environment, on the other hand, is a global, imported from cloudflare:test and reused across all tests. While storage (like KV) is reset between tests, the env object is not, nor is it frozen. Mutations from one test affect all others that run afterwards.

I would like a function (tentatively and unimaginatively) named createEnvironment. This function can be used to create an environment for each test, where mutations have a clear lifecycle.

import { createEnvironment } from "cloudflare:test";
import { test } from "vitest";

test('modification a', ({ expect }) => {
  const env = createEnvironment();
  env.token = "test";
  expect(env.token).toBe("test");
})
  
test('no modification', ({ expect }) => {
  const env = createEnvironment();
  expect(env.token).toBeUndefined();
})
@terinjokes terinjokes added the enhancement New feature or request label Nov 25, 2024
@github-project-automation github-project-automation bot moved this to Untriaged in workers-sdk Nov 25, 2024
@GregBrimble
Copy link
Contributor

I've needed this several times myself. An official API here would make things much easier! Please also consider taking relevant Miniflare workerOptions here as parameters. It would be great to be able to override these on a per-test basis rather than being restricted to just a single global implementation in vitest.config.mts.

@emily-shen emily-shen added the vitest Relating to the Workers Vitest integration label Nov 28, 2024
@emily-shen emily-shen removed this from workers-sdk Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request vitest Relating to the Workers Vitest integration
Projects
None yet
Development

No branches or pull requests

3 participants