test(build): type-check the bundled declarations from a consumer project (#5486)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Run tsc against dist/types the way a consumer does, so regressions in the bundled declarations are caught in CI: JSX attribute checking for both runtimes (#5480) and declaration emit for inferred hono types (#5484).
authored by Yusuke Wada · committed by GitHub · parent be1f749 · Browse files · 6 files·+122 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Jeremy Banka <hello@jeremybanka.com>
Diff stats
6 files+122 added−0 removed
- tests:+58 added−0 removed
- other:+64 added−0 removed
Provenance
- Commit
c437d75- Tree
2617293- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
be1f749
6 files changed+122 added−0 removed
added:build/fixtures/declaration-emit/index.ts+10 added
12345678910import { Hono } from 'hono'import { createFactory } from 'hono/factory'
// Inferred types like these mention internal aliases from hono's declarations// (for example the return type of `c.json()`). Consumers that build with// `declaration: true` must be able to emit them without naming those aliases.export const handlers = createFactory().createHandlers((c) => c.json({ ok: true }))
export const app = new Hono().get('/', (c) => c.json({ ok: true }))export type AppType = typeof appadded:build/fixtures/declaration-emit/tsconfig.json+14 added
1234567891011121314{ "compilerOptions": { "target": "ESNext", "module": "NodeNext", "moduleResolution": "NodeNext", "strict": true, "declaration": true, "emitDeclarationOnly": true, "skipLibCheck": false, "preserveSymlinks": true, "types": [] }, "include": ["index.ts"]}added:build/fixtures/jsx-types/index.tsx+26 added
1234567891011121314151617181920212223242526import type { JSX } from 'hono/jsx'
declare module 'hono/jsx' { // eslint-disable-next-line @typescript-eslint/no-namespace namespace JSX { interface IntrinsicElements { 'custom-element': JSX.HTMLAttributes } }}
export const button = ( <button disabled={false} class='button'> Hello </button>)export const customElement = <custom-element class='custom' />
// @ts-expect-error Native boolean attributes must reject strings.export const invalidButton = <button disabled='yes' />
// @ts-expect-error Native class attributes must reject numbers.export const invalidClass = <button class={123} />
// @ts-expect-error Augmented custom elements must retain their attribute types.export const invalidCustomElement = <custom-element class={123} />added:build/fixtures/jsx-types/tsconfig.json+14 added
1234567891011121314{ "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "hono/jsx", "lib": ["DOM", "ES2024"], "module": "NodeNext", "moduleResolution": "NodeNext", "noEmit": true, "skipLibCheck": false, "strict": true, "types": [] }, "include": ["index.tsx"]}added:build/declaration-emit.test.ts+38 added
1234567891011121314151617181920212223242526272829303132333435363738import { spawnSync } from 'node:child_process'import { cpSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'import { createRequire } from 'node:module'import { tmpdir } from 'node:os'import { join } from 'node:path'import { fileURLToPath } from 'node:url'import { describe, expect, it } from 'vitest'
const compiler = createRequire(import.meta.url).resolve('typescript/bin/tsc')const fixture = fileURLToPath(new URL('./fixtures/declaration-emit', import.meta.url))const packageRoot = fileURLToPath(new URL('..', import.meta.url))
describe('published declarations', () => { it('let consumers emit declarations for types inferred from hono', () => { // The fixture must live outside this package: inside it, tsc can always reach // `dist/types/*` with a relative path and never reports TS2883. const project = mkdtempSync(join(tmpdir(), 'hono-declaration-emit-')) try { cpSync(fixture, project, { recursive: true }) mkdirSync(join(project, 'node_modules')) symlinkSync(packageRoot, join(project, 'node_modules', 'hono'), 'dir') writeFileSync(join(project, 'package.json'), '{ "type": "module" }\n')
const result = spawnSync( process.execPath, [compiler, '--project', project, '--outDir', join(project, 'out')], { encoding: 'utf8' } ) // Without `export {}` in the bundled `.d.ts`, tsc fails with TS2883 here: // internal aliases such as the return type of `c.json()` look exported from // `hono/dist/types/...`, which is not reachable through the package exports. expect(result.stdout + result.stderr).toBe('') expect(result.status).toBe(0) } finally { rmSync(project, { recursive: true, force: true }) } })})added:build/jsx-types.test.ts+20 added
1234567891011121314151617181920import { spawnSync } from 'node:child_process'import { createRequire } from 'node:module'import { fileURLToPath } from 'node:url'import { describe, expect, it } from 'vitest'
const compiler = createRequire(import.meta.url).resolve('typescript/bin/tsc')const project = fileURLToPath(new URL('./fixtures/jsx-types/tsconfig.json', import.meta.url))
describe.each(['hono/jsx', 'hono/jsx/dom'])('published %s declarations', (jsxImportSource) => { it.each(['react-jsx', 'react-jsxdev'])('preserves JSX attribute checking with %s', (jsx) => { // Resolve the package exports to dist/types, as a consumer would after a build. const result = spawnSync( process.execPath, [compiler, '--project', project, '--jsxImportSource', jsxImportSource, '--jsx', jsx], { encoding: 'utf8' } ) expect(result.stdout + result.stderr).toBe('') expect(result.status).toBe(0) })})