Skip to content

Instantly share code, notes, and snippets.

@dan-lee
Last active December 4, 2021 19:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dan-lee/aac354cd5012666deaef744ac5ed7b52 to your computer and use it in GitHub Desktop.
Save dan-lee/aac354cd5012666deaef744ac5ed7b52 to your computer and use it in GitHub Desktop.
miniflare: The requested module 'ws' does not provide an export named 'WebSocketServer'
import NodeEnvironment from 'jest-environment-node'
export default class CustomTestEnvironment extends NodeEnvironment {
async setup() {
await super.setup()
;['atob', 'btoa', 'AbortSignal'].forEach((fn) => {
if (typeof this.global[fn] === 'undefined') {
this.global[fn] = global[fn]
}
})
}
}
import { Miniflare } from 'miniflare'
test('miniflare', async () => {
const mf = new Miniflare({
script: `export default {
async fetch() {
return new Response('Ok')
}
}`,
modules: true,
})
const res = await mf.dispatchFetch('file:')
expect(await res.text()).toBe('Ok')
})
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
const config = {
testEnvironment: '<rootDir>/custom-test-env.mjs',
verbose: true,
preset: 'ts-jest/presets/default-esm',
globals: { 'ts-jest': { useESM: true } },
transform: {},
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
}
export default config
{
"name": "tmp",
"version": "1.0.0",
"type": "module",
"license": "MIT",
"scripts": {
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"postinstall": "patch-package"
},
"devDependencies": {
"jest": "27.3.1",
"jest-environment-node": "27.3.1",
"miniflare": "2.0.0-next.2",
"patch-package": "6.4.7",
"ts-jest": "27.0.7"
}
}
diff --git a/node_modules/@miniflare/http-server/dist/src/index.js b/node_modules/@miniflare/http-server/dist/src/index.js
index 69f51d0..e852c31 100644
--- a/node_modules/@miniflare/http-server/dist/src/index.js
+++ b/node_modules/@miniflare/http-server/dist/src/index.js
@@ -26,7 +26,7 @@ import {
import { randomHex } from "@miniflare/shared";
import { coupleWebSocket } from "@miniflare/web-sockets";
import { Headers } from "undici";
-import { WebSocketServer } from "ws";
+import { WebSocketServer } from "ws/wrapper.mjs";
// packages/http-server/src/helpers.ts
import { networkInterfaces } from "os";
{
"compilerOptions": {
"moduleResolution": "node",
"lib": ["esnext"],
"esModuleInterop": true,
"target": "es2020",
"strict": true,
"skipLibCheck": true,
"module": "esnext",
"types": ["jest"]
},
"include": ["src"],
"exclude": ["node_modules", "dist/"]
}
@metawrap-dev
Copy link

Have you found a solution to this? I'm stuck with the same issue.

@dan-lee
Copy link
Author

dan-lee commented Dec 4, 2021

@metawrap-dev
Copy link

Thanks! I was having this issue with another project and was trying to work out if it was a problem with ws (recent changes there with @types/ws with a similar issue) or a bug in jest, but it looks like you figured it out already. Much appreciated!

Do you know if the jest team is aware of this issue?

@dan-lee
Copy link
Author

dan-lee commented Dec 4, 2021

No sorry, I don't know if they are aware. You might report it to them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment