/
transpilers.spec.ts
47 lines (41 loc) · 1.85 KB
/
transpilers.spec.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
// third-party transpiler and swc transpiler tests
// TODO: at the time of writing, other transpiler tests have not been moved into this file.
// Should consolidate them here.
import { context } from './testlib';
import { contextTsNodeUnderTest, testsDirRequire } from './helpers';
import * as expect from 'expect';
const test = context(contextTsNodeUnderTest);
test.suite('swc', (test) => {
test('verify that TS->SWC target mappings suppport all possible values from both TS and SWC', async (t) => {
const swcTranspiler = testsDirRequire(
'ts-node/transpilers/swc-experimental'
) as typeof import('../transpilers/swc');
// Detect when mapping is missing any ts.ScriptTargets
const ts = testsDirRequire('typescript') as typeof import('typescript');
for (const key of Object.keys(ts.ScriptTarget)) {
if (/^\d+$/.test(key)) continue;
if (key === 'JSON') continue;
expect(
swcTranspiler.targetMapping.has(ts.ScriptTarget[key as any] as any)
).toBe(true);
}
// Detect when mapping is missing any swc targets
// Assuming that tests/package.json declares @swc/core: latest
const swc = testsDirRequire('@swc/core');
let msg: string | undefined = undefined;
try {
swc.transformSync('', { jsc: { target: 'invalid' } });
} catch (e) {
msg = (e as Error).message;
}
expect(msg).toBeDefined();
// Error looks like:
// unknown variant `invalid`, expected one of `es3`, `es5`, `es2015`, `es2016`, `es2017`, `es2018`, `es2019`, `es2020`, `es2021` at line 1 column 28
const match = msg!.match(/unknown variant.*, expected one of (.*) at line/);
expect(match).toBeDefined();
const targets = match![1].split(', ').map((v: string) => v.slice(1, -1));
for (const target of targets) {
expect([...swcTranspiler.targetMapping.values()]).toContain(target);
}
});
});