From bc6674f5acd3fde504468b0ac4d7f2e6b4cd2d3a Mon Sep 17 00:00:00 2001 From: Ninad Sheth Date: Fri, 6 Oct 2023 11:18:36 +0530 Subject: [PATCH] Added token support in percy config --- packages/client/src/client.js | 9 +++++++-- packages/client/test/client.test.js | 23 +++++++++++++++++++---- packages/core/src/config.js | 3 +++ packages/core/src/percy.js | 6 +++--- packages/core/test/percy.test.js | 4 ++++ 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/packages/client/src/client.js b/packages/client/src/client.js index c91c20be1..08740f8c7 100644 --- a/packages/client/src/client.js +++ b/packages/client/src/client.js @@ -49,10 +49,11 @@ export class PercyClient { // initial user agent info clientInfo, environmentInfo, + config, // versioned api url apiUrl = PERCY_CLIENT_API_URL } = {}) { - Object.assign(this, { token, apiUrl }); + Object.assign(this, { token, config: config || {}, apiUrl }); this.addClientInfo(clientInfo); this.addEnvironmentInfo(environmentInfo); } @@ -84,8 +85,12 @@ export class PercyClient { } // Checks for a Percy token and returns it. + // Priority order is + // 1. passed token to constructor + // 2. PERCY_TOKEN env var [ from env package ] + // 3. token from percy config getToken(raiseIfMissing = true) { - let token = this.token || this.env.token; + let token = this.token || this.env.token || this.config.percy?.token; if (!token && raiseIfMissing) throw new Error('Missing Percy token'); return token; } diff --git a/packages/client/test/client.test.js b/packages/client/test/client.test.js index 86b95d4d3..30276a502 100644 --- a/packages/client/test/client.test.js +++ b/packages/client/test/client.test.js @@ -40,10 +40,6 @@ describe('PercyClient', () => { }); it('it logs a debug warning when no info is passed', async () => { - client = new PercyClient({ - token: 'PERCY_TOKEN' - }); - await expectAsync(client.createSnapshot(123, { name: 'snapfoo', widths: [1000], @@ -1338,6 +1334,10 @@ describe('PercyClient', () => { }); describe('#getToken', () => { + afterEach(() => { + delete process.env.PERCY_TOKEN; + }) + it('should throw error when called with true', () => { const client = new PercyClient({}); expect(() => { @@ -1351,5 +1351,20 @@ describe('PercyClient', () => { }); expect(client.getToken(false)).toBe('PERCY_TOKEN'); }); + + it('should read from env package if token is not passed', () => { + process.env.PERCY_TOKEN = 'PERCY_TOKEN' + const client = new PercyClient({ + config: { percy: { token: 'DONT_USE_THIS' }} + }); + expect(client.getToken()).toBe('PERCY_TOKEN'); + }); + + it('should read from config if env is not set and config has percy.token', () => { + const client = new PercyClient({ + config: { percy: { token: 'USE_THIS_TOKEN' }} + }); + expect(client.getToken()).toBe('USE_THIS_TOKEN'); + }); }); }); diff --git a/packages/core/src/config.js b/packages/core/src/config.js index 05ac86e95..ed4929911 100644 --- a/packages/core/src/config.js +++ b/packages/core/src/config.js @@ -6,6 +6,9 @@ export const configSchema = { properties: { deferUploads: { type: 'boolean' + }, + token: { + type: 'string' } } }, diff --git a/packages/core/src/percy.js b/packages/core/src/percy.js index 2a9489aca..b4f9e32a1 100644 --- a/packages/core/src/percy.js +++ b/packages/core/src/percy.js @@ -68,12 +68,12 @@ export class Percy { // options which will become accessible via the `.config` property ...options } = {}) { - let { percy, ...config } = PercyConfig.load({ + let config = PercyConfig.load({ overrides: options, path: configFile }); - deferUploads ??= percy?.deferUploads; + deferUploads ??= config.percy?.deferUploads; this.config = config; if (testing) loglevel = 'silent'; @@ -87,7 +87,7 @@ export class Percy { this.delayUploads = this.skipUploads || !!delayUploads; this.deferUploads = this.skipUploads || !!deferUploads; - this.client = new PercyClient({ token, clientInfo, environmentInfo }); + this.client = new PercyClient({ token, clientInfo, environmentInfo, config }); if (server) this.server = createPercyServer(this, port); this.browser = new Browser(this); diff --git a/packages/core/test/percy.test.js b/packages/core/test/percy.test.js index d65043e58..8f930a3d0 100644 --- a/packages/core/test/percy.test.js +++ b/packages/core/test/percy.test.js @@ -26,6 +26,10 @@ describe('Percy', () => { await server.close(); }); + it('loads config and intializes client with config', () => { + expect(percy.client.config).toEqual(percy.config); + }); + it('logs when a snapshot is missing env info', async () => { percy = new Percy({ token: 'PERCY_TOKEN',