Skip to content

Commit a927ed8

Browse files
authored
test(typescript): move declaration tests, use typescript (#791)
1 parent 8b1691e commit a927ed8

File tree

2 files changed

+174
-158
lines changed

2 files changed

+174
-158
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
});

packages/typescript/test/test.js

Lines changed: 0 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -36,164 +36,6 @@ test.serial('runs code through typescript', async (t) => {
3636
t.false(code.includes('const'), code);
3737
});
3838

39-
test.serial('supports creating declaration files', async (t) => {
40-
const bundle = await rollup({
41-
input: 'fixtures/basic/main.ts',
42-
plugins: [
43-
typescript({
44-
tsconfig: 'fixtures/basic/tsconfig.json',
45-
outDir: 'fixtures/basic/dist',
46-
declaration: true
47-
})
48-
],
49-
onwarn
50-
});
51-
const output = await getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true);
52-
53-
t.deepEqual(
54-
output.map((out) => out.fileName),
55-
['main.js', 'main.d.ts']
56-
);
57-
58-
t.true(output[1].source.includes('declare const answer = 42;'), output[1].source);
59-
});
60-
61-
test.serial('supports creating declaration files in subfolder', async (t) => {
62-
const bundle = await rollup({
63-
input: 'fixtures/basic/main.ts',
64-
plugins: [
65-
typescript({
66-
tsconfig: 'fixtures/basic/tsconfig.json',
67-
outDir: 'fixtures/basic/dist/types',
68-
declaration: true,
69-
declarationMap: true
70-
})
71-
],
72-
onwarn
73-
});
74-
const output = await getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true);
75-
76-
t.deepEqual(
77-
output.map((out) => out.fileName),
78-
['main.js', 'types/main.d.ts', 'types/main.d.ts.map']
79-
);
80-
81-
const declarationSource = output[1].source;
82-
t.true(declarationSource.includes('declare const answer = 42;'), declarationSource);
83-
t.true(declarationSource.includes('//# sourceMappingURL=main.d.ts.map'), declarationSource);
84-
});
85-
86-
test.serial('supports creating declarations with non-default rootDir', async (t) => {
87-
const bundle = await rollup({
88-
input: 'fixtures/declaration-root-dir/src/main.ts',
89-
plugins: [
90-
typescript({
91-
tsconfig: 'fixtures/declaration-root-dir/tsconfig.json'
92-
})
93-
],
94-
onwarn
95-
});
96-
const output = await getCode(
97-
bundle,
98-
{ format: 'esm', dir: 'fixtures/declaration-root-dir/lib' },
99-
true
100-
);
101-
102-
t.deepEqual(
103-
output.map((out) => out.fileName),
104-
['main.js', 'main.d.ts']
105-
);
106-
});
107-
108-
test.serial('supports creating declaration files for interface only source file', async (t) => {
109-
const bundle = await rollup({
110-
input: 'fixtures/export-interface-only/main.ts',
111-
plugins: [
112-
typescript({
113-
tsconfig: 'fixtures/export-interface-only/tsconfig.json',
114-
declarationDir: 'fixtures/export-interface-only/dist/types',
115-
declaration: true,
116-
declarationMap: true
117-
})
118-
],
119-
onwarn
120-
});
121-
122-
const output = await getCode(
123-
bundle,
124-
{ format: 'esm', dir: 'fixtures/export-interface-only/dist' },
125-
true
126-
);
127-
128-
t.deepEqual(
129-
output.map((out) => out.fileName),
130-
[
131-
'main.js',
132-
'types/interface.d.ts',
133-
'types/interface.d.ts.map',
134-
'types/main.d.ts',
135-
'types/main.d.ts.map'
136-
]
137-
);
138-
139-
const declarationSource = output[1].source;
140-
t.true(declarationSource.includes('export interface ITest'), declarationSource);
141-
t.true(declarationSource.includes('//# sourceMappingURL=interface.d.ts.map'), declarationSource);
142-
});
143-
144-
test.serial('supports creating declaration files in declarationDir', async (t) => {
145-
const bundle = await rollup({
146-
input: 'fixtures/basic/main.ts',
147-
plugins: [
148-
typescript({
149-
tsconfig: 'fixtures/basic/tsconfig.json',
150-
declarationDir: 'fixtures/basic/dist/types',
151-
declaration: true
152-
})
153-
],
154-
onwarn
155-
});
156-
const output = await getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true);
157-
158-
t.deepEqual(
159-
output.map((out) => out.fileName),
160-
['main.js', 'types/main.d.ts']
161-
);
162-
163-
t.true(output[1].source.includes('declare const answer = 42;'), output[1].source);
164-
});
165-
166-
async function ensureOutDirWhenCreatingDeclarationFiles(t, compilerOptionName) {
167-
const bundle = await rollup({
168-
input: 'fixtures/basic/main.ts',
169-
plugins: [
170-
typescript({
171-
tsconfig: 'fixtures/basic/tsconfig.json',
172-
[compilerOptionName]: true
173-
})
174-
],
175-
onwarn
176-
});
177-
const caughtError = await t.throwsAsync(() =>
178-
getCode(bundle, { format: 'esm', dir: 'fixtures/basic/dist' }, true)
179-
);
180-
181-
t.true(
182-
caughtError.message.includes(
183-
`'outDir' or 'declarationDir' must be specified to generate declaration files`
184-
),
185-
`Unexpected error message: ${caughtError.message}`
186-
);
187-
}
188-
189-
test.serial('ensures outDir is set when creating declaration files (declaration)', async (t) => {
190-
await ensureOutDirWhenCreatingDeclarationFiles(t, 'declaration');
191-
});
192-
193-
test.serial('ensures outDir is set when creating declaration files (composite)', async (t) => {
194-
await ensureOutDirWhenCreatingDeclarationFiles(t, 'composite');
195-
});
196-
19739
test.serial('ensures outDir is located in Rollup output dir', async (t) => {
19840
const bundle = await rollup({
19941
input: 'fixtures/basic/main.ts',

0 commit comments

Comments
 (0)