Skip to content

Commit

Permalink
✅ Rewrite exec tests to use core testing server
Browse files Browse the repository at this point in the history
  • Loading branch information
wwilsman committed Sep 30, 2020
1 parent d756fde commit bc3819a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 39 deletions.
4 changes: 4 additions & 0 deletions packages/cli-exec/test/exec.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { stdio } from './helpers';
import { Exec } from '../src/commands/exec';

describe('percy exec', () => {
afterEach(() => {
mock.stopAll();
});

it('logs an error when no command is provided', async () => {
await expect(stdio.capture(() => (
Exec.run([])
Expand Down
5 changes: 1 addition & 4 deletions packages/cli-exec/test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import nock from 'nock';
import mock from 'mock-require';
import mockAPI from '@percy/client/test/helper';

beforeEach(() => {
Expand All @@ -12,9 +10,8 @@ afterEach(() => {
delete process.env.PERCY_ENABLE;
delete process.env.PERCY_PARALLEL_TOTAL;
process.removeAllListeners();
nock.cleanAll();
mock.stopAll();
});

export { mockAPI };
export { default as stdio } from '@percy/logger/test/helper';
export { default as createTestServer } from '@percy/core/test/helpers/server';
24 changes: 14 additions & 10 deletions packages/cli-exec/test/ping.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import expect from 'expect';
import nock from 'nock';
import { stdio } from './helpers';
import { stdio, createTestServer } from './helpers';
import { Ping } from '../src/commands/exec/ping';

describe('percy exec:ping', () => {
let percyServer;

afterEach(async () => {
await percyServer?.close();
});

it('calls the /percy/healthcheck endpoint and logs', async () => {
let request = nock('http://localhost:5338')
.get('/percy/healthcheck').reply(200, { success: true });
percyServer = await createTestServer({
'/percy/healthcheck': () => [200, 'application/json', { success: true }]
}, 5338);

await stdio.capture(() => Ping.run([]));

expect(percyServer.requests).toEqual([['/percy/healthcheck']]);

expect(stdio[2]).toHaveLength(0);
expect(stdio[1]).toEqual(['[percy] Percy is running\n']);
request.done();
});

it('logs an error when the endpoint errors', async () => {
nock('http://localhost:5338').post('/percy/healthcheck').reply(500);

await expect(stdio.capture(() => (
Ping.run([])
))).rejects.toThrow('EEXIT: 1');
await expect(stdio.capture(() => Ping.run([])))
.rejects.toThrow('EEXIT: 1');

expect(stdio[1]).toHaveLength(0);
expect(stdio[2]).toEqual(['[percy] Percy is not running\n']);
Expand Down
21 changes: 7 additions & 14 deletions packages/cli-exec/test/start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@ import expect from 'expect';
import fetch from 'node-fetch';
import { stdio } from './helpers';
import { Start } from '../src/commands/exec/start';
import { Stop } from '../src/commands/exec/stop';

describe('percy exec:start', () => {
async function stop() {
await stdio.capture(() => (
fetch('http://localhost:5338/percy/stop', {
method: 'POST',
timeout: 100
}).catch(() => {})
));
}

beforeEach(async () => {
await Start.run(['--quiet']);
});

afterEach(async () => {
await stop();
await Stop.run(['--silent']).catch(() => {});
});

it('starts a long-running percy process', async () => {
Expand All @@ -32,16 +24,17 @@ describe('percy exec:start', () => {
).resolves.toBeDefined();

process.emit('SIGTERM');
// wait for event to be handled
await new Promise(r => setTimeout(r, 100));

await expect(
fetch('http://localhost:5338/percy/healthcheck', { timeout: 10 })
).rejects.toThrow();
});

it('logs an error when percy is already running', async () => {
await expect(stdio.capture(() => (
Start.run([])
))).rejects.toThrow('EEXIT: 1');
await expect(stdio.capture(() => Start.run([])))
.rejects.toThrow('EEXIT: 1');

expect(stdio[1]).toHaveLength(0);
expect(stdio[2]).toEqual([
Expand All @@ -50,7 +43,7 @@ describe('percy exec:start', () => {
});

it('logs when percy has been disabled', async () => {
await stop();
await Stop.run(['--quiet']);

process.env.PERCY_ENABLE = '0';
await stdio.capture(() => Start.run([]));
Expand Down
29 changes: 18 additions & 11 deletions packages/cli-exec/test/stop.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import expect from 'expect';
import nock from 'nock';
import { stdio } from './helpers';
import { stdio, createTestServer } from './helpers';
import { Stop } from '../src/commands/exec/stop';

describe('percy exec:stop', () => {
it('calls the /percy/stop endpoint and logs', async () => {
let request = nock('http://localhost:5338')
.post('/percy/stop').reply(200, { success: true });
let percyServer;

afterEach(async () => {
await percyServer?.close();
});

it('calls the /percy/stop endpoint and logs when the server is down', async () => {
percyServer = await createTestServer({
'/percy/stop': () => [200, 'application/json', { success: true }]
}, 5338);

await stdio.capture(() => Stop.run([]));

expect(percyServer.requests).toEqual([
['/percy/stop'],
['/percy/healthcheck']
]);

expect(stdio[2]).toHaveLength(0);
expect(stdio[1]).toEqual(['[percy] Percy has stopped\n']);
request.done();
});

it('logs when percy is disabled', async () => {
Expand All @@ -24,11 +34,8 @@ describe('percy exec:stop', () => {
});

it('logs an error when the endpoint errors', async () => {
nock('http://localhost:5338').post('/percy/stop').reply(500);

await expect(stdio.capture(() => (
Stop.run([])
))).rejects.toThrow('EEXIT: 1');
await expect(stdio.capture(() => Stop.run([])))
.rejects.toThrow('EEXIT: 1');

expect(stdio[1]).toHaveLength(0);
expect(stdio[2]).toEqual(['[percy] Percy is not running\n']);
Expand Down

0 comments on commit bc3819a

Please sign in to comment.