Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 9f64855

Browse files
committed
Upgrade vitest-environment-miniflare for recent Vitest versions
- Mark `vitest-environment-miniflare` as `"type": "module"` - Use Vitest's own `createChainable()` - Delay importing `vitest` until needed Closes #645 Closes #661
1 parent 7e4d906 commit 9f64855

File tree

8 files changed

+1728
-318
lines changed

8 files changed

+1728
-318
lines changed

package-lock.json

Lines changed: 1679 additions & 218 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"lint:fix": "npm run lint -- --fix",
2727
"prepublishOnly": "npm run lint && npm run clean && npm run build && npm run types:bundle && npm run test",
2828
"helpers:scaffold": "node scripts/scaffold.mjs",
29-
"helpers:version": "node scripts/version.mjs"
29+
"helpers:version": "node scripts/version.mjs",
30+
"postinstall": "patch-package"
3031
},
3132
"devDependencies": {
3233
"@ava/typescript": "^2.0.0",
@@ -47,6 +48,7 @@
4748
"eslint-plugin-import": "^2.24.2",
4849
"eslint-plugin-prettier": "^3.4.1",
4950
"esm": "^3.2.25",
51+
"patch-package": "^8.0.0",
5052
"prettier": "^2.3.2",
5153
"rimraf": "^3.0.2",
5254
"sinon": "^14.0.0",

packages/vitest-environment-miniflare/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
],
1212
"author": "MrBBot <[email protected]>",
1313
"license": "MIT",
14+
"type": "module",
1415
"main": "./dist/src/index.js",
15-
"module": "./dist/src/index.js",
1616
"types": "./dist/src/index.d.ts",
1717
"files": [
1818
"dist/src",
@@ -50,6 +50,6 @@
5050
"@miniflare/shared-test": "2.14.0",
5151
"miniflare": "2.14.0",
5252
"react-dom": "^18.1.0",
53-
"vitest": "^0.23.1"
53+
"vitest": "^0.34.3"
5454
}
5555
}

packages/vitest-environment-miniflare/src/chain.ts

Lines changed: 0 additions & 73 deletions
This file was deleted.

packages/vitest-environment-miniflare/src/index.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable es/no-dynamic-import */
12
// noinspection ES6ConvertVarToLetConst
23

34
import 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

2315
const scriptRunner = new VMScriptRunner();
2416
const 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

7166
export 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,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
diff --git a/node_modules/@ava/typescript/index.js b/node_modules/@ava/typescript/index.js
2+
index 8fc5369..f71c919 100644
3+
--- a/node_modules/@ava/typescript/index.js
4+
+++ b/node_modules/@ava/typescript/index.js
5+
@@ -167,7 +167,8 @@ module.exports = ({negotiateProtocol}) => {
6+
7+
const [from, to] = rewritePaths.find(([from]) => ref.startsWith(from));
8+
// TODO: Support JSX preserve mode — https://www.typescriptlang.org/docs/handbook/jsx.html
9+
- const rewritten = `${to}${ref.slice(from.length)}`.replace(testFileExtension, '.js');
10+
+ const jsExtension = from.includes("vitest-environment-miniflare") ? ".cjs" : ".js";
11+
+ const rewritten = `${to}${ref.slice(from.length)}`.replace(testFileExtension, jsExtension);
12+
return requireFn(rewritten);
13+
}
14+
};

scripts/build.mjs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ async function buildPackage(name) {
102102
);
103103
}
104104
// `vitest` requires custom environments to be ES modules with default exports
105-
if (name === "vitest-environment-miniflare") {
105+
const isVitestEnvironment = name === "vitest-environment-miniflare";
106+
if (isVitestEnvironment) {
106107
esmEntryPoints.unshift(indexPath);
107108
} else {
108109
cjsEntryPoints.unshift(indexPath);
@@ -120,7 +121,11 @@ async function buildPackage(name) {
120121
outdir: outPath,
121122
outbase: pkgRoot,
122123
};
123-
await esbuild.build({ ...pkgBuildOptions, entryPoints: cjsEntryPoints });
124+
await esbuild.build({
125+
...pkgBuildOptions,
126+
entryPoints: cjsEntryPoints,
127+
outExtension: isVitestEnvironment ? { ".js": ".cjs" } : undefined,
128+
});
124129
if (esmEntryPoints.length) {
125130
await esbuild.build({
126131
...pkgBuildOptions,

types/global.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ declare global {
44
var CryptoKey: typeof import("crypto").webcrypto.CryptoKey;
55
// eslint-disable-next-line no-var
66
var MessagePort: typeof import("worker_threads").MessagePort;
7+
// eslint-disable-next-line no-var
8+
type WebSocket = unknown;
79
}
810

911
export {};

0 commit comments

Comments
 (0)