1+ /* eslint-disable es/no-dynamic-import */
12// noinspection ES6ConvertVarToLetConst
23
34import stream from "stream/web" ;
@@ -8,51 +9,45 @@ import {
89 StackedMemoryStorageFactory ,
910 createMiniflareEnvironment ,
1011} from "@miniflare/shared-test-environment" ;
11- import {
12- Environment ,
13- SuiteAPI ,
14- SuiteFactory ,
15- afterAll ,
16- afterEach ,
17- beforeAll ,
18- beforeEach ,
19- describe ,
20- } from "vitest" ;
21- import { createChainable } from "./chain" ;
12+ import { createChainable } from "@vitest/runner/utils" ;
13+ import type { Environment , SuiteAPI , SuiteFactory , describe } from "vitest" ;
2214
2315const scriptRunner = new VMScriptRunner ( ) ;
2416const queueBroker = new QueueBroker ( ) ;
2517
26- function setupIsolatedStorage ( storageFactory : StackedMemoryStorageFactory ) {
18+ function setupIsolatedStorage (
19+ vitestImpl : typeof import ( "vitest" ) ,
20+ storageFactory : StackedMemoryStorageFactory
21+ ) {
2722 // `push()`/`pop()` at the start/end of each test
28- beforeEach ( ( ) => storageFactory . push ( ) ) ;
29- afterEach ( ( ) => storageFactory . pop ( ) ) ;
23+ vitestImpl . beforeEach ( ( ) => storageFactory . push ( ) ) ;
24+ vitestImpl . afterEach ( ( ) => storageFactory . pop ( ) ) ;
3025
3126 // `push()`/`pop()` at the start/end of each `describe` block
3227 // (users must use the returned `describe` function instead of the default
3328 // `describe`/`suite` from the `vitest` module)
3429 const wrappedDescribeFn : typeof describe . fn = function ( name , factory ) {
3530 if ( typeof factory !== "function" ) {
36- return describe . fn . call ( this , name , factory ) ;
31+ return vitestImpl . describe . fn . call ( this , name , factory ) ;
3732 }
3833 const newFactory : SuiteFactory = ( test ) => {
39- beforeAll ( ( ) => storageFactory . push ( ) ) ;
40- afterAll ( ( ) => storageFactory . pop ( ) ) ;
34+ vitestImpl . beforeAll ( ( ) => storageFactory . push ( ) ) ;
35+ vitestImpl . afterAll ( ( ) => storageFactory . pop ( ) ) ;
4136 return factory ( test ) ;
4237 } ;
43- return describe . fn . call ( this , name , newFactory ) ;
38+ return vitestImpl . describe . fn . call ( this , name , newFactory ) ;
4439 } ;
4540
4641 // https://github.com/vitest-dev/vitest/blob/69d55bc19c8ca6e1dfb28724eb55a45aefc37562/packages/vitest/src/runtime/suite.ts#L204-L215
4742 const wrappedDescribe = wrappedDescribeFn as typeof describe ;
48- wrappedDescribe . each = describe . each ;
43+ wrappedDescribe . each = vitestImpl . describe . each ;
4944 wrappedDescribe . skipIf = ( condition ) =>
5045 ( condition ? wrappedChainable . skip : wrappedChainable ) as SuiteAPI ;
5146 wrappedDescribe . runIf = ( condition ) =>
5247 ( condition ? wrappedChainable : wrappedChainable . skip ) as SuiteAPI ;
5348
49+ // https://github.com/vitest-dev/vitest/blob/e691a9ca229dd84765a4b40192761ffc1827069c/packages/runner/src/suite.ts#L228
5450 const wrappedChainable = createChainable (
55- // https://github.com/vitest-dev/vitest/blob/69d55bc19c8ca6e1dfb28724eb55a45aefc37562/packages/vitest/src/runtime/suite.ts#L217-L220
5651 [ "concurrent" , "shuffle" , "skip" , "only" , "todo" ] ,
5752 wrappedDescribe
5853 ) ;
@@ -70,7 +65,10 @@ declare global {
7065
7166export default < Environment > {
7267 name : "miniflare" ,
68+ transformMode : "ssr" ,
7369 async setup ( global , options ) {
70+ const vitestImpl = await import ( "vitest" ) ;
71+
7472 // Since `[email protected] `, stream classes are loaded from the global scope 7573 // if available (https://github.com/nodejs/undici/pull/1793). Make sure
7674 // `undici` sets module variables for stream classes before we assign
@@ -79,7 +77,8 @@ export default <Environment>{
7977 globalThis . ReadableStream = stream . ReadableStream ;
8078 globalThis . WritableStream = stream . WritableStream ;
8179 globalThis . TransformStream = stream . TransformStream ;
82- require ( "undici/lib/fetch" ) ;
80+ // @ts -expect-error `undici` doesn't provide type definitions for internals
81+ await import ( "undici/lib/fetch/index.js" ) ;
8382
8483 // Create a Miniflare instance
8584 const storageFactory = new StackedMemoryStorageFactory ( ) ;
@@ -91,7 +90,7 @@ export default <Environment>{
9190
9291 // Attach isolated storage setup function
9392 mfGlobalScope . setupMiniflareIsolatedStorage = ( ) =>
94- setupIsolatedStorage ( storageFactory ) ;
93+ setupIsolatedStorage ( vitestImpl , storageFactory ) ;
9594
9695 // `crypto` is defined as a getter on the global scope in Node 19+,
9796 // so attempting to set it with `Object.assign()` would fail. Instead,
0 commit comments