|
| 1 | +import { afterEach, describe, expect, test } from 'bun:test' |
| 2 | +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import { join, resolve } from 'node:path' |
| 5 | +import { nativeEngineAvailable } from '../src/native-engine' |
| 6 | + |
| 7 | +const CLI_PATH = resolve(import.meta.dir, '../bin/cli.ts') |
| 8 | +const projects: string[] = [] |
| 9 | + |
| 10 | +/** Runs `dtsx <args>` in a fresh project holding `src/index.ts`, and returns the declaration. */ |
| 11 | +function generate(source: string, args: string[]): string { |
| 12 | + const dir = mkdtempSync(join(tmpdir(), 'dtsx-cli-defaults-')) |
| 13 | + projects.push(dir) |
| 14 | + mkdirSync(join(dir, 'src')) |
| 15 | + writeFileSync(join(dir, 'src', 'index.ts'), source) |
| 16 | + const run = Bun.spawnSync([process.execPath, CLI_PATH, ...args], { cwd: dir }) |
| 17 | + expect(run.exitCode).toBe(0) |
| 18 | + return readFileSync(join(dir, 'dist', 'index.d.ts'), 'utf8') |
| 19 | +} |
| 20 | + |
| 21 | +afterEach(() => { |
| 22 | + for (const dir of projects.splice(0)) |
| 23 | + rmSync(dir, { recursive: true, force: true }) |
| 24 | +}) |
| 25 | + |
| 26 | +describe('dtsx generate defaults', () => { |
| 27 | + // The argument parser leaves an untouched list option as its raw default |
| 28 | + // string, so `dtsx generate` alone once passed importOrder 'bun' and skipped |
| 29 | + // the native engine, which only implements the import order ['bun']. |
| 30 | + test.skipIf(!nativeEngineAvailable())('with no flags it emits with the native engine', () => { |
| 31 | + const awaited = 'export const value = await Promise.resolve(1)\n' |
| 32 | + expect(generate(awaited, ['generate'])).toContain('export declare const value: number;') |
| 33 | + expect(generate(awaited, ['generate', '--stats'])).toContain('export declare const value: number;') |
| 34 | + }) |
| 35 | + |
| 36 | + test('--import-order takes comma-separated patterns', () => { |
| 37 | + const source = [ |
| 38 | + `import type { BunFile } from 'bun'`, |
| 39 | + `import type { Stats } from 'node:fs'`, |
| 40 | + 'export function read(file: BunFile, stats: Stats): void {}', |
| 41 | + '', |
| 42 | + ].join('\n') |
| 43 | + const output = generate(source, ['generate', '--import-order', 'node:,bun']) |
| 44 | + expect(output.indexOf(`from 'node:fs'`)).toBeGreaterThanOrEqual(0) |
| 45 | + expect(output.indexOf(`from 'node:fs'`)).toBeLessThan(output.indexOf(`from 'bun'`)) |
| 46 | + }) |
| 47 | +}) |
0 commit comments