-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
swc.test.ts
99 lines (90 loc) · 2.81 KB
/
swc.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
import * as fs from 'fs';
import * as path from 'path';
import * as swc from '@swc/core';
import {
extractExportedConstValue,
getConfig,
UnsupportedValueError,
} from '../src/swc';
import {
NO_SUCH_DECLARATION_CASES,
TEST_CASES,
UNSUPPORTED_VALUE_CASES,
} from './fixtures/extract-const-value';
import { BaseFunctionConfigSchema } from '../src';
function parse(source: string): swc.Module {
return swc.parseSync(source, { syntax: 'typescript' });
}
function parseFixture(filename: string): swc.Module {
const sourcePath = path.join(__dirname, filename);
const input = fs.readFileSync(sourcePath, { encoding: 'utf-8' });
return parse(input);
}
describe('extractExportedConstValue for swc', () => {
describe('parses successfully', () => {
test.each(TEST_CASES)('$input', ({ input, identifier, expected }) => {
const ast = parse(input);
const value = extractExportedConstValue(ast, identifier);
expect(value).toStrictEqual(expected);
});
});
describe('fails with UnsupportedValueError', () => {
test.each(UNSUPPORTED_VALUE_CASES)('$input', ({ input, identifier }) => {
const ast = parse(input);
expect(() => {
extractExportedConstValue(ast, identifier);
}).toThrow(UnsupportedValueError);
});
});
describe('returns null if the declaration is not found', () => {
test.each(NO_SUCH_DECLARATION_CASES)('$input', ({ input, identifier }) => {
const ast = parse(input);
const value = extractExportedConstValue(ast, identifier);
expect(value).toBe(null);
});
});
});
describe('getConfig for swc', () => {
it('should parse config from Node.js file', () => {
const ast = parseFixture('fixtures/node.js');
const config = getConfig(ast, BaseFunctionConfigSchema);
expect(config).toMatchInlineSnapshot(`
{
"maxDuration": 60,
"memory": 1024,
"runtime": "nodejs",
}
`);
});
it('should parse config from Deno file', () => {
const ast = parseFixture('fixtures/deno.ts');
const config = getConfig(ast, {
type: 'object',
properties: {
location: { type: 'string' },
},
} as const);
expect(config).toMatchInlineSnapshot(`
{
"location": "https://example.com/page",
"maxDuration": 60,
"runtime": "deno",
}
`);
});
it('should return `null` when no config was exported', () => {
const ast = parseFixture('fixtures/no-config.js');
const config = getConfig(ast, BaseFunctionConfigSchema);
expect(config).toBeNull();
});
it('should throw an error upon schema validation failure', () => {
const ast = parseFixture('fixtures/invalid-schema.js');
let err;
try {
getConfig(ast, BaseFunctionConfigSchema);
} catch (_err) {
err = _err;
}
expect(err.message).toEqual('Invalid data');
});
});