Skip to content

Commit 686af31

Browse files
committed
test(client): update client tests for stability
1 parent 3319b0b commit 686af31

File tree

4 files changed

+55
-62
lines changed

4 files changed

+55
-62
lines changed

test/client/clients/SockJSClient.test.js

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,47 +34,42 @@ describe('SockJSClient', () => {
3434
});
3535
});
3636

37-
afterAll(() => {
38-
consoleMock.mockRestore();
39-
});
37+
it('should open, receive message, and close', (done) => {
38+
socketServer.on('connection', (connection) => {
39+
connection.write('hello world');
4040

41-
describe('client', () => {
42-
it('should open, receive message, and close', (done) => {
43-
socketServer.on('connection', (connection) => {
44-
connection.write('hello world');
45-
46-
setTimeout(() => {
47-
connection.close();
48-
}, 1000);
49-
});
41+
setTimeout(() => {
42+
connection.close();
43+
}, 1000);
44+
});
5045

51-
const client = new SockJSClient(`http://localhost:${port}/ws`);
52-
const data = [];
46+
const client = new SockJSClient(`http://localhost:${port}/ws`);
47+
const data = [];
5348

54-
client.onOpen(() => {
55-
data.push('open');
56-
});
57-
client.onClose(() => {
58-
data.push('close');
59-
});
60-
client.onMessage((msg) => {
61-
data.push(msg);
62-
});
49+
client.onOpen(() => {
50+
data.push('open');
51+
});
52+
client.onClose(() => {
53+
data.push('close');
54+
});
55+
client.onMessage((msg) => {
56+
data.push(msg);
57+
});
6358

64-
const testError = new Error('test');
65-
client.sock.onerror(testError);
59+
const testError = new Error('test');
60+
client.sock.onerror(testError);
6661

67-
expect(log.error.mock.calls.length).toEqual(1);
68-
expect(log.error.mock.calls[0]).toEqual([testError]);
62+
expect(log.error.mock.calls.length).toEqual(1);
63+
expect(log.error.mock.calls[0]).toEqual([testError]);
6964

70-
setTimeout(() => {
71-
expect(data).toMatchSnapshot();
72-
done();
73-
}, 3000);
74-
});
65+
setTimeout(() => {
66+
expect(data).toMatchSnapshot();
67+
done();
68+
}, 3000);
7569
});
7670

7771
afterAll((done) => {
72+
consoleMock.mockRestore();
7873
listeningApp.close(() => {
7974
done();
8075
});

test/client/clients/WebsocketClient.test.js

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,40 +31,38 @@ describe('WebsocketClient', () => {
3131
});
3232
});
3333

34-
describe('client', () => {
35-
it('should open, receive message, and close', (done) => {
36-
socketServer.on('connection', (connection) => {
37-
connection.send('hello world');
34+
it('should open, receive message, and close', (done) => {
35+
socketServer.on('connection', (connection) => {
36+
connection.send('hello world');
3837

39-
setTimeout(() => {
40-
connection.close();
41-
}, 1000);
42-
});
38+
setTimeout(() => {
39+
connection.close();
40+
}, 1000);
41+
});
4342

44-
const client = new WebsocketClient(`http://localhost:${port}/ws-server`);
45-
const data = [];
43+
const client = new WebsocketClient(`http://localhost:${port}/ws-server`);
44+
const data = [];
4645

47-
client.onOpen(() => {
48-
data.push('open');
49-
});
50-
client.onClose(() => {
51-
data.push('close');
52-
});
53-
client.onMessage((msg) => {
54-
data.push(msg);
55-
});
46+
client.onOpen(() => {
47+
data.push('open');
48+
});
49+
client.onClose(() => {
50+
data.push('close');
51+
});
52+
client.onMessage((msg) => {
53+
data.push(msg);
54+
});
5655

57-
const testError = new Error('test');
58-
client.client.onerror(testError);
56+
const testError = new Error('test');
57+
client.client.onerror(testError);
5958

60-
expect(log.error.mock.calls.length).toEqual(1);
61-
expect(log.error.mock.calls[0]).toEqual([testError]);
59+
expect(log.error.mock.calls.length).toEqual(1);
60+
expect(log.error.mock.calls[0]).toEqual([testError]);
6261

63-
setTimeout(() => {
64-
expect(data).toMatchSnapshot();
65-
done();
66-
}, 3000);
67-
});
62+
setTimeout(() => {
63+
expect(data).toMatchSnapshot();
64+
done();
65+
}, 3000);
6866
});
6967

7068
afterAll((done) => {

test/client/clients/__snapshots__/SockJSClient.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`SockJSClient client should open, receive message, and close 1`] = `
3+
exports[`SockJSClient should open, receive message, and close 1`] = `
44
Array [
55
"open",
66
"hello world",

test/client/clients/__snapshots__/WebsocketClient.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`WebsocketClient client should open, receive message, and close 1`] = `
3+
exports[`WebsocketClient should open, receive message, and close 1`] = `
44
Array [
55
"open",
66
"hello world",

0 commit comments

Comments
 (0)