From 9fe0f67664f7f8ae737cd08e31395f3ecea7b23c Mon Sep 17 00:00:00 2001 From: Ninad Sheth Date: Tue, 28 Nov 2023 19:05:54 +0530 Subject: [PATCH] Fixed tests --- packages/core/test/unit/server.test.js | 41 ++++++++++++++------------ 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/packages/core/test/unit/server.test.js b/packages/core/test/unit/server.test.js index e2c5d1247..a05e49cd4 100644 --- a/packages/core/test/unit/server.test.js +++ b/packages/core/test/unit/server.test.js @@ -21,20 +21,8 @@ describe('Unit / Server', () => { }); describe('#host', () => { - beforeEach(async () => { - await server.listen(9000); - server.route('get', '/test/:path', (req, res) => res.text(req.params.path)); - }); - it('returns the host', async () => { expect(server.host).toEqual('0.0.0.0'); - - // test connection via 0.0.0.0 - await expectAsync(request('/test/foo', 'GET')).toBeResolvedTo('foo'); - - // test connection via localhost - let { request } = await import('../helpers/request.js'); - await expectAsync(request(new URL(path, 'localhost'), 'GET')).toBeResolvedTo('foo'); }); describe('with PERCY_SERVER_HOST set', () => { @@ -46,13 +34,8 @@ describe('Unit / Server', () => { delete process.env.PERCY_SERVER_HOST; }); - it('uses correct host', async () => { - // test connection via localhost - await expectAsync(request('/test/foo', 'GET')).toBeResolvedTo('foo'); - - // test connection via 0.0.0.0 - let { request } = await import('../helpers/request.js'); - await expectAsync(request(new URL(path, '0.0.0.0'), 'GET')).toBeRejected(); + it('returns correct host', async () => { + expect(server.host).toEqual('localhost'); }); }); }); @@ -97,6 +80,26 @@ describe('Unit / Server', () => { Server.createServer().listen(server.port) ).toBeRejected(); }); + + describe('with PERCY_SERVER_HOST set', () => { + beforeEach(() => { + process.env.PERCY_SERVER_HOST = 'localhost'; + }); + + afterEach(() => { + delete process.env.PERCY_SERVER_HOST; + }); + + it('listens on correct host', async () => { + expect(server.host).toEqual('localhost'); + await server.listen() + server.route('get', '/test/:path', (req, res) => res.text(req.params.path)); + await expectAsync(request('/test/foo', 'GET')).toBeResolvedTo('foo'); + + // as we have a single network interface locally its not easy to test a negative test + // where with a separate network interface we are unable to access server + }); + }); }); describe('#close()', () => {