Skip to content

Commit

Permalink
Added token support in percy config
Browse files Browse the repository at this point in the history
  • Loading branch information
ninadbstack committed Oct 6, 2023
1 parent 5d95e76 commit bc6674f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
9 changes: 7 additions & 2 deletions packages/client/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down
23 changes: 19 additions & 4 deletions packages/client/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -1338,6 +1334,10 @@ describe('PercyClient', () => {
});

describe('#getToken', () => {
afterEach(() => {
delete process.env.PERCY_TOKEN;
})

Check failure on line 1339 in packages/client/test/client.test.js

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon

it('should throw error when called with true', () => {
const client = new PercyClient({});
expect(() => {
Expand All @@ -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'

Check failure on line 1356 in packages/client/test/client.test.js

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
const client = new PercyClient({
config: { percy: { token: 'DONT_USE_THIS' }}

Check failure on line 1358 in packages/client/test/client.test.js

View workflow job for this annotation

GitHub Actions / Lint

A space is required before '}'
});
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' }}

Check failure on line 1365 in packages/client/test/client.test.js

View workflow job for this annotation

GitHub Actions / Lint

A space is required before '}'
});
expect(client.getToken()).toBe('USE_THIS_TOKEN');
});
});
});
3 changes: 3 additions & 0 deletions packages/core/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export const configSchema = {
properties: {
deferUploads: {
type: 'boolean'
},
token: {
type: 'string'
}
}
},
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/percy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);

Expand Down
4 changes: 4 additions & 0 deletions packages/core/test/percy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit bc6674f

Please sign in to comment.