From 3a2619cc53b39f05b57e2174ba9df4016c36cc6a Mon Sep 17 00:00:00 2001
From: Ninad Sheth <ninad@browserstack.com>
Date: Tue, 28 Nov 2023 19:05:54 +0530
Subject: [PATCH] Fixed tests

---
 packages/core/test/percy.test.js       |  2 +-
 packages/core/test/unit/server.test.js | 45 ++++++++++++++------------
 2 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/packages/core/test/percy.test.js b/packages/core/test/percy.test.js
index 96000e838..3f14b81d8 100644
--- a/packages/core/test/percy.test.js
+++ b/packages/core/test/percy.test.js
@@ -149,7 +149,7 @@ describe('Percy', () => {
 
   describe('#address()', () => {
     it('returns the server API address', async () => {
-      expect(percy.address()).toEqual('http://localhost:5338');
+      expect(percy.address()).toEqual('http://0.0.0.0:5338');
     });
   });
 
diff --git a/packages/core/test/unit/server.test.js b/packages/core/test/unit/server.test.js
index e2c5d1247..648c4b923 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');
       });
     });
   });
@@ -70,11 +53,11 @@ describe('Unit / Server', () => {
 
   describe('#address()', () => {
     it('returns the localhost address for the server', () => {
-      expect(server.address()).toEqual('http://localhost:8000');
+      expect(server.address()).toEqual('http://0.0.0.0:8000');
     });
 
     it('does not include the port without a default when not listening', () => {
-      expect(Server.createServer().address()).toEqual('http://localhost');
+      expect(Server.createServer().address()).toEqual('http://0.0.0.0');
     });
   });
 
@@ -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()', () => {