mirror of
https://github.com/honojs/hono.git
synced 2024-11-25 13:19:30 +01:00
dfbd717263
* chore: rename `runtime_tests` to `runtime-tests` * fixed GitHub workflow
70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import { unstable_dev } from 'wrangler'
|
|
import type { UnstableDevWorker } from 'wrangler'
|
|
import { WebSocket } from 'ws'
|
|
|
|
describe('workerd', () => {
|
|
let worker: UnstableDevWorker
|
|
|
|
beforeAll(async () => {
|
|
worker = await unstable_dev('./runtime-tests/workerd/index.ts', {
|
|
vars: {
|
|
NAME: 'Hono',
|
|
},
|
|
experimental: { disableExperimentalWarning: true },
|
|
})
|
|
})
|
|
|
|
afterAll(async () => {
|
|
await worker.stop()
|
|
})
|
|
|
|
it('Should return 200 response with the runtime key', async () => {
|
|
const res = await worker.fetch('/')
|
|
expect(res.status).toBe(200)
|
|
expect(await res.text()).toBe('Hello from workerd')
|
|
})
|
|
|
|
it('Should return 200 response with the environment variable', async () => {
|
|
const res = await worker.fetch('/env')
|
|
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()
|
|
})
|
|
})
|