test(workerd): add tests for WebSocket (#2891)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent f634c82 · Browse files · 5 files·+57 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+57 added−1 removed
- tests:+38 added−0 removed
- other:+19 added−1 removed
Provenance
- Commit
786f9e2- Tree
2bc32e3- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
f634c82
5 files changed+57 added−1 removed
modified:runtime_tests/workerd/index.test.ts+38 added
123456721 unmodified lines2930313233343536373839404142434445464748495051525354555657585960616263646566676869import { unstable_dev } from 'wrangler'import type { UnstableDevWorker } from 'wrangler'import { WebSocket } from 'ws'
describe('workerd', () => { let worker: UnstableDevWorker
21 unmodified lines expect(res.status).toBe(200) expect(await res.text()).toBe('Hono') })})
describe('workerd with WebSocket', () => { // worker.fetch does not support WebSocket: // https://github.com/cloudflare/workers-sdk/issues/4573#issuecomment-1850420973 it('Should handle the WebSocket connection correctly', async () => { const worker = await unstable_dev('./runtime_tests/workerd/index.ts', { experimental: { disableExperimentalWarning: true }, }) const ws = new WebSocket(`ws://${worker.address}:${worker.port}/ws`)
const openHandler = vi.fn() const messageHandler = vi.fn() const closeHandler = vi.fn()
const waitForOpen = new Promise((resolve) => { ws.addEventListener('open', () => { openHandler() ws.send('Hello') resolve(undefined) }) ws.addEventListener('close', async () => { closeHandler() }) ws.addEventListener('message', async (event) => { messageHandler(event.data) ws.close() }) })
await waitForOpen await worker.stop()
expect(openHandler).toHaveBeenCalled() expect(messageHandler).toHaveBeenCalledWith('Hello') expect(closeHandler).toHaveBeenCalled() })})modified:runtime_tests/workerd/index.ts+12 added
1234563 unmodified lines10111213141516171819202122232425import { env, getRuntimeKey } from '../../src/helper/adapter'import { upgradeWebSocket } from '../../src/adapter/cloudflare-workers'import { Hono } from '../../src/hono'
const app = new Hono()
3 unmodified lines const { NAME } = env<{ NAME: string }>(c) return c.text(NAME)})
app.get( '/ws', upgradeWebSocket(() => { return { onMessage(event, ws) { ws.send(event.data as string) }, } }))
export default appmodified:bun.lockbbinary
A binary file, modified.
modified:package.json+1 added
592 unmodified lines593 "typescript": "^5.3.3",594 "vite-plugin-fastly-js-compute": "^0.4.2",595 "vitest": "^1.2.2",596 "wrangler": "^3.58.0",597+ "ws": "^8.17.0",598 "zod": "^3.20.2"599 },600 "engines": {601 "node": ">=16.0.0"2 unmodified linesmodified:yarn.lock+6 added-1 removed
1 # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.2 # yarn lockfile v13-# bun ./bun.lockb --hash: BAD784DA2D3E8912-7bb272adaca1c26f-B77747473A8BC735-f20e673ef1d530e33+# bun ./bun.lockb --hash: 733DAF3BDF9F84ED-89e11d5f0e9dc6ef-7974BC9F6F5A02AD-41e5e99e9db5457f4 5 6 "@aashutoshrathi/word-wrap@^1.2.3":7 version "1.2.6"6096 unmodified lines6104 version "8.16.0"6105 resolved "https://registry.npmjs.org/ws/-/ws-8.16.0.tgz"6106 integrity sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==6107 6108+ws@^8.17.0:6109+ version "8.17.0"6110+ resolved "https://registry.npmjs.org/ws/-/ws-8.17.0.tgz"6111+ integrity sha512-uJq6108EgZMAl20KagGkzCKfMEjxmKvZHG7Tlq0Z6nOky7YF7aq4mOx6xK8TJ/i1LeK4Qus7INktacctDgY8Ow==6112+6113 xdg-basedir@^4.0.0:6114 version "4.0.0"6115 resolved "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-4.0.0.tgz"6116 integrity sha512-PSNhEJDejZYV7h50BohL09Er9VaIefr2LMAf3OEmpCkjOi34eYyQYAXUTjEQtZJTKcF0E2UKTh+osDLsgNim9Q==77 unmodified lines