|
1 | 1 | import type { DtsGenerationConfig } from '../src/types' |
2 | 2 | import { afterEach, describe, expect, it } from 'bun:test' |
3 | | -import { mkdir, rm, writeFile } from 'node:fs/promises' |
| 3 | +import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' |
| 4 | +import { tmpdir } from 'node:os' |
4 | 5 | import { join } from 'node:path' |
5 | 6 | import { generate, processSource } from '../src/generator' |
6 | 7 | import { extractDeclarations } from '../src/extractor' |
7 | 8 | import { processCode } from './test-utils' |
8 | 9 |
|
| 10 | +const tempDirs: string[] = [] |
| 11 | + |
| 12 | +async function createTempDir(): Promise<string> { |
| 13 | + const tempDir = await mkdtemp(join(tmpdir(), 'dtsx-generator-')) |
| 14 | + tempDirs.push(tempDir) |
| 15 | + return tempDir |
| 16 | +} |
| 17 | + |
| 18 | +afterEach(async () => { |
| 19 | + await Promise.all(tempDirs.splice(0).map(dir => rm(dir, { recursive: true, force: true }))) |
| 20 | +}) |
| 21 | + |
9 | 22 | describe('processSource (stdin support)', () => { |
10 | 23 | it('should process simple variable declaration', () => { |
11 | 24 | const source = `export const foo: string = 'bar';` |
@@ -97,6 +110,40 @@ describe('processSource (stdin support)', () => { |
97 | 110 | }) |
98 | 111 | }) |
99 | 112 |
|
| 113 | +describe('generate relative declaration specifiers', () => { |
| 114 | + it('rewrites relative imports and exports from the emitted declaration path', async () => { |
| 115 | + const tempDir = await createTempDir() |
| 116 | + const srcDir = join(tempDir, 'src') |
| 117 | + const outDir = join(tempDir, 'dist') |
| 118 | + |
| 119 | + await mkdir(join(srcDir, 'nested'), { recursive: true }) |
| 120 | + await writeFile(join(tempDir, 'index.ts'), [ |
| 121 | + `export * from './src/request'`, |
| 122 | + `export { Thing } from './src/nested/thing'`, |
| 123 | + `export interface Wrapper { request: import('./src/request').RequestInstance }`, |
| 124 | + ].join('\n')) |
| 125 | + await writeFile(join(srcDir, 'request.ts'), `export interface RequestInstance { id: string }\n`) |
| 126 | + await writeFile(join(srcDir, 'nested', 'thing.ts'), `export interface Thing { name: string }\n`) |
| 127 | + |
| 128 | + await generate({ |
| 129 | + cwd: tempDir, |
| 130 | + root: '.', |
| 131 | + outdir: outDir, |
| 132 | + entrypoints: ['index.ts'], |
| 133 | + outputStructure: 'mirror', |
| 134 | + clean: false, |
| 135 | + keepComments: true, |
| 136 | + logLevel: 'error', |
| 137 | + }) |
| 138 | + |
| 139 | + const output = await readFile(join(outDir, 'index.d.ts'), 'utf8') |
| 140 | + |
| 141 | + expect(output).toContain(`export * from './src/request';`) |
| 142 | + expect(output).toContain(`export { Thing } from './src/nested/thing';`) |
| 143 | + expect(output).toContain(`import('./src/request').RequestInstance`) |
| 144 | + }) |
| 145 | +}) |
| 146 | + |
100 | 147 | describe('generate with parallel processing', () => { |
101 | 148 | const tempDir = join(__dirname, 'temp-parallel-test') |
102 | 149 | const inputDir = join(tempDir, 'input') |
|
0 commit comments