Skip to content

Commit

Permalink
✨ In testing mode, clear logs on reset (#989)
Browse files Browse the repository at this point in the history
During testing mode, full logs are rarely useful, but individual snapshot logs are more important to
test against. Clearing logs on reset will allow SDK tests to read only recent and relevant logs
without having to filter them.
  • Loading branch information
wwilsman committed Jul 12, 2022
1 parent 7ca626f commit abf4219
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ export function createPercyServer(percy, port) {
body = Buffer.isBuffer(body) ? body.toString() : body;

if (cmd === 'reset') {
// the reset command will reset testing mode to its default options
// the reset command will reset testing mode and clear any logs
percy.testing = {};
logger.instance.messages.clear();
} else if (cmd === 'version') {
// the version command will update the api version header for testing
percy.testing.version = body;
Expand Down
34 changes: 26 additions & 8 deletions packages/core/test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,19 @@ describe('API Server', () => {
expect(percy.dryRun).toBeTrue();
});

it('enables a /test/snapshot endpoint that serves a simple html document', async () => {
await expectAsync(get('/test/snapshot')).toBeResolvedTo('<p>Snapshot Me!</p>');
});

it('enables a /test/logs endpoint to return raw logs', async () => {
percy.log.info('foo bar from test');
let { logs } = await get('/test/logs');

expect(logs).toEqual(jasmine.arrayContaining([
jasmine.objectContaining({ message: 'foo bar from test' })
]));
});

it('enables several /test/api endpoint commands', async () => {
expect(percy.testing).toEqual({});
await post('/test/api/version', false);
Expand Down Expand Up @@ -300,17 +313,22 @@ describe('API Server', () => {
await expectAsync(req('/percy/healthcheck')).toBeRejected();
});

it('enables a /test/logs endpoint to return raw logs', async () => {
it('can reset testing mode and clear logs via /test/reset', async () => {
// already tested up above
await post('/test/api/version', false);
await post('/test/api/disconnect', '/percy/healthcheck');
percy.log.info('foo bar from test');
let { logs } = await get('/test/logs');

expect(logs).toEqual(jasmine.arrayContaining([
jasmine.objectContaining({ message: 'foo bar from test' })
]));
});
// the actual endpoint to test
await post('/test/api/reset');

it('enables a /test/snapshot endpoint that serves a simple html document', async () => {
await expectAsync(get('/test/snapshot')).toBeResolvedTo('<p>Snapshot Me!</p>');
// everything should work as usual
let { headers } = await req('/percy/healthcheck');
expect(headers['x-percy-core-version']).toBeDefined();

// logs should be empty after reset
let { logs } = await get('/test/logs');
expect(logs).toEqual([]);
});
});
});

0 comments on commit abf4219

Please sign in to comment.