Skip to content

Commit

Permalink
✨ Add backwards compatability for DOM library fetching (#134)
Browse files Browse the repository at this point in the history
This allows us to use the new CLI with any non-JS SDK right now. This is because
the only breaking changes for those non-js SDKs was:

- Change in server path for where to get the DOM library
- Change in function signature for how the dom library works

This commit adds back the `percy-agent.js` server endpoint (with a deprecation
log), and also adds a wrapper to match the old function signature.
  • Loading branch information
Robdel12 authored Jan 14, 2021
1 parent 2974bd7 commit ba345ab
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/core/src/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import http from 'http';
import log from '@percy/logger';
import pkg from '../package.json';

async function getReply({ version, routes }, request) {
Expand Down Expand Up @@ -104,6 +105,16 @@ export default function createPercyServer(percy) {
.readFile(require.resolve('@percy/dom'), 'utf-8')
.then(content => [200, 'applicaton/javascript', content]),

// serves the new DOM library, wrapped for compatability to `@percy/agent`
'/percy-agent.js': () => fs.promises
.readFile(require.resolve('@percy/dom'), 'utf-8')
.then(content => {
let wrapper = '(window.PercyAgent = class PercyAgent { snapshot(n, o) { return PercyDOM.serialize(o) } });';
log.warn('Warning: `percy-agent.js` is deprecated, please update to the latest SDK version');

return [200, 'applicaton/javascript', content.concat(wrapper)];
}),

// forward snapshot requests
'/percy/snapshot': ({ body }) => percy.snapshot(body)
.then(() => [200, 'application/json', { success: true }]),
Expand Down
17 changes: 17 additions & 0 deletions packages/core/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fetch from 'node-fetch';
import PercyConfig from '@percy/config';
import Percy from '../src';
import pkg from '../package.json';
import { stdio } from './helpers';

describe('Snapshot Server', () => {
let percy;
Expand All @@ -15,6 +16,7 @@ describe('Snapshot Server', () => {
});

afterEach(async () => {
percy.loglevel('error');
delete percy.stop; // remove own mocks
await percy.stop();
});
Expand Down Expand Up @@ -70,6 +72,21 @@ describe('Snapshot Server', () => {
await expect(response.text()).resolves.toBe(bundle);
});

it('serves the legacy percy-agent.js dom bundle', async () => {
let bundle = require('fs')
.readFileSync(require.resolve('@percy/dom'), { encoding: 'utf-8' })
.concat('(window.PercyAgent = class PercyAgent { snapshot(n, o) { return PercyDOM.serialize(o) } });');

await percy.start();
percy.loglevel('warn');
let response = await stdio.capture(() => fetch('http://localhost:1337/percy-agent.js'));

await expect(response.text()).resolves.toBe(bundle);
expect(stdio[1]).toEqual([
'[percy] Warning: `percy-agent.js` is deprecated, please update to the latest SDK version\n'
]);
});

it('has a /stop endpoint that calls #stop()', async () => {
await percy.start();
percy.stop = async () => (
Expand Down

0 comments on commit ba345ab

Please sign in to comment.