Skip to content

Commit

Permalink
test: fix leaky globals
Browse files Browse the repository at this point in the history
  • Loading branch information
Danilo Alonso authored and Danilo Alonso committed Jun 26, 2024
1 parent 349e36d commit 47dca53
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@

options = options || {};

this._isBrowser = typeof WebSocket !== 'undefined' && global.window !== undefined;
// $lab:coverage:off$
this._isBrowser = typeof WebSocket !== 'undefined' && typeof window !== 'undefined';
// $lab:coverage:on$

if (!this._isBrowser) {
options.ws = options.ws || {};
Expand Down
2 changes: 2 additions & 0 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ describe('Client', () => {
delete global.WebSocket;
}

delete global.window;

Nes.Client.WebSocket = Ws;

return new Ws(...args);
Expand Down
7 changes: 0 additions & 7 deletions test/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,8 @@ describe('Listener', () => {
await server.register({ plugin: Nes, options: { auth: false, origin: ['http://localhost:12345'] } });

await server.start();

// mimick a server connection because WebSocket in node 22
// does not work like it does in the browser
const orig = global.WebSocket;
delete global.WebSocket;
const client = new Nes.Client(getUri(server.info), { ws: { origin: 'http://localhost:12345' } });
await client.connect();

global.WebSocket = orig;
client.disconnect();
await server.stop();
});
Expand Down

0 comments on commit 47dca53

Please sign in to comment.