Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ Fix flakey tests #184

Merged
merged 4 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions packages/cli-exec/test/start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ describe('percy exec:start', () => {
).resolves.toBeDefined();

process.emit('SIGTERM');
// wait for event to be handled
await new Promise(r => setTimeout(r, 500));

await expect(
fetch('http://localhost:5338/percy/healthcheck', { timeout: 10 })
).rejects.toThrow();
// check a few times rather than wait on a timeout to be deterministic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

connnveerrrggeeeeee

await expect(function check(i = 0) {
return fetch('http://localhost:5338/percy/healthcheck', { timeout: 10 })
.then(r => i >= 10 ? r : new Promise((res, rej) => {
setTimeout(() => check(i++).then(res, rej), 100);
}));
}()).rejects.toThrow();
});

it('logs an error when percy is already running', async () => {
Expand Down
30 changes: 28 additions & 2 deletions packages/core/test/capture.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,33 @@ describe('Percy Capture', () => {
]);
});

it('handles the page or browser closing early', async () => {
it('handles the browser closing early', async () => {
let created;

let page = percy.discoverer.page;
percy.discoverer.page = function() {
created = true;
return page.apply(this, arguments);
};

let capture = percy.capture({
name: 'test snapshot',
url: 'http://localhost:8000'
});

// wait until a page is requested
await waitFor(() => created);
percy.discoverer.close();
await capture;

expect(logger.stdout).toEqual([]);
expect(logger.stderr).toEqual([
expect.stringMatching('Encountered an error'),
expect.stringMatching('Protocol error \\(Target\\.createTarget\\): Browser closed')
]);
});

it('handles the page closing early', async () => {
let accessed = 0;

testDOM += '<link rel="stylesheet" href="/style.css"/>';
Expand Down Expand Up @@ -306,8 +332,8 @@ describe('Percy Capture', () => {
let accessed;

server.reply('/img.png', () => new Promise(resolve => {
setTimeout(() => (accessed = true), 100);
setTimeout(resolve, 500, [500, 'text/plain', 'Server Error']);
accessed = true;
}));

let capture = percy.capture({
Expand Down
22 changes: 14 additions & 8 deletions packages/dom/test/serialize-frames.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import serializeDOM from '@percy/dom';
describe('serializeFrames', () => {
let $;

const getFrame = id => when(() => {
let $frame = document.getElementById(id);
let accessible = !!$frame.contentDocument;
let loaded = accessible && $frame.contentWindow.performance.timing.loadEventEnd;
assert(!accessible || loaded, `#${id} did not load in time`, 5000);
return $frame;
});

beforeEach(async function() {
this.timeout(5000);
this.timeout(0); // frames may take a bit to load

withExample(`
<iframe id="frame-external" src="https://example.com"></iframe>
Expand All @@ -18,27 +26,25 @@ describe('serializeFrames', () => {
<iframe id="frame-js-no-src"></iframe>
`);

let $frameInput = document.getElementById('frame-input');
await when(() => assert($frameInput.contentWindow.performance.timing.loadEventEnd, '#frame-input did not load in time'));
let $frameInput = await getFrame('frame-input');
await I.type(() => $frameInput.contentDocument.querySelector('input'), 'iframe with an input');

let $frameJS = document.getElementById('frame-js-no-src');
let $frameJS = await getFrame('frame-js-no-src');
$frameJS.contentDocument.body.innerHTML = '<p>generated iframe</p>';

let $frameHead = document.createElement('iframe');
$frameHead.id = 'frame-head';
document.head.appendChild($frameHead);

// ensure external frame has loaded for coverage
let $frameExt = document.getElementById('frame-external');
await when(() => assert(!$frameExt.contentDocument, '#frame-external did not load in time'), 3000);

let $frameInject = document.createElement('iframe');
$frameInject.id = 'frame-inject';
$frameInject.src = 'javascript:false';
$frameInject.sandbox = '';
document.getElementById('test').appendChild($frameInject);

// ensure external frame has loaded for coverage
await getFrame('frame-external');

$ = parseDOM(serializeDOM());
});

Expand Down