Skip to content

Commit

Permalink
creating helper method to send failed event from js sdk's
Browse files Browse the repository at this point in the history
  • Loading branch information
prklm10 committed Oct 20, 2023
1 parent 15b3d06 commit 78ec02e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export function createPercyServer(percy, port) {
// Recieves events from sdk's.
.route('post', '/percy/events', async (req, res) => {
percyFailedEventHandler(req, pkg.version);
await percy.client.sendFailedEvents(percy.build.id, req.body);
await percy.client.sendFailedEvents(percy.build?.id, req.body);
res.json(200, { success: true });
})
// stops percy at the end of the current event loop
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import EventEmitter from 'events';
import { sha256hash, getPackageJSON } from '@percy/client/utils';
import { sha256hash } from '@percy/client/utils';
import { camelcase, merge } from '@percy/config/utils';

export {
Expand Down Expand Up @@ -72,9 +72,9 @@ export function percyFailedEventHandler(req, cliVersion) {
// Remove the original clientInfo field
delete req.body.clientInfo;
}
req.body.cliVersion = cliVersion;
let pkg = getPackageJSON(import.meta.url);
req.body.cliVersion = pkg.version;
if (!req.body.cliVersion) {
req.body.cliVersion = cliVersion;
}
}

// Creates a local resource object containing the resource URL, mimetype, content, sha, and any
Expand Down
7 changes: 3 additions & 4 deletions packages/core/test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,23 +344,22 @@ describe('API Server', () => {
});

it('has a /events endpoint that calls #sendFailedEvents() async with provided options with clientInfo absent', async () => {
let { getPackageJSON } = await import('@percy/client/utils');
let pkg = getPackageJSON(import.meta.url);
let resolve, test = new Promise(r => (resolve = r));
let sendFailedEventsSpy = spyOn(percy.client, 'sendFailedEvents').and.resolveTo('some response');

await percy.start();

await expectAsync(request('/percy/events', {
body: {
errorMessage: 'some error'
errorMessage: 'some error',
cliVersion: '1.2.3'
},
method: 'post'
})).toBeResolvedTo({ success: true });

expect(sendFailedEventsSpy).toHaveBeenCalledOnceWith(percy.build.id, jasmine.objectContaining({
errorMessage: 'some error',
cliVersion: pkg.version
cliVersion: '1.2.3'
}));

await expectAsync(test).toBePending();
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import waitForPercyIdle from './percy-idle.js';
import fetchPercyDOM from './percy-dom.js';
import postSnapshot from './post-snapshot.js';
import postComparison from './post-comparison.js';
import postFailedEvent from './post-failed-event.js';
import flushSnapshots from './flush-snapshots.js';
import captureAutomateScreenshot from './post-screenshot.js';

Expand All @@ -19,7 +20,8 @@ export {
postSnapshot,
postComparison,
flushSnapshots,
captureAutomateScreenshot
captureAutomateScreenshot,
postFailedEvent
};

// export the namespace by default
Expand Down
10 changes: 10 additions & 0 deletions packages/sdk-utils/src/post-failed-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import request from './request.js';

// Post failed event data to the CLI event endpoint.
export async function postFailedEvent(options) {
return await request.post('/percy/events', options).catch(err => {
throw err;
});
}

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

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

beforeEach(() => {
options = {
errorMessage: 'someError',
errorKind: 'sdk',
cliVersion: '1.2.3'
};
});

it('posts comparison options to the CLI API event endpoint', async () => {
spyOn(utils.request, 'post').and.callFake(() => Promise.resolve());
await expectAsync(postFailedEvent(options)).toBeResolved();
await expectAsync(helpers.get('requests')).toBeResolvedTo({});
});

it('throws when the event API fails', async () => {
await helpers.test('error', '/percy/events');

await expectAsync(postFailedEvent({}))
.toBeRejectedWithError('testing');
});
});

describe('flushSnapshots([options])', () => {
let { flushSnapshots } = utils;

Expand Down

0 comments on commit 78ec02e

Please sign in to comment.