Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Added token support in percy config #1388

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
});

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';
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');
});
});
});
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
Loading