Skip to content

Commit

Permalink
ci: improve stability (#246)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dominikg committed Jan 4, 2022
1 parent 65b29c7 commit 9416dd2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
31 changes: 21 additions & 10 deletions packages/e2e-tests/e2e-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,26 @@ exports.serve = async function serve(root, isBuild, port) {
server: null,
build: null
};

const pushLines = (str, arr) => {
const lines = str.split(/\r?\n/);
if (lines[lines.length - 1] === '') {
lines.pop(); // last element empty means str ended with \n, remove it or we end up with extra \n when joining again
}
Array.prototype.push.apply(arr, lines);
};
const collectLogs = (proc, { out, err }) => {
proc.stdout.on('data', (d) => pushLines(d.toString(), out));
proc.stderr.on('data', (d) => pushLines(d.toString(), err));
};

const writeLogs = async (name, result) => {
try {
if (result.out && result.out.length > 0) {
fs.writeFileSync(path.join(logDir, `${name}.log`), result.out.join(''), 'utf-8');
fs.writeFileSync(path.join(logDir, `${name}.log`), result.out.join('\n'), 'utf-8');
}
if (result.err && result.err.length > 0) {
fs.writeFileSync(path.join(logDir, `${name}.err.log`), result.err.join(''), 'utf-8');
fs.writeFileSync(path.join(logDir, `${name}.err.log`), result.err.join('\n'), 'utf-8');
}
} catch (e1) {
console.error(`failed to write ${name} logs in ${logDir}`, e1);
Expand All @@ -72,16 +85,15 @@ exports.serve = async function serve(root, isBuild, port) {
stdio: 'pipe'
});
logs.build = { out, err };
buildProcess.stdout.on('data', (d) => out.push(d.toString()));
buildProcess.stderr.on('data', (d) => err.push(d.toString()));
collectLogs(buildProcess, logs.build);
await buildProcess;
} catch (e) {
buildResult = e;
if (buildResult.stdout) {
out.push(buildResult.stdout);
pushLines(buildResult.stdout, out);
}
if (buildResult.stderr) {
err.push(buildResult.stderr);
pushLines(buildResult.stderr, err);
}
hasErr = true;
}
Expand All @@ -99,8 +111,7 @@ exports.serve = async function serve(root, isBuild, port) {
const out = [],
err = [];
logs.server = { out, err };
serverProcess.stdout.on('data', (d) => out.push(d.toString()));
serverProcess.stderr.on('data', (d) => err.push(d.toString()));
collectLogs(serverProcess, logs.server);

const closeServer = async () => {
if (serverProcess) {
Expand All @@ -121,10 +132,10 @@ exports.serve = async function serve(root, isBuild, port) {
await serverProcess;
} catch (e) {
if (e.stdout) {
out.push(e.stdout);
pushLines(e.stdout, out);
}
if (e.stderr) {
err.push(e.stderr);
pushLines(e.stderr, err);
}
if (!!process.env.DEBUG && !isWin) {
// treekill on windows uses taskkill and that ends up here always
Expand Down
11 changes: 3 additions & 8 deletions packages/e2e-tests/kit-node/__tests__/kit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,15 @@ import path from 'path';
describe('kit-node', () => {
describe('index route', () => {
it('should hydrate', async () => {
// mark initial nodes
await page.$eval('#load', (e) => {
e['__initialNode'] = true;
});
await page.$eval('#mount', (e) => {
e['__initialNode'] = true;
});

// check content before hydration
expect(await getText('h1')).toBe('Hello world!');
expect(await getText('#load')).toBe('SERVER_LOADED');
expect(await getText('#mount')).toBe('BEFORE_MOUNT');
expect(await getText('#i18n')).toBe('WELCOME');
expect(await getText('#env')).toBe('FOOBARENV');
// check that inline script added the initial node markers
expect(await page.$eval('#load', (e) => e['__initialNode'])).toBe(true);
expect(await page.$eval('#mount', (e) => e['__initialNode'])).toBe(true);

// also get page as text to confirm
const html = await (await fetch(page.url())).text();
Expand Down
10 changes: 10 additions & 0 deletions packages/e2e-tests/kit-node/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,15 @@
</head>
<body>
<div id="svelte">%svelte.body%</div>
<script>
// used for testing hydration
function markInitialNode(id) {
const el = document.getElementById(id);
if (el) {
el.__initialNode = true;
}
}
['load', 'mount'].forEach(markInitialNode);
</script>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ test('css', async () => {
test('loaded esm only package', async () => {
expect(await page.textContent('#esm')).toMatch('esm');
expect(browserLogs).toContain('esm');
expect(e2eServer.logs.server.out).toContain('esm\n');
expect(e2eServer.logs.server.out).toContain('esm');
});

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

test('asset', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e-tests/vite-ssr/__tests__/vite-ssr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test('css', async () => {
test('loaded esm only package', async () => {
expect(await page.textContent('#esm')).toMatch('esm');
expect(browserLogs).toContain('esm');
expect(e2eServer.logs.server.out).toContain('esm\n');
expect(e2eServer.logs.server.out).toContain('esm');
});

test('asset', async () => {
Expand Down

0 comments on commit 9416dd2

Please sign in to comment.