Skip to content

Commit 7f87cd2

Browse files
committed
Add review suggestions (miniflare do plugin, dispose introspectors)
1 parent 0b82842 commit 7f87cd2

File tree

5 files changed

+67
-17
lines changed

5 files changed

+67
-17
lines changed

packages/miniflare/src/plugins/do/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const DurableObjectsOptionsSchema = z.object({
3434
unsafeUniqueKey: z
3535
.union([z.string(), z.literal(kUnsafeEphemeralUniqueKey)])
3636
.optional(),
37-
unsafeScriptName: z.string().optional(),
37+
unsafeScriptName: z.boolean().optional(),
3838
// Prevents the Durable Object being evicted.
3939
unsafePreventEviction: z.boolean().optional(),
4040
remoteProxyConnectionString: z
@@ -72,7 +72,7 @@ export function normaliseDurableObject(
7272
: undefined;
7373
const serviceName =
7474
isObject && designator.unsafeScriptName
75-
? designator.unsafeScriptName
75+
? designator.scriptName
7676
: scriptName
7777
? getUserServiceName(scriptName)
7878
: undefined;

packages/miniflare/src/plugins/workflows/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ export const WORKFLOWS_PLUGIN: Plugin<
106106
className: "Engine",
107107
enableSql: true,
108108
uniqueKey,
109-
// Note(osilva): the engine should not be prevented from eviction if we wish
110-
// to abort it during tests (vitest-pool-workers)
111-
// preventEviction: true,
109+
preventEviction: true,
112110
},
113111
],
114112
durableObjectStorage: {

packages/vitest-pool-workers/src/pool/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,8 @@ Workflows defined in project: ${workflowClassNames.join(", ")}`);
538538
const engineName = `${WORKFLOW_ENGINE_BINDING}${value.name.toUpperCase()}`;
539539
runnerWorker.durableObjects[engineName] = {
540540
className: "Engine",
541-
unsafeScriptName: `workflows:${value.name}`,
541+
scriptName: `workflows:${value.name}`,
542+
unsafeScriptName: true,
542543
unsafeUniqueKey: `miniflare-workflows-${value.name}`,
543544
};
544545
}

packages/vitest-pool-workers/src/worker/workflows.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ class WorkflowInstanceIntrospectorHandle
103103
// do nothing because we want to clean up this instance
104104
}
105105
}
106+
107+
async [Symbol.asyncDispose](): Promise<void> {
108+
await this.cleanUp();
109+
}
106110
}
107111

108112
// See public facing `cloudflare:test` types for docs
@@ -111,7 +115,7 @@ export interface WorkflowIntrospector {
111115

112116
get(): WorkflowInstanceIntrospector[];
113117

114-
cleanUp(): void;
118+
cleanUp(): Promise<void>;
115119
}
116120

117121
export async function introspectWorkflow(
@@ -250,9 +254,17 @@ class WorkflowIntrospectorHandle implements WorkflowIntrospector {
250254
return this.#instanceIntrospectors;
251255
}
252256

253-
cleanUp(): void {
257+
async cleanUp(): Promise<void> {
258+
// also cleans all instance introspectors
259+
await Promise.all(
260+
this.#instanceIntrospectors.map((introspector) => introspector.cleanUp())
261+
);
254262
this.#modifierCallbacks = [];
255263
this.#instanceIntrospectors = [];
256264
this.#cleanupCallback();
257265
}
266+
267+
async [Symbol.asyncDispose](): Promise<void> {
268+
await this.cleanUp();
269+
}
258270
}

packages/vitest-pool-workers/types/cloudflare-test.d.ts

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,28 @@ declare module "cloudflare:test" {
200200
waitForStatus(status: InstanceStatus["status"]): Promise<void>;
201201

202202
/**
203-
* Cleans up the Workflow instance.
203+
* Cleans the Workflow instance introspector.
204+
*
204205
* This is crucial for ensuring test isolation by preventing state from
205-
* leaking between tests. It's best practice to call this in an `afterEach`
206-
* hook or at the end of every test.
206+
* leaking between tests. It should be called at the end or after each test.
207207
*/
208208
cleanUp(): Promise<void>;
209+
210+
/**
211+
* An alias for {@link cleanUp} to support automatic disposal with the `using` keyword.
212+
* This is an alternative to calling `cleanUp()` in an `afterEach` hook.
213+
*
214+
* @see {@link cleanUp}
215+
* @example
216+
* it('my workflow test', async () => {
217+
* await using instance = await introspectWorkflowInstance(env.WORKFLOW, "123456");
218+
*
219+
* // ... your test logic ...
220+
*
221+
* // .cleanUp() is automatically called here at the end of the scope
222+
* });
223+
*/
224+
[Symbol.asyncDispose](): Promise<void>;
209225
}
210226

211227
/**
@@ -406,19 +422,42 @@ declare module "cloudflare:test" {
406422
* @param fn - An async callback that receives a `WorkflowInstanceModifier` object.
407423
*/
408424
modifyAll(fn: (m: WorkflowInstanceModifier) => Promise<void>): void;
425+
409426
/**
410-
* Returns all `WorkflowInstanceIntrospectors` from Workflow instances
427+
* Returns all `WorkflowInstanceIntrospector`s from Workflow instances
411428
* created after calling `introspectWorkflow`.
412429
*/
413430
get(): WorkflowInstanceIntrospector[];
414431

415432
/**
416-
* Cleans up the Workflow introspector.
417-
* This is crucial for ensuring that the introspection of a Workflow does
418-
* not get persisted across tests.
419-
* Call this in an `afterEach` hook or at the end of every test.
433+
*
434+
* Cleans the introspector and every `WorkflowInstanceIntrospector` from Workflow
435+
* instances created after calling `introspectWorkflow`.
436+
*
437+
* This function is essential for test isolation, ensuring that results from one
438+
* test do not leak into the next. It should be called at the end or after each test.
439+
*
440+
* **Note:** After cleanup, `introspectWorkflow()` must be called again to begin
441+
* a new introspection.
442+
*
443+
*/
444+
cleanUp(): Promise<void>;
445+
446+
/**
447+
* An alias for {@link cleanUp} to support automatic disposal with the `using` keyword.
448+
* This is an alternative to calling `cleanUp()` in an `afterEach` hook.
449+
*
450+
* @see {@link cleanUp}
451+
* @example
452+
* it('my workflow test', async () => {
453+
* await using workflowIntrospector = await introspectWorkflow(env.WORKFLOW);
454+
*
455+
* // ... your test logic ...
456+
*
457+
* // .cleanUp() is automatically called here at the end of the scope
458+
* });
420459
*/
421-
cleanUp(): void;
460+
[Symbol.asyncDispose](): Promise<void>;
422461
}
423462

424463
// Only require `params` and `data` to be specified if they're non-empty

0 commit comments

Comments
 (0)