|  | 
|  | 1 | +import test, { ExecutionContext } from 'ava'; | 
|  | 2 | +import { rollup } from 'rollup'; | 
|  | 3 | + | 
|  | 4 | +import typescript from '..'; | 
|  | 5 | + | 
|  | 6 | +import { getCode } from '../../../util/test'; | 
|  | 7 | + | 
|  | 8 | +test.beforeEach(() => process.chdir(__dirname)); | 
|  | 9 | + | 
|  | 10 | +// eslint-disable-next-line no-console | 
|  | 11 | +const onwarn = (warning: any) => console.warn(warning.toString()); | 
|  | 12 | + | 
|  | 13 | +test.serial('supports creating declaration files', async (t) => { | 
|  | 14 | +  const bundle = await rollup({ | 
|  | 15 | +    input: 'fixtures/basic/main.ts', | 
|  | 16 | +    plugins: [ | 
|  | 17 | +      typescript({ | 
|  | 18 | +        tsconfig: 'fixtures/basic/tsconfig.json', | 
|  | 19 | +        outDir: 'fixtures/basic/dist', | 
|  | 20 | +        declaration: true | 
|  | 21 | +      }) | 
|  | 22 | +    ], | 
|  | 23 | +    onwarn | 
|  | 24 | +  }); | 
|  | 25 | +  const output = await getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true); | 
|  | 26 | +  const declaration = output[1].source as string; | 
|  | 27 | + | 
|  | 28 | +  t.deepEqual( | 
|  | 29 | +    output.map((out) => out.fileName), | 
|  | 30 | +    ['main.js', 'main.d.ts'] | 
|  | 31 | +  ); | 
|  | 32 | + | 
|  | 33 | +  t.true(declaration.includes('declare const answer = 42;'), declaration); | 
|  | 34 | +}); | 
|  | 35 | + | 
|  | 36 | +test.serial('supports creating declaration files in subfolder', async (t) => { | 
|  | 37 | +  const bundle = await rollup({ | 
|  | 38 | +    input: 'fixtures/basic/main.ts', | 
|  | 39 | +    plugins: [ | 
|  | 40 | +      typescript({ | 
|  | 41 | +        tsconfig: 'fixtures/basic/tsconfig.json', | 
|  | 42 | +        outDir: 'fixtures/basic/dist/types', | 
|  | 43 | +        declaration: true, | 
|  | 44 | +        declarationMap: true | 
|  | 45 | +      }) | 
|  | 46 | +    ], | 
|  | 47 | +    onwarn | 
|  | 48 | +  }); | 
|  | 49 | +  const output = await getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true); | 
|  | 50 | +  const declaration = output[1].source as string; | 
|  | 51 | + | 
|  | 52 | +  t.deepEqual( | 
|  | 53 | +    output.map((out) => out.fileName), | 
|  | 54 | +    ['main.js', 'types/main.d.ts', 'types/main.d.ts.map'] | 
|  | 55 | +  ); | 
|  | 56 | + | 
|  | 57 | +  t.true(declaration.includes('declare const answer = 42;'), declaration); | 
|  | 58 | +  t.true(declaration.includes('//# sourceMappingURL=main.d.ts.map'), declaration); | 
|  | 59 | +}); | 
|  | 60 | + | 
|  | 61 | +test.serial('supports creating declarations with non-default rootDir', async (t) => { | 
|  | 62 | +  const bundle = await rollup({ | 
|  | 63 | +    input: 'fixtures/declaration-root-dir/src/main.ts', | 
|  | 64 | +    plugins: [ | 
|  | 65 | +      typescript({ | 
|  | 66 | +        tsconfig: 'fixtures/declaration-root-dir/tsconfig.json' | 
|  | 67 | +      }) | 
|  | 68 | +    ], | 
|  | 69 | +    onwarn | 
|  | 70 | +  }); | 
|  | 71 | +  const output = await getCode( | 
|  | 72 | +    bundle, | 
|  | 73 | +    { format: 'esm', dir: 'fixtures/declaration-root-dir/lib' }, | 
|  | 74 | +    true | 
|  | 75 | +  ); | 
|  | 76 | + | 
|  | 77 | +  t.deepEqual( | 
|  | 78 | +    output.map((out) => out.fileName), | 
|  | 79 | +    ['main.js', 'main.d.ts'] | 
|  | 80 | +  ); | 
|  | 81 | +}); | 
|  | 82 | + | 
|  | 83 | +test.serial('supports creating declaration files for interface only source file', async (t) => { | 
|  | 84 | +  const bundle = await rollup({ | 
|  | 85 | +    input: 'fixtures/export-interface-only/main.ts', | 
|  | 86 | +    plugins: [ | 
|  | 87 | +      typescript({ | 
|  | 88 | +        tsconfig: 'fixtures/export-interface-only/tsconfig.json', | 
|  | 89 | +        declarationDir: 'fixtures/export-interface-only/dist/types', | 
|  | 90 | +        declaration: true, | 
|  | 91 | +        declarationMap: true | 
|  | 92 | +      }) | 
|  | 93 | +    ], | 
|  | 94 | +    onwarn | 
|  | 95 | +  }); | 
|  | 96 | + | 
|  | 97 | +  const output = await getCode( | 
|  | 98 | +    bundle, | 
|  | 99 | +    { format: 'esm', dir: 'fixtures/export-interface-only/dist' }, | 
|  | 100 | +    true | 
|  | 101 | +  ); | 
|  | 102 | +  const declaration = output[1].source as string; | 
|  | 103 | + | 
|  | 104 | +  t.deepEqual( | 
|  | 105 | +    output.map((out) => out.fileName), | 
|  | 106 | +    [ | 
|  | 107 | +      'main.js', | 
|  | 108 | +      'types/interface.d.ts', | 
|  | 109 | +      'types/interface.d.ts.map', | 
|  | 110 | +      'types/main.d.ts', | 
|  | 111 | +      'types/main.d.ts.map' | 
|  | 112 | +    ] | 
|  | 113 | +  ); | 
|  | 114 | + | 
|  | 115 | +  t.true(declaration.includes('export interface ITest'), declaration); | 
|  | 116 | +  t.true(declaration.includes('//# sourceMappingURL=interface.d.ts.map'), declaration); | 
|  | 117 | +}); | 
|  | 118 | + | 
|  | 119 | +test.serial('supports creating declaration files in declarationDir', async (t) => { | 
|  | 120 | +  const bundle = await rollup({ | 
|  | 121 | +    input: 'fixtures/basic/main.ts', | 
|  | 122 | +    plugins: [ | 
|  | 123 | +      typescript({ | 
|  | 124 | +        tsconfig: 'fixtures/basic/tsconfig.json', | 
|  | 125 | +        declarationDir: 'fixtures/basic/dist/types', | 
|  | 126 | +        declaration: true | 
|  | 127 | +      }) | 
|  | 128 | +    ], | 
|  | 129 | +    onwarn | 
|  | 130 | +  }); | 
|  | 131 | +  const output = await getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true); | 
|  | 132 | +  const declaration = output[1].source as string; | 
|  | 133 | + | 
|  | 134 | +  t.deepEqual( | 
|  | 135 | +    output.map((out) => out.fileName), | 
|  | 136 | +    ['main.js', 'types/main.d.ts'] | 
|  | 137 | +  ); | 
|  | 138 | + | 
|  | 139 | +  t.true(declaration.includes('declare const answer = 42;'), declaration); | 
|  | 140 | +}); | 
|  | 141 | + | 
|  | 142 | +async function ensureOutDirWhenCreatingDeclarationFiles( | 
|  | 143 | +  t: ExecutionContext, | 
|  | 144 | +  compilerOptionName: string | 
|  | 145 | +) { | 
|  | 146 | +  const bundle = await rollup({ | 
|  | 147 | +    input: 'fixtures/basic/main.ts', | 
|  | 148 | +    plugins: [ | 
|  | 149 | +      typescript({ | 
|  | 150 | +        tsconfig: 'fixtures/basic/tsconfig.json', | 
|  | 151 | +        [compilerOptionName]: true | 
|  | 152 | +      }) | 
|  | 153 | +    ], | 
|  | 154 | +    onwarn | 
|  | 155 | +  }); | 
|  | 156 | +  const caughtError = await t.throwsAsync(() => | 
|  | 157 | +    getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true) | 
|  | 158 | +  ); | 
|  | 159 | + | 
|  | 160 | +  t.true( | 
|  | 161 | +    caughtError.message.includes( | 
|  | 162 | +      `'outDir' or 'declarationDir' must be specified to generate declaration files` | 
|  | 163 | +    ), | 
|  | 164 | +    `Unexpected error message: ${caughtError.message}` | 
|  | 165 | +  ); | 
|  | 166 | +} | 
|  | 167 | + | 
|  | 168 | +test.serial('ensures outDir is set when creating declaration files (declaration)', async (t) => { | 
|  | 169 | +  await ensureOutDirWhenCreatingDeclarationFiles(t, 'declaration'); | 
|  | 170 | +}); | 
|  | 171 | + | 
|  | 172 | +test.serial('ensures outDir is set when creating declaration files (composite)', async (t) => { | 
|  | 173 | +  await ensureOutDirWhenCreatingDeclarationFiles(t, 'composite'); | 
|  | 174 | +}); | 
0 commit comments