Skip to content

Commit 7242c0e

Browse files
lbogdanCompuIves
authored andcommitted
Integration tests improvements. (codesandbox#2099)
1 parent 472dafe commit 7242c0e

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

packages/app/integration-tests/tests/sandboxes.test.js

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import puppeteer from 'puppeteer';
22

3+
const SECOND = 1000;
34
const SANDBOXES = [
45
'new',
56
// 'preact',
@@ -49,6 +50,52 @@ function pageLoaded(page) {
4950
});
5051
}
5152

53+
function loadSandbox(page, sandboxId, timeout) {
54+
return new Promise(async (resolve, reject) => {
55+
const timer = setTimeout(async () => {
56+
reject(
57+
Error(
58+
`Timeout: loading sandbox '${sandboxId}' took more than ${timeout /
59+
SECOND}s`
60+
)
61+
);
62+
}, timeout);
63+
page.goto(`http://localhost:3002/#${sandboxId}`, {
64+
timeout: 0, // we manage the timeout ourselves
65+
});
66+
await pageLoaded(page);
67+
clearTimeout(timer);
68+
await page.waitFor(2 * SECOND);
69+
resolve();
70+
});
71+
}
72+
73+
// eslint-disable-next-line consistent-return
74+
async function loadSandboxRetry(browser, sandboxId, timeout, retries) {
75+
let page;
76+
for (let i = 1; i <= retries; i++) {
77+
try {
78+
const start = new Date();
79+
/* eslint-disable no-await-in-loop */
80+
page = await browser.newPage();
81+
await loadSandbox(page, sandboxId, timeout);
82+
process.stdout.write(
83+
`Sandbox '${sandboxId}' loaded in ${(new Date() - start) / SECOND}s\n`
84+
);
85+
return page;
86+
} catch (err) {
87+
await page.waitFor(SECOND);
88+
await page.close();
89+
/* eslint-enable no-await-in-loop */
90+
if (i === retries) {
91+
throw new Error(`${err.message}, retried ${retries} times.`);
92+
} else {
93+
process.stdout.write(`Loading sandbox '${sandboxId}', retry ${i}...\n`);
94+
}
95+
}
96+
}
97+
}
98+
5299
describe('sandboxes', () => {
53100
let browser = puppeteer.launch({
54101
args: ['--no-sandbox', '--disable-setuid-sandbox'],
@@ -62,16 +109,11 @@ describe('sandboxes', () => {
62109
const threshold = sandbox.threshold || 0.01;
63110

64111
it(
65-
`loads the sandbox with id '${id}'`,
112+
`loads the sandbox '${id}'`,
66113
async () => {
67114
browser = await browser;
68-
const page = await browser.newPage();
69-
const waitFunction = pageLoaded(page);
70-
page.goto('http://localhost:3002/#' + id, {
71-
timeout: 80000,
72-
});
73-
await waitFunction;
74-
await page.waitFor(sandbox.waitFor || 2000);
115+
116+
const page = await loadSandboxRetry(browser, id, 30 * SECOND, 2);
75117

76118
const screenshot = await page.screenshot();
77119

@@ -84,7 +126,7 @@ describe('sandboxes', () => {
84126

85127
await page.close();
86128
},
87-
1000 * 120 * 1
129+
65 * SECOND
88130
);
89131
});
90132
});

0 commit comments

Comments
 (0)