Skip to content

Commit 9416dd2

Browse files
authored
ci: improve stability (#246)
* test: use inline script to mark initial nodes to improve test stability * test: collect individual log lines to improve test stability (on certain conditions one data event contains multiple lines) * fix: avoid extra empty lines in collected logs
1 parent 65b29c7 commit 9416dd2

File tree

5 files changed

+37
-21
lines changed

5 files changed

+37
-21
lines changed

packages/e2e-tests/e2e-server.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,26 @@ exports.serve = async function serve(root, isBuild, port) {
4646
server: null,
4747
build: null
4848
};
49+
50+
const pushLines = (str, arr) => {
51+
const lines = str.split(/\r?\n/);
52+
if (lines[lines.length - 1] === '') {
53+
lines.pop(); // last element empty means str ended with \n, remove it or we end up with extra \n when joining again
54+
}
55+
Array.prototype.push.apply(arr, lines);
56+
};
57+
const collectLogs = (proc, { out, err }) => {
58+
proc.stdout.on('data', (d) => pushLines(d.toString(), out));
59+
proc.stderr.on('data', (d) => pushLines(d.toString(), err));
60+
};
61+
4962
const writeLogs = async (name, result) => {
5063
try {
5164
if (result.out && result.out.length > 0) {
52-
fs.writeFileSync(path.join(logDir, `${name}.log`), result.out.join(''), 'utf-8');
65+
fs.writeFileSync(path.join(logDir, `${name}.log`), result.out.join('\n'), 'utf-8');
5366
}
5467
if (result.err && result.err.length > 0) {
55-
fs.writeFileSync(path.join(logDir, `${name}.err.log`), result.err.join(''), 'utf-8');
68+
fs.writeFileSync(path.join(logDir, `${name}.err.log`), result.err.join('\n'), 'utf-8');
5669
}
5770
} catch (e1) {
5871
console.error(`failed to write ${name} logs in ${logDir}`, e1);
@@ -72,16 +85,15 @@ exports.serve = async function serve(root, isBuild, port) {
7285
stdio: 'pipe'
7386
});
7487
logs.build = { out, err };
75-
buildProcess.stdout.on('data', (d) => out.push(d.toString()));
76-
buildProcess.stderr.on('data', (d) => err.push(d.toString()));
88+
collectLogs(buildProcess, logs.build);
7789
await buildProcess;
7890
} catch (e) {
7991
buildResult = e;
8092
if (buildResult.stdout) {
81-
out.push(buildResult.stdout);
93+
pushLines(buildResult.stdout, out);
8294
}
8395
if (buildResult.stderr) {
84-
err.push(buildResult.stderr);
96+
pushLines(buildResult.stderr, err);
8597
}
8698
hasErr = true;
8799
}
@@ -99,8 +111,7 @@ exports.serve = async function serve(root, isBuild, port) {
99111
const out = [],
100112
err = [];
101113
logs.server = { out, err };
102-
serverProcess.stdout.on('data', (d) => out.push(d.toString()));
103-
serverProcess.stderr.on('data', (d) => err.push(d.toString()));
114+
collectLogs(serverProcess, logs.server);
104115

105116
const closeServer = async () => {
106117
if (serverProcess) {
@@ -121,10 +132,10 @@ exports.serve = async function serve(root, isBuild, port) {
121132
await serverProcess;
122133
} catch (e) {
123134
if (e.stdout) {
124-
out.push(e.stdout);
135+
pushLines(e.stdout, out);
125136
}
126137
if (e.stderr) {
127-
err.push(e.stderr);
138+
pushLines(e.stderr, err);
128139
}
129140
if (!!process.env.DEBUG && !isWin) {
130141
// treekill on windows uses taskkill and that ends up here always

packages/e2e-tests/kit-node/__tests__/kit.spec.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,15 @@ import path from 'path';
1717
describe('kit-node', () => {
1818
describe('index route', () => {
1919
it('should hydrate', async () => {
20-
// mark initial nodes
21-
await page.$eval('#load', (e) => {
22-
e['__initialNode'] = true;
23-
});
24-
await page.$eval('#mount', (e) => {
25-
e['__initialNode'] = true;
26-
});
27-
2820
// check content before hydration
2921
expect(await getText('h1')).toBe('Hello world!');
3022
expect(await getText('#load')).toBe('SERVER_LOADED');
3123
expect(await getText('#mount')).toBe('BEFORE_MOUNT');
3224
expect(await getText('#i18n')).toBe('WELCOME');
3325
expect(await getText('#env')).toBe('FOOBARENV');
26+
// check that inline script added the initial node markers
27+
expect(await page.$eval('#load', (e) => e['__initialNode'])).toBe(true);
28+
expect(await page.$eval('#mount', (e) => e['__initialNode'])).toBe(true);
3429

3530
// also get page as text to confirm
3631
const html = await (await fetch(page.url())).text();

packages/e2e-tests/kit-node/src/app.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,15 @@
88
</head>
99
<body>
1010
<div id="svelte">%svelte.body%</div>
11+
<script>
12+
// used for testing hydration
13+
function markInitialNode(id) {
14+
const el = document.getElementById(id);
15+
if (el) {
16+
el.__initialNode = true;
17+
}
18+
}
19+
['load', 'mount'].forEach(markInitialNode);
20+
</script>
1121
</body>
1222
</html>

packages/e2e-tests/vite-ssr-esm/__tests__/vite-ssr-esm.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ test('css', async () => {
3232
test('loaded esm only package', async () => {
3333
expect(await page.textContent('#esm')).toMatch('esm');
3434
expect(browserLogs).toContain('esm');
35-
expect(e2eServer.logs.server.out).toContain('esm\n');
35+
expect(e2eServer.logs.server.out).toContain('esm');
3636
});
3737

3838
test('loaded external node esm only package', () => {
39-
expect(e2eServer.logs.server.out).toContain('hello_world\n');
39+
expect(e2eServer.logs.server.out).toContain('hello_world');
4040
});
4141

4242
test('asset', async () => {

packages/e2e-tests/vite-ssr/__tests__/vite-ssr.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ test('css', async () => {
3232
test('loaded esm only package', async () => {
3333
expect(await page.textContent('#esm')).toMatch('esm');
3434
expect(browserLogs).toContain('esm');
35-
expect(e2eServer.logs.server.out).toContain('esm\n');
35+
expect(e2eServer.logs.server.out).toContain('esm');
3636
});
3737

3838
test('asset', async () => {

0 commit comments

Comments
 (0)