From bc3819a91d0f9fe7025e0c22af7153ff44136204 Mon Sep 17 00:00:00 2001 From: Wil Wilsman Date: Wed, 30 Sep 2020 18:21:04 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Rewrite=20exec=20tests=20to=20use?= =?UTF-8?q?=20core=20testing=20server?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/cli-exec/test/exec.test.js | 4 ++++ packages/cli-exec/test/helpers.js | 5 +---- packages/cli-exec/test/ping.test.js | 24 +++++++++++++---------- packages/cli-exec/test/start.test.js | 21 +++++++------------- packages/cli-exec/test/stop.test.js | 29 +++++++++++++++++----------- 5 files changed, 44 insertions(+), 39 deletions(-) diff --git a/packages/cli-exec/test/exec.test.js b/packages/cli-exec/test/exec.test.js index b82fa769a..c59f751d2 100644 --- a/packages/cli-exec/test/exec.test.js +++ b/packages/cli-exec/test/exec.test.js @@ -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([]) diff --git a/packages/cli-exec/test/helpers.js b/packages/cli-exec/test/helpers.js index 42e7d3894..21fda7275 100644 --- a/packages/cli-exec/test/helpers.js +++ b/packages/cli-exec/test/helpers.js @@ -1,5 +1,3 @@ -import nock from 'nock'; -import mock from 'mock-require'; import mockAPI from '@percy/client/test/helper'; beforeEach(() => { @@ -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'; diff --git a/packages/cli-exec/test/ping.test.js b/packages/cli-exec/test/ping.test.js index 35b244600..71de31417 100644 --- a/packages/cli-exec/test/ping.test.js +++ b/packages/cli-exec/test/ping.test.js @@ -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']); diff --git a/packages/cli-exec/test/start.test.js b/packages/cli-exec/test/start.test.js index c3d59ad86..69c1d5ee3 100644 --- a/packages/cli-exec/test/start.test.js +++ b/packages/cli-exec/test/start.test.js @@ -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 () => { @@ -32,6 +24,8 @@ 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 }) @@ -39,9 +33,8 @@ describe('percy exec:start', () => { }); 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([ @@ -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([])); diff --git a/packages/cli-exec/test/stop.test.js b/packages/cli-exec/test/stop.test.js index 9e6f59581..70807788c 100644 --- a/packages/cli-exec/test/stop.test.js +++ b/packages/cli-exec/test/stop.test.js @@ -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 () => { @@ -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']);