-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.ts
More file actions
86 lines (75 loc) · 2.41 KB
/
index.spec.ts
File metadata and controls
86 lines (75 loc) · 2.41 KB
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
/*
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import path from 'node:path';
import { describe } from 'vitest';
import { transformSync } from '@babel/core';
import { LWC_VERSION, HIGHEST_API_VERSION } from '@lwc/shared';
import { testFixtureDir } from '@lwc/test-utils-lwc-internals';
import plugin, { type LwcBabelPluginOptions } from '../index';
const BASE_OPTS = {
namespace: 'lwc',
name: 'test',
};
const BASE_CONFIG = {
babelrc: false,
configFile: false,
filename: `${BASE_OPTS.name}.js`,
// Force Babel to generate new line and white spaces. This prevent Babel from generating
// an error when the generated code is over 500KB.
compact: false,
};
function normalizeError(err: any) {
if (err.code === 'BABEL_TRANSFORM_ERROR') {
return {
// Filter out the stacktrace, just include the error message
message: err.message.match(/^.*?\.js: ([^\n]+)/)[1],
loc: err.loc,
filename: err.filename ? path.basename(err.filename) : undefined,
};
} else {
return {
name: err.name,
message: err.message,
};
}
}
function transform(source: string, opts = {}) {
const testConfig = {
...BASE_CONFIG,
plugins: [[plugin, { ...BASE_OPTS, ...opts }]],
};
let { code } = transformSync(source, testConfig)!;
// Replace LWC's version with X.X.X so the snapshots don't frequently change
code = code!.replace(new RegExp(LWC_VERSION.replace(/\./g, '\\.'), 'g'), 'X.X.X');
// Replace the latest API version as well
code = code.replace(
new RegExp(`apiVersion: ${HIGHEST_API_VERSION}`, 'g'),
`apiVersion: 9999999`
);
return code;
}
describe('fixtures', () => {
testFixtureDir<LwcBabelPluginOptions>(
{
root: path.resolve(__dirname, 'fixtures'),
pattern: '**/actual.js',
},
({ src, config }) => {
let result;
let error;
try {
result = transform(src, config);
} catch (err) {
error = JSON.stringify(normalizeError(err), null, 4);
}
return {
'expected.js': result,
'error.json': error,
};
}
);
});