Skip to content

Commit

Permalink
test(typescript): move declaration tests, use typescript (#791)
Browse files Browse the repository at this point in the history
  • Loading branch information
shellscape authored Feb 5, 2021
1 parent 8b1691e commit a927ed8
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 158 deletions.
174 changes: 174 additions & 0 deletions packages/typescript/test/declarations.ts
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');
});
158 changes: 0 additions & 158 deletions packages/typescript/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,164 +36,6 @@ test.serial('runs code through typescript', async (t) => {
t.false(code.includes('const'), code);
});

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);

t.deepEqual(
output.map((out) => out.fileName),
['main.js', 'main.d.ts']
);

t.true(output[1].source.includes('declare const answer = 42;'), output[1].source);
});

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);

t.deepEqual(
output.map((out) => out.fileName),
['main.js', 'types/main.d.ts', 'types/main.d.ts.map']
);

const declarationSource = output[1].source;
t.true(declarationSource.includes('declare const answer = 42;'), declarationSource);
t.true(declarationSource.includes('//# sourceMappingURL=main.d.ts.map'), declarationSource);
});

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
);

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'
]
);

const declarationSource = output[1].source;
t.true(declarationSource.includes('export interface ITest'), declarationSource);
t.true(declarationSource.includes('//# sourceMappingURL=interface.d.ts.map'), declarationSource);
});

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);

t.deepEqual(
output.map((out) => out.fileName),
['main.js', 'types/main.d.ts']
);

t.true(output[1].source.includes('declare const answer = 42;'), output[1].source);
});

async function ensureOutDirWhenCreatingDeclarationFiles(t, compilerOptionName) {
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');
});

test.serial('ensures outDir is located in Rollup output dir', async (t) => {
const bundle = await rollup({
input: 'fixtures/basic/main.ts',
Expand Down

0 comments on commit a927ed8

Please sign in to comment.