Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ninadbstack committed Nov 28, 2023
1 parent 8c65a65 commit 3a2619c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/core/test/percy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

Expand Down
45 changes: 24 additions & 21 deletions packages/core/test/unit/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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');
});
});
});
Expand All @@ -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');
});
});

Expand All @@ -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()

Check failure on line 95 in packages/core/test/unit/server.test.js

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon
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()', () => {
Expand Down

0 comments on commit 3a2619c

Please sign in to comment.