Last active
December 4, 2021 19:01
-
-
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'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
} | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"compilerOptions": { | |
"moduleResolution": "node", | |
"lib": ["esnext"], | |
"esModuleInterop": true, | |
"target": "es2020", | |
"strict": true, | |
"skipLibCheck": true, | |
"module": "esnext", | |
"types": ["jest"] | |
}, | |
"include": ["src"], | |
"exclude": ["node_modules", "dist/"] | |
} |
Hey, @metawrap-dev I think this was fixed in miniflare@v2.0.0-rc.2
The import now looks like this: https://github.com/cloudflare/miniflare/blob/666a69433f6c3131b6f85ad746207846cc5c04e9/packages/http-server/src/index.ts#L337-L341
Also the patch in this gist should fix it temporarily
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?
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
Have you found a solution to this? I'm stuck with the same issue.