From ccb80f96d57d0d67c61ecbdebd489f4db5d321e1 Mon Sep 17 00:00:00 2001 From: Wil Wilsman Date: Wed, 2 Sep 2020 13:29:02 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20core=20method=20to=20return?= =?UTF-8?q?=20local=20server=20address?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/percy.js | 7 +++++++ packages/core/test/percy.test.js | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/packages/core/src/percy.js b/packages/core/src/percy.js index 8fab6ff14..b574c9d47 100644 --- a/packages/core/src/percy.js +++ b/packages/core/src/percy.js @@ -73,6 +73,13 @@ export default class Percy { return log.loglevel(level); } + // Snapshot server API address + apiAddress() { + let { address } = this.server?.address() || {}; + address = address?.includes(':') ? `[${address}]` : address; + return address && `http://${address}:${this.port}/percy`; + } + // Returns a boolean indicating if this instance is running. isRunning() { return this.#running; diff --git a/packages/core/test/percy.test.js b/packages/core/test/percy.test.js index 9f771cc97..a8b628bdb 100644 --- a/packages/core/test/percy.test.js +++ b/packages/core/test/percy.test.js @@ -98,6 +98,17 @@ describe('Percy', () => { }); }); + describe('#apiAddress()', () => { + it('returns undefined when the server is not running', () => { + expect(percy.apiAddress()).toBeUndefined(); + }); + + it('returns the server API address when it is running', async () => { + await percy.start(); + expect(percy.apiAddress()).toEqual('http://[::]:5338/percy'); + }); + }); + describe('#start()', () => { it('creates a build', async () => { await expect(percy.start()).resolves.toBeUndefined();