diff --git a/packages/core/src/server.js b/packages/core/src/server.js index b0229236d..0da867521 100644 --- a/packages/core/src/server.js +++ b/packages/core/src/server.js @@ -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) { @@ -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 }]), diff --git a/packages/core/test/server.test.js b/packages/core/test/server.test.js index 938153c71..18b22ec9f 100644 --- a/packages/core/test/server.test.js +++ b/packages/core/test/server.test.js @@ -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; @@ -15,6 +16,7 @@ describe('Snapshot Server', () => { }); afterEach(async () => { + percy.loglevel('error'); delete percy.stop; // remove own mocks await percy.stop(); }); @@ -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 () => (