Skip to content
Myr

declaration-emit.test.ts

This file

Path

build/declaration-emit.test.ts

38 lines · 1.7 KiB · TypeScript

Last changed by

test(build): type-check the bundled declarations from a consumer project (#5486)Yusuke Wadabefore adoptionc437d75

History of this file

Take it

test(build): type-check the bundled declarations from a consumer project (#5486)Yusuke Wadabefore adoptionc437d75

lines 1–38why, one step back
1import { spawnSync } from 'node:child_process'
2import { cpSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'3import { createRequire } from 'node:module'4import { tmpdir } from 'node:os'5import { join } from 'node:path'6import { fileURLToPath } from 'node:url'7import { describe, expect, it } from 'vitest'89const compiler = createRequire(import.meta.url).resolve('typescript/bin/tsc')10const fixture = fileURLToPath(new URL('./fixtures/declaration-emit', import.meta.url))11const packageRoot = fileURLToPath(new URL('..', import.meta.url))1213describe('published declarations', () => {14 it('let consumers emit declarations for types inferred from hono', () => {15 // The fixture must live outside this package: inside it, tsc can always reach16 // `dist/types/*` with a relative path and never reports TS2883.17 const project = mkdtempSync(join(tmpdir(), 'hono-declaration-emit-'))18 try {19 cpSync(fixture, project, { recursive: true })20 mkdirSync(join(project, 'node_modules'))21 symlinkSync(packageRoot, join(project, 'node_modules', 'hono'), 'dir')22 writeFileSync(join(project, 'package.json'), '{ "type": "module" }\n')2324 const result = spawnSync(25 process.execPath,26 [compiler, '--project', project, '--outDir', join(project, 'out')],27 { encoding: 'utf8' }28 )29 // Without `export {}` in the bundled `.d.ts`, tsc fails with TS2883 here:30 // internal aliases such as the return type of `c.json()` look exported from31 // `hono/dist/types/...`, which is not reachable through the package exports.32 expect(result.stdout + result.stderr).toBe('')33 expect(result.status).toBe(0)34 } finally {35 rmSync(project, { recursive: true, force: true })36 }37 })38})