forked from sveltejs/svelte-preprocess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypescript.test.ts
262 lines (205 loc) · 7.81 KB
/
typescript.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { resolve } from 'path';
import { compile } from 'svelte/compiler';
import type { Diagnostic } from 'typescript';
import sveltePreprocess from '../../src';
import { loadTsconfig } from '../../src/transformers/typescript';
import type { Processed } from '../../src/types';
import {
preprocess,
getFixtureContent,
spyConsole,
getTestAppFilename,
} from '../utils';
spyConsole({ silent: true });
const EXPECTED_SCRIPT = getFixtureContent('script.js');
const autoProcessTS = (content: string, compilerOptions?: any) => {
const opts = sveltePreprocess({
typescript: {
tsconfigFile: false,
compilerOptions: {
...compilerOptions,
skipLibCheck: true,
},
},
});
return opts.script?.({
content,
markup: `<script lang="ts">${content}</script>`,
attributes: { lang: 'ts' },
filename: resolve(__dirname, '..', 'App.svelte'),
}) as Processed & { diagnostics: Diagnostic[] };
};
describe('transformer - typescript', () => {
const template = `<div></div><script lang="ts">${getFixtureContent(
'script.ts',
)}</script>`;
it('should disallow transpilation to es5 or lower', async () => {
await expect(
autoProcessTS('export let foo = 10', { target: 'es3' }),
).rejects.toThrow();
await expect(
autoProcessTS('export let foo = 10', { target: 'es5' }),
).rejects.toThrow();
});
it('should throw if errors are found', async () => {
await expect(autoProcessTS('export l et 0')).rejects.toThrow();
});
describe('configuration file', () => {
it('should work with no compilerOptions', async () => {
const opts = sveltePreprocess({ typescript: { tsconfigFile: false } });
const preprocessed = await preprocess(template, opts);
expect(preprocessed.toString?.()).toContain('export var hello');
});
it('should work with tsconfigDirectory', async () => {
const opts = sveltePreprocess({
typescript: {
tsconfigFile: false,
tsconfigDirectory: './test/fixtures',
},
});
const preprocessed = await preprocess(template, opts);
expect(preprocessed.toString?.()).toContain(EXPECTED_SCRIPT);
});
it('should work with tsconfigFile', async () => {
const opts = sveltePreprocess({
typescript: {
tsconfigFile: './test/fixtures/tsconfig.json',
},
});
const preprocessed = await preprocess(template, opts);
expect(preprocessed.toString?.()).toContain(EXPECTED_SCRIPT);
});
it('should report config semantic errors in tsconfig file', () => {
const opts = sveltePreprocess({
typescript: {
tsconfigFile: './test/fixtures/tsconfig.semantic.json',
},
});
return expect(preprocess(template, opts)).rejects.toThrow('TS6046');
});
it('should transpile ts to js', async () => {
const opts = sveltePreprocess({
typescript: {
compilerOptions: {
module: 'es6',
sourceMap: false,
},
},
});
const { code } = await preprocess(template, opts);
expect(code).toContain(getFixtureContent('script.js'));
});
it('should strip unused and type imports', async () => {
const tpl = getFixtureContent('TypeScriptImports.svelte');
const opts = sveltePreprocess({
typescript: { tsconfigFile: false },
});
const { code } = await preprocess(tpl, opts);
// Test that imports are properly preserved
expect(code).toContain(`import { fly } from "svelte/transition"`);
expect(code).toContain(`import { flip } from "svelte/animate"`);
expect(code).toContain(`import Nested from "./Nested.svelte"`);
expect(code).toContain(`import { hello } from "./script"`);
expect(code).toContain(`import { AValue } from "./types"`);
expect(code).toContain(
`import { storeTemplateOnly, storeScriptOnly } from "./store"`,
);
expect(code).toContain(
`import { onlyUsedInModuleScript } from "./modulescript";`,
);
expect(code).toContain(
`import { storeModuleTemplateOnly, storeModuleScriptOnly } from "./store";`,
);
// Test that comments are properly preserved
expect(code).toContain('<!-- Some comment -->');
});
it('should keep all value imports with preserveValueImports', async () => {
const tpl = getFixtureContent('PreserveValueImports.svelte');
const opts = sveltePreprocess({
typescript: {
tsconfigFile: false,
compilerOptions: { preserveValueImports: true },
},
});
const { code } = await preprocess(tpl, opts);
expect(code).toContain("import { Bar } from './somewhere'");
expect(code).toContain("import Component from './component.svelte'");
expect(code).toContain("import { Value } from './value'");
expect(code).not.toContain("'./type'");
});
it('should deal with empty transpilation result', async () => {
const tpl = getFixtureContent('TypeScriptTypesOnly.svelte');
const opts = sveltePreprocess({
typescript: { tsconfigFile: false },
sourceMap: true,
});
const { code } = await preprocess(tpl, opts);
expect(code).toBe(`<script lang="ts" context="module"></script>`);
});
it('should strip unused and type imports in context="module" tags', async () => {
const tpl = getFixtureContent('TypeScriptImportsModule.svelte');
const opts = sveltePreprocess({
typescript: { tsconfigFile: false },
});
const { code } = await preprocess(tpl, opts);
expect(code).toContain(`import { AValue } from "./types";`);
});
it('should produce sourcemap', async () => {
const tpl = getFixtureContent('TypeScriptImportsModule.svelte');
const opts = sveltePreprocess({
typescript: { tsconfigFile: false },
sourceMap: true,
});
const { map } = await preprocess(tpl, opts);
expect(map).toHaveProperty('sources', ['App.svelte']);
});
it('should work when tsconfig contains outDir', async () => {
const opts = sveltePreprocess({
typescript: {
tsconfigFile: './test/fixtures/tsconfig.outdir.json',
handleMixedImports: true,
},
sourceMap: true,
});
const preprocessed = await preprocess(template, opts);
expect(preprocessed.toString?.()).toContain(EXPECTED_SCRIPT);
});
it('supports extends field', () => {
const { options } = loadTsconfig({}, getTestAppFilename(), {
tsconfigFile: './test/fixtures/tsconfig.extends1.json',
});
expect(options).toMatchObject({
module: 5,
skipLibCheck: false,
esModuleInterop: true,
});
});
it('should not override the target setting if one is present', async () => {
const tpl = getFixtureContent('TypeScriptES2021.svelte');
const opts = sveltePreprocess({
typescript: {
tsconfigFile: './test/fixtures/tsconfig.es2021target.json',
},
});
const { code } = await preprocess(tpl, opts);
expect(code).toContain('await ');
expect(code).toContain('async function doIt');
expect(code).toContain('&&=');
expect(code).toContain('||=');
// Svelte should be able to compile ES2021.
const { warnings } = compile(code, { name: 'Test' });
expect(warnings).toHaveLength(0);
});
it('should target ES6 if the configuration has no target setting', async () => {
const tpl = getFixtureContent('TypeScriptES2021.svelte');
const opts = sveltePreprocess({
typescript: { tsconfigFile: false },
});
const { code } = await preprocess(tpl, opts);
expect(code).not.toContain('await ');
expect(code).not.toContain('async function doIt');
expect(code).not.toContain('&&=');
expect(code).not.toContain('||=');
});
});
});