@@ -106,3 +106,103 @@ test("automatically re-runs integration tests", async ({
106
106
expect ( result . stdout ) . toMatch ( "Tests 1 passed" ) ;
107
107
} ) ;
108
108
} ) ;
109
+
110
+ test ( "automatically reset module graph" , async ( {
111
+ expect,
112
+ seed,
113
+ vitestDev,
114
+ } ) => {
115
+ await seed ( {
116
+ "vitest.config.mts" : dedent `
117
+ import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
118
+ export default defineWorkersConfig({
119
+ test: {
120
+ poolOptions: {
121
+ workers: {
122
+ main: "./index.ts",
123
+ singleWorker: true,
124
+ miniflare: {
125
+ compatibilityDate: "2024-01-01",
126
+ compatibilityFlags: ["nodejs_compat"],
127
+ },
128
+ },
129
+ },
130
+ }
131
+ });
132
+ ` ,
133
+ "answer.ts" : dedent `
134
+ export function getAnswer() {
135
+ return "wrong";
136
+ }
137
+ ` ,
138
+ "index.ts" : dedent `
139
+ import { getAnswer } from "./answer";
140
+
141
+ export default {
142
+ async fetch(request, env, ctx) {
143
+ const answer = getAnswer();
144
+ return new Response(answer);
145
+ }
146
+ }
147
+ ` ,
148
+ "index.test.ts" : dedent `
149
+ import { env, createExecutionContext, waitOnExecutionContext } from "cloudflare:test";
150
+ import { it, expect, vi } from "vitest";
151
+ import worker from "./index";
152
+ import { getAnswer } from './answer';
153
+
154
+ vi.mock('./answer');
155
+
156
+ it("mocks module properly", async () => {
157
+ vi.mocked(getAnswer).mockReturnValue("correct");
158
+
159
+ const request = new Request("https://example.com");
160
+ const ctx = createExecutionContext();
161
+ const response = await worker.fetch(request, env, ctx);
162
+ await waitOnExecutionContext(ctx);
163
+ expect(await response.text()).toBe("correct");
164
+ });
165
+ ` ,
166
+ } ) ;
167
+ const result = vitestDev ( ) ;
168
+
169
+ await waitFor ( ( ) => {
170
+ expect ( result . stdout ) . toMatch ( "Tests 1 passed" ) ;
171
+ } ) ;
172
+
173
+ // Trigger a re-run by updating the test file with an extra test.
174
+ await seed ( {
175
+ "index.test.ts" : dedent `
176
+ import { env, createExecutionContext, waitOnExecutionContext } from "cloudflare:test";
177
+ import { it, expect, vi } from "vitest";
178
+ import worker from "./index";
179
+ import { getAnswer } from './answer';
180
+
181
+ vi.mock('./answer');
182
+
183
+ it("mocks module properly", async () => {
184
+ vi.mocked(getAnswer).mockReturnValue("correct");
185
+
186
+ const request = new Request("https://example.com");
187
+ const ctx = createExecutionContext();
188
+ const response = await worker.fetch(request, env, ctx);
189
+ await waitOnExecutionContext(ctx);
190
+ expect(await response.text()).toBe("correct");
191
+ });
192
+
193
+ it("mocks module properly when re-run in watch mode", async () => {
194
+ vi.mocked(getAnswer).mockReturnValue("test");
195
+
196
+ const request = new Request("https://example.com");
197
+ const ctx = createExecutionContext();
198
+ const response = await worker.fetch(request, env, ctx);
199
+ await waitOnExecutionContext(ctx);
200
+ expect(await response.text()).toBe("test");
201
+ });
202
+ ` ,
203
+ } ) ;
204
+
205
+ await waitFor ( ( ) => {
206
+ expect ( result . stdout ) . toMatch ( "Tests 2 passed" ) ;
207
+ } ) ;
208
+ } ) ;
0 commit comments