test(react-renderer): supports streaming tests (#422)

pull/423/head
Yusuke Wada 2024-03-17 09:08:43 +09:00 committed by GitHub
parent 74dfa07ded
commit a5e59c4199
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 936 additions and 17 deletions

View File

@ -35,13 +35,15 @@
"hono": "*"
},
"devDependencies": {
"@cloudflare/vitest-pool-workers": "^0.1.2",
"@types/react": "^18",
"@types/react-dom": "^18.2.17",
"esbuild": "^0.20.2",
"hono": "^3.11.7",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"tsup": "^8.0.1",
"vitest": "^1.0.4"
"vitest": "1.3.0"
},
"engines": {
"node": ">=18"

View File

@ -1,4 +1,5 @@
import { Hono } from 'hono'
import { reactRenderer, useRequestContext } from '../src/react-renderer'
const RequestUrl = () => {
@ -107,5 +108,27 @@ describe('Basic', () => {
})
describe('Streaming', () => {
it.skip('Vitest does not support Streaming')
it('Should return a stream response', async () => {
const app = new Hono()
app.use(
'*',
reactRenderer(
({ children }) => {
return (
<html>
<body>{children}</body>
</html>
)
},
{ stream: true }
)
)
app.get('/', (c) => c.render(<h1>Hello</h1>, { title: 'Title' }))
const res = await app.request('/')
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(res.headers.get('Transfer-Encoding')).toBe('chunked')
expect(res.headers.get('Content-Type')).toBe('text/html; charset=UTF-8')
expect(await res.text()).toBe('<!DOCTYPE html><html><body><h1>Hello</h1></body></html>')
})
})

View File

@ -3,13 +3,14 @@
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"moduleResolution": "Bundler",
"rootDir": "./",
"outDir": "./dist",
"jsx": "react-jsx",
"jsxImportSource": "react"
},
"include": [
"vitest.config.ts",
"src/**/*.ts",
"test/**/*.tsx",
],

View File

@ -1,8 +1,15 @@
/// <reference types="vitest" />
import { defineConfig } from 'vitest/config'
import { defineWorkersProject } from '@cloudflare/vitest-pool-workers/config'
export default defineConfig({
export default defineWorkersProject({
test: {
globals: true,
poolOptions: {
workers: {
miniflare: {
compatibilityDate: '2024-01-01',
compatibilityFlags: ['nodejs_compat'],
},
},
},
},
})

908
yarn.lock

File diff suppressed because it is too large Load Diff