-
-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(typescript): move declaration tests, use typescript (#791)
- Loading branch information
1 parent
8b1691e
commit a927ed8
Showing
2 changed files
with
174 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import test, { ExecutionContext } from 'ava'; | ||
import { rollup } from 'rollup'; | ||
|
||
import typescript from '..'; | ||
|
||
import { getCode } from '../../../util/test'; | ||
|
||
test.beforeEach(() => process.chdir(__dirname)); | ||
|
||
// eslint-disable-next-line no-console | ||
const onwarn = (warning: any) => console.warn(warning.toString()); | ||
|
||
test.serial('supports creating declaration files', async (t) => { | ||
const bundle = await rollup({ | ||
input: 'fixtures/basic/main.ts', | ||
plugins: [ | ||
typescript({ | ||
tsconfig: 'fixtures/basic/tsconfig.json', | ||
outDir: 'fixtures/basic/dist', | ||
declaration: true | ||
}) | ||
], | ||
onwarn | ||
}); | ||
const output = await getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true); | ||
const declaration = output[1].source as string; | ||
|
||
t.deepEqual( | ||
output.map((out) => out.fileName), | ||
['main.js', 'main.d.ts'] | ||
); | ||
|
||
t.true(declaration.includes('declare const answer = 42;'), declaration); | ||
}); | ||
|
||
test.serial('supports creating declaration files in subfolder', async (t) => { | ||
const bundle = await rollup({ | ||
input: 'fixtures/basic/main.ts', | ||
plugins: [ | ||
typescript({ | ||
tsconfig: 'fixtures/basic/tsconfig.json', | ||
outDir: 'fixtures/basic/dist/types', | ||
declaration: true, | ||
declarationMap: true | ||
}) | ||
], | ||
onwarn | ||
}); | ||
const output = await getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true); | ||
const declaration = output[1].source as string; | ||
|
||
t.deepEqual( | ||
output.map((out) => out.fileName), | ||
['main.js', 'types/main.d.ts', 'types/main.d.ts.map'] | ||
); | ||
|
||
t.true(declaration.includes('declare const answer = 42;'), declaration); | ||
t.true(declaration.includes('//# sourceMappingURL=main.d.ts.map'), declaration); | ||
}); | ||
|
||
test.serial('supports creating declarations with non-default rootDir', async (t) => { | ||
const bundle = await rollup({ | ||
input: 'fixtures/declaration-root-dir/src/main.ts', | ||
plugins: [ | ||
typescript({ | ||
tsconfig: 'fixtures/declaration-root-dir/tsconfig.json' | ||
}) | ||
], | ||
onwarn | ||
}); | ||
const output = await getCode( | ||
bundle, | ||
{ format: 'esm', dir: 'fixtures/declaration-root-dir/lib' }, | ||
true | ||
); | ||
|
||
t.deepEqual( | ||
output.map((out) => out.fileName), | ||
['main.js', 'main.d.ts'] | ||
); | ||
}); | ||
|
||
test.serial('supports creating declaration files for interface only source file', async (t) => { | ||
const bundle = await rollup({ | ||
input: 'fixtures/export-interface-only/main.ts', | ||
plugins: [ | ||
typescript({ | ||
tsconfig: 'fixtures/export-interface-only/tsconfig.json', | ||
declarationDir: 'fixtures/export-interface-only/dist/types', | ||
declaration: true, | ||
declarationMap: true | ||
}) | ||
], | ||
onwarn | ||
}); | ||
|
||
const output = await getCode( | ||
bundle, | ||
{ format: 'esm', dir: 'fixtures/export-interface-only/dist' }, | ||
true | ||
); | ||
const declaration = output[1].source as string; | ||
|
||
t.deepEqual( | ||
output.map((out) => out.fileName), | ||
[ | ||
'main.js', | ||
'types/interface.d.ts', | ||
'types/interface.d.ts.map', | ||
'types/main.d.ts', | ||
'types/main.d.ts.map' | ||
] | ||
); | ||
|
||
t.true(declaration.includes('export interface ITest'), declaration); | ||
t.true(declaration.includes('//# sourceMappingURL=interface.d.ts.map'), declaration); | ||
}); | ||
|
||
test.serial('supports creating declaration files in declarationDir', async (t) => { | ||
const bundle = await rollup({ | ||
input: 'fixtures/basic/main.ts', | ||
plugins: [ | ||
typescript({ | ||
tsconfig: 'fixtures/basic/tsconfig.json', | ||
declarationDir: 'fixtures/basic/dist/types', | ||
declaration: true | ||
}) | ||
], | ||
onwarn | ||
}); | ||
const output = await getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true); | ||
const declaration = output[1].source as string; | ||
|
||
t.deepEqual( | ||
output.map((out) => out.fileName), | ||
['main.js', 'types/main.d.ts'] | ||
); | ||
|
||
t.true(declaration.includes('declare const answer = 42;'), declaration); | ||
}); | ||
|
||
async function ensureOutDirWhenCreatingDeclarationFiles( | ||
t: ExecutionContext, | ||
compilerOptionName: string | ||
) { | ||
const bundle = await rollup({ | ||
input: 'fixtures/basic/main.ts', | ||
plugins: [ | ||
typescript({ | ||
tsconfig: 'fixtures/basic/tsconfig.json', | ||
[compilerOptionName]: true | ||
}) | ||
], | ||
onwarn | ||
}); | ||
const caughtError = await t.throwsAsync(() => | ||
getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true) | ||
); | ||
|
||
t.true( | ||
caughtError.message.includes( | ||
`'outDir' or 'declarationDir' must be specified to generate declaration files` | ||
), | ||
`Unexpected error message: ${caughtError.message}` | ||
); | ||
} | ||
|
||
test.serial('ensures outDir is set when creating declaration files (declaration)', async (t) => { | ||
await ensureOutDirWhenCreatingDeclarationFiles(t, 'declaration'); | ||
}); | ||
|
||
test.serial('ensures outDir is set when creating declaration files (composite)', async (t) => { | ||
await ensureOutDirWhenCreatingDeclarationFiles(t, 'composite'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters