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": "*" "hono": "*"
}, },
"devDependencies": { "devDependencies": {
"@cloudflare/vitest-pool-workers": "^0.1.2",
"@types/react": "^18", "@types/react": "^18",
"@types/react-dom": "^18.2.17", "@types/react-dom": "^18.2.17",
"esbuild": "^0.20.2",
"hono": "^3.11.7", "hono": "^3.11.7",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0",
"tsup": "^8.0.1", "tsup": "^8.0.1",
"vitest": "^1.0.4" "vitest": "1.3.0"
}, },
"engines": { "engines": {
"node": ">=18" "node": ">=18"

View File

@ -1,4 +1,5 @@
import { Hono } from 'hono' import { Hono } from 'hono'
import { reactRenderer, useRequestContext } from '../src/react-renderer' import { reactRenderer, useRequestContext } from '../src/react-renderer'
const RequestUrl = () => { const RequestUrl = () => {
@ -107,5 +108,27 @@ describe('Basic', () => {
}) })
describe('Streaming', () => { 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": { "compilerOptions": {
"target": "ESNext", "target": "ESNext",
"module": "ESNext", "module": "ESNext",
"moduleResolution": "node", "moduleResolution": "Bundler",
"rootDir": "./", "rootDir": "./",
"outDir": "./dist", "outDir": "./dist",
"jsx": "react-jsx", "jsx": "react-jsx",
"jsxImportSource": "react" "jsxImportSource": "react"
}, },
"include": [ "include": [
"vitest.config.ts",
"src/**/*.ts", "src/**/*.ts",
"test/**/*.tsx", "test/**/*.tsx",
], ],

View File

@ -1,8 +1,15 @@
/// <reference types="vitest" /> import { defineWorkersProject } from '@cloudflare/vitest-pool-workers/config'
import { defineConfig } from 'vitest/config'
export default defineConfig({ export default defineWorkersProject({
test: { test: {
globals: true, 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