-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.ts
More file actions
169 lines (146 loc) · 4.9 KB
/
index.spec.ts
File metadata and controls
169 lines (146 loc) · 4.9 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
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
/*
* 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 babelClassPropertiesPlugin from '@babel/plugin-transform-class-properties';
import { LWC_VERSION, HIGHEST_API_VERSION } from '@lwc/shared';
import { testFixtureDir } from '@lwc/test-utils-lwc-internals';
import plugin, {
LwcPrivateMethodTransform,
LwcReversePrivateMethodTransform,
type LwcBabelPluginOptions,
} from '../index';
interface TestConfig extends LwcBabelPluginOptions {
experimentalErrorRecoveryMode?: boolean;
}
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 normalizeLwcError(lwcErrors: any) {
if (!lwcErrors || !Array.isArray(lwcErrors)) {
return;
}
return lwcErrors.map((err) => normalizeError(err));
}
function transform(source: string, opts = {}) {
const testConfig = {
...BASE_CONFIG,
parserOpts: (opts as any).parserOpts ?? {},
plugins: [[plugin, { ...BASE_OPTS, ...opts }]],
};
const result = transformSync(source, testConfig)!;
let { code } = result;
// 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,
lwcErrors: (result.metadata as any)?.lwcErrors,
};
}
describe('fixtures', () => {
testFixtureDir<TestConfig>(
{
root: path.resolve(import.meta.dirname, 'fixtures'),
pattern: '!(private-methods)/**/actual.js',
ssrVersion: 2,
},
({ src, config }) => {
let code;
let error;
let lwcErrors;
try {
const result = transform(src, config);
code = result.code;
lwcErrors = JSON.stringify(normalizeLwcError(result.lwcErrors), null, 4) + '\n';
} catch (err) {
error = JSON.stringify(normalizeError(err), null, 4);
}
// Conditionally including lwcErrors.json otherwise, toMatchFileSnapshot
// will create an empty snapshot file regardless of the the option being present for all tests
return {
'expected.js': code,
'error.json': error,
...((config as any)?.parserOpts?.errorRecovery
? { 'lwcErrors.json': lwcErrors }
: {}),
};
}
);
});
function transformWithPrivateMethodPipeline(source: string, opts = {}) {
const testConfig = {
...BASE_CONFIG,
parserOpts: (opts as any).parserOpts ?? {},
plugins: [
LwcPrivateMethodTransform,
[plugin, { ...BASE_OPTS, ...opts }],
[babelClassPropertiesPlugin, { loose: true }],
LwcReversePrivateMethodTransform,
],
};
const result = transformSync(source, testConfig)!;
let { code } = result;
code = code!.replace(new RegExp(LWC_VERSION.replace(/\./g, '\\.'), 'g'), 'X.X.X');
code = code.replace(
new RegExp(`apiVersion: ${HIGHEST_API_VERSION}`, 'g'),
`apiVersion: 9999999`
);
return { code };
}
describe('private-methods fixtures', () => {
testFixtureDir(
{
root: path.resolve(import.meta.dirname, 'fixtures', 'private-methods'),
pattern: '**/actual.js',
ssrVersion: 2,
},
({ src, config }) => {
let code;
let error;
try {
const result = transformWithPrivateMethodPipeline(src, config);
code = result.code;
} catch (err) {
error = JSON.stringify(normalizeError(err), null, 4);
}
return {
'expected.js': code,
'error.json': error,
};
}
);
});