-
Notifications
You must be signed in to change notification settings - Fork 382
/
fixtures.spec.ts
190 lines (162 loc) · 5.74 KB
/
fixtures.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
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
/*
* Copyright (c) 2020, 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 fs from 'fs';
import path from 'path';
import { rollup, RollupLog } from 'rollup';
import lwcRollupPlugin from '@lwc/rollup-plugin';
import { isVoidElement, HTML_NAMESPACE } from '@lwc/shared';
import { FeatureFlagName } from '@lwc/features/dist/types';
import { testFixtureDir } from '@lwc/jest-utils-lwc-internals';
import { serverSideRenderComponent } from '@lwc/ssr-runtime';
interface FixtureModule {
tagName: string;
default: any;
generateMarkup: any;
props?: { [key: string]: any };
features?: FeatureFlagName[];
}
jest.setTimeout(10_000 /* 10 seconds */);
async function compileFixture({ input, dirname }: { input: string; dirname: string }) {
const modulesDir = path.resolve(dirname, './modules');
const outputFile = path.resolve(dirname, './dist/compiled-experimental-ssr.js');
// TODO [#3331]: this is only needed to silence warnings on lwc:dynamic, remove in 246.
const warnings: RollupLog[] = [];
const bundle = await rollup({
input,
external: ['lwc'],
plugins: [
lwcRollupPlugin({
targetSSR: true,
enableDynamicComponents: true,
modules: [
{
dir: modulesDir,
},
],
}),
],
onwarn(warning) {
warnings.push(warning);
},
});
await bundle.write({
file: outputFile,
format: 'cjs',
exports: 'named',
});
return outputFile;
}
/**
* Naive HTML fragment formatter.
*
* This is a replacement for Prettier HTML formatting. Prettier formatting is too aggressive for
* fixture testing. It not only indent the HTML code but also fixes HTML issues. For testing we want
* to make sure that the fixture file is as close as possible to what the engine produces.
*/
function formatHTML(src: string): string {
let res = '';
let pos = 0;
let start = pos;
let depth = 0;
const getPadding = () => {
return ' '.repeat(depth);
};
while (pos < src.length) {
// Consume element tags and comments.
if (src.charAt(pos) === '<') {
const tagNameMatch = src.slice(pos).match(/(\w+)/);
if (!tagNameMatch) {
throw new Error(
`Expected to find tagname at pos ${pos} but found "${src.slice(pos, 20)}"`
);
}
// Special handling for `<style>` tags – these are not encoded, so we may hit '<' or '>'
// inside the text content. So we just serialize it as-is.
if (tagNameMatch[0] === 'style') {
const styleMatch = src.slice(pos).match(/<style([\s\S]*?)>([\s\S]*?)<\/style>/);
if (styleMatch) {
// opening tag
const [wholeMatch, attrs, textContent] = styleMatch;
res += getPadding() + `<style${attrs}>` + '\n';
depth++;
res += getPadding() + textContent + '\n';
depth--;
res += getPadding() + '</style>' + '\n';
start = pos = pos + wholeMatch.length;
continue;
}
}
const isVoid = isVoidElement(tagNameMatch[0], HTML_NAMESPACE);
const isClosing = src.charAt(pos + 1) === '/';
const isComment = src.slice(pos, pos + 3) === '!--';
start = pos;
while (src.charAt(pos++) !== '>') {
// Keep advancing until consuming the closing tag.
}
// Adjust current depth and print the element tag or comment.
if (isClosing) {
depth--;
}
res += getPadding() + src.slice(start, pos) + '\n';
const isSelfClosing = src.charAt(pos - 2) === '/';
if (!isClosing && !isSelfClosing && !isVoid && !isComment) {
depth++;
}
}
// Consume text content.
start = pos;
while (src.charAt(pos) !== '<' && pos < src.length) {
pos++;
}
if (start !== pos) {
res += getPadding() + src.slice(start, pos) + '\n';
}
}
return res.trim();
}
function testFixtures() {
testFixtureDir(
{
root: path.resolve(__dirname, '../../../engine-server/src/__tests__/fixtures'),
pattern: '**/index.js',
},
async ({ filename, dirname }) => {
const configPath = path.resolve(dirname, 'config.json');
let config: any = {};
if (fs.existsSync(configPath)) {
config = require(configPath);
}
const compiledFixturePath = await compileFixture({
input: filename,
dirname,
});
let module: FixtureModule;
jest.isolateModules(() => {
module = require(compiledFixturePath);
});
try {
const result = await serverSideRenderComponent(
module!.tagName,
module!.generateMarkup,
config.props || {}
);
return {
'expected.html': formatHTML(result),
'error.txt': undefined,
};
} catch (_err: any) {
return {
'error.txt': _err.message,
'expected.html': undefined,
};
}
}
);
}
describe('fixtures', () => {
testFixtures();
});