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

✨ Add sdk-utils waitForPercyIdle helper #831

Merged
merged 1 commit into from
Mar 17, 2022
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
2 changes: 2 additions & 0 deletions packages/sdk-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import logger from '@percy/logger';
import percy from './percy-info';
import request from './request';
import isPercyEnabled from './percy-enabled';
import waitForPercyIdle from './percy-idle';
import fetchPercyDOM from './percy-dom';
import postSnapshot from './post-snapshot';

Expand All @@ -13,6 +14,7 @@ export {
percy,
request,
isPercyEnabled,
waitForPercyIdle,
fetchPercyDOM,
postSnapshot
};
13 changes: 13 additions & 0 deletions packages/sdk-utils/src/percy-idle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import request from './request';

const RETRY_ERROR_CODES = ['ECONNRESET', 'ETIMEDOUT'];

export async function waitForPercyIdle() {
try {
return !!(await request('/percy/idle'));
} catch (e) {
return RETRY_ERROR_CODES.includes(e.code) && waitForPercyIdle();
}
}

export default waitForPercyIdle;
21 changes: 21 additions & 0 deletions packages/sdk-utils/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,27 @@ describe('SDK Utils', () => {
});
});

describe('waitForPercyIdle()', () => {
let { waitForPercyIdle } = utils;

it('gets idle state from the CLI API idle endpoint', async () => {
await expectAsync(waitForPercyIdle()).toBeResolvedTo(true);
await expectAsync(helpers.getRequests()).toBeResolvedTo([['/percy/idle']]);
});

it('polls the CLI API idle endpoint on timeout', async () => {
spyOn(utils.request, 'fetch').and.callFake((...args) => {
return utils.request.fetch.calls.count() > 2
? utils.request.fetch.and.originalFn(...args)
// eslint-disable-next-line prefer-promise-reject-errors
: Promise.reject({ code: 'ETIMEDOUT' });
});

await expectAsync(waitForPercyIdle()).toBeResolvedTo(true);
expect(utils.request.fetch).toHaveBeenCalledTimes(3);
});
});

describe('fetchPercyDOM()', () => {
let { fetchPercyDOM } = utils;

Expand Down
3 changes: 2 additions & 1 deletion packages/sdk-utils/test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ function context() {
{ success: true, config: { snapshot: { widths: [1280] } } })],
'/percy/config': ({ body }) => [200, 'application/json', (
{ success: true, config: body })],
'/percy/snapshot': () => [200, 'application/json', { success: true }]
'/percy/snapshot': () => [200, 'application/json', { success: true }],
'/percy/idle': () => [200, 'application/json', { success: true }]
}, 5338);

ctx.server.route((req, res, next) => {
Expand Down