Skip to content

Commit 2f9c86d

Browse files
author
Thomas Belin
committed
convert to jest
1 parent 848a761 commit 2f9c86d

12 files changed

+90
-64
lines changed

packages/api-client/babel.config.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,5 @@
1818
*/
1919

2020
module.exports = {
21-
plugins: [
22-
'@babel/plugin-proposal-optional-chaining',
23-
'@babel/plugin-proposal-class-properties',
24-
'@babel/plugin-proposal-object-rest-spread',
25-
],
26-
presets: ['@babel/preset-react', '@babel/preset-typescript'],
21+
presets: [['@babel/preset-env', {targets: {node: 'current'}}], '@babel/preset-typescript'],
2722
};

packages/api-client/jest.config.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Wire
3+
* Copyright (C) 2022 Wire Swiss GmbH
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see http://www.gnu.org/licenses/.
17+
*
18+
*/
19+
20+
21+
export default {
22+
clearMocks: true,
23+
testMatch: ['**/?(*.)+(spec|test).node.[t]s?(x)'],
24+
coverageDirectory: 'coverage',
25+
};

packages/api-client/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
"@babel/plugin-proposal-class-properties": "7.14.5",
2222
"@babel/plugin-proposal-object-rest-spread": "7.14.7",
2323
"@babel/plugin-proposal-optional-chaining": "7.14.5",
24+
"@babel/preset-env": "^7.19.1",
2425
"@babel/preset-react": "7.14.5",
2526
"@babel/preset-typescript": "7.14.5",
27+
"@types/babel__preset-env": "^7",
2628
"@types/jasmine": "3.8.2",
29+
"@types/jest": "^29.0.3",
2730
"@types/karma": "6.3.3",
2831
"@types/node": "^14.18.29",
2932
"@types/spark-md5": "3.0.2",

packages/api-client/src/APIClient.test.node.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,10 @@ describe('APIClient', () => {
281281
const client = new APIClient();
282282
const testError = new Error('Test rejection');
283283

284-
spyOn(client.api.auth, 'postLogout').and.returnValue(Promise.reject(testError));
285-
spyOn(client, 'disconnect').and.returnValue();
286-
spyOn(client['accessTokenStore'], 'delete').and.returnValue(Promise.resolve(undefined));
287-
spyOn(client['logger'], 'warn').and.returnValue();
284+
jest.spyOn(client.api.auth, 'postLogout').mockReturnValue(Promise.reject(testError));
285+
jest.spyOn(client, 'disconnect').mockReturnValue();
286+
jest.spyOn(client['accessTokenStore'], 'delete').mockReturnValue(Promise.resolve(undefined));
287+
jest.spyOn(client['logger'], 'warn').mockReturnValue();
288288

289289
await client.logout();
290290
expect(client['logger'].warn).toHaveBeenCalledWith(testError);
@@ -293,10 +293,10 @@ describe('APIClient', () => {
293293
it('skips request when told to', async () => {
294294
const client = new APIClient();
295295

296-
spyOn(client.api.auth, 'postLogout');
297-
spyOn(client, 'disconnect').and.returnValue();
298-
spyOn(client['accessTokenStore'], 'delete').and.returnValue(Promise.resolve(undefined));
299-
spyOn(client['logger'], 'warn').and.returnValue();
296+
jest.spyOn(client.api.auth, 'postLogout');
297+
jest.spyOn(client, 'disconnect').mockReturnValue();
298+
jest.spyOn(client['accessTokenStore'], 'delete').mockReturnValue(Promise.resolve(undefined));
299+
jest.spyOn(client['logger'], 'warn').mockReturnValue();
300300

301301
await client.logout({skipLogoutRequest: true});
302302
expect(client['logger'].warn).not.toHaveBeenCalled();

packages/api-client/src/asset/AssetAPI.test.node.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('"AssetAPI"', () => {
2424
const apiClient = new APIClient();
2525

2626
it('adds token parameters', async () => {
27-
spyOn(apiClient.transport.http, 'sendRequest').and.returnValue(
27+
jest.spyOn(apiClient.transport.http, 'sendRequest').mockReturnValue(
2828
Promise.resolve({
2929
config: {},
3030
data: '',
@@ -39,18 +39,18 @@ describe('"AssetAPI"', () => {
3939
await apiClient.api.asset.getAssetV3(assetId, assetToken);
4040

4141
expect(apiClient.transport.http.sendRequest).toHaveBeenCalledWith(
42-
jasmine.objectContaining({
42+
expect.objectContaining({
4343
params: {
4444
asset_token: assetToken,
4545
},
46-
url: jasmine.stringMatching(new RegExp(assetId)),
46+
url: expect.stringMatching(new RegExp(assetId)),
4747
}),
4848
true,
4949
);
5050
});
5151

5252
it('removes token parameters', async () => {
53-
spyOn(apiClient.transport.http, 'sendRequest').and.returnValue(
53+
jest.spyOn(apiClient.transport.http, 'sendRequest').mockReturnValue(
5454
Promise.resolve({
5555
config: {},
5656
data: '',
@@ -64,9 +64,9 @@ describe('"AssetAPI"', () => {
6464
await apiClient.api.asset.getAssetV3(assetId);
6565

6666
expect(apiClient.transport.http.sendRequest).toHaveBeenCalledWith(
67-
jasmine.objectContaining({
67+
expect.objectContaining({
6868
params: {},
69-
url: jasmine.stringMatching(new RegExp(assetId)),
69+
url: expect.stringMatching(new RegExp(assetId)),
7070
}),
7171
true,
7272
);

packages/api-client/src/auth/AuthAPI.test.node.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('AuthAPI', () => {
3131
password: '[email protected]',
3232
};
3333

34-
spyOn(apiClient.transport.http, 'sendJSON').and.returnValue(
34+
jest.spyOn(apiClient.transport.http, 'sendJSON').mockReturnValue(
3535
Promise.resolve({
3636
config: {},
3737
data: '',
@@ -44,7 +44,7 @@ describe('AuthAPI', () => {
4444
await apiClient.api.auth.postLogin(data);
4545

4646
expect(apiClient.transport.http.sendJSON).toHaveBeenCalledWith(
47-
jasmine.objectContaining({
47+
expect.objectContaining({
4848
data: {
4949
clientType: undefined,
5050
email: data.email,
@@ -64,7 +64,7 @@ describe('AuthAPI', () => {
6464
password: '[email protected]',
6565
};
6666

67-
spyOn(apiClient.transport.http, 'sendJSON').and.returnValue(
67+
jest.spyOn(apiClient.transport.http, 'sendJSON').mockReturnValue(
6868
Promise.resolve({
6969
config: {},
7070
data: '',
@@ -77,7 +77,7 @@ describe('AuthAPI', () => {
7777
await apiClient.api.auth.postLogin(data);
7878

7979
expect(apiClient.transport.http.sendJSON).toHaveBeenCalledWith(
80-
jasmine.objectContaining({
80+
expect.objectContaining({
8181
data: {
8282
clientType: undefined,
8383
email: data.email,

packages/api-client/src/conversation/ConversationAPI/ConversationAPI.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ describe('ConversationAPI', () => {
3232
it('add ignore_missing and report_missing parameters', async () => {
3333
await conversationApi.postOTRMessage('client-id', 'conv-id', undefined, false);
3434
expect(httpClientMock.sendJSON).toHaveBeenCalledWith(
35-
jasmine.objectContaining({
35+
expect.objectContaining({
3636
params: {ignore_missing: false},
3737
}),
3838
true,
3939
);
4040

4141
await conversationApi.postOTRMessage('client-id', 'conv-id', undefined, true);
4242
expect(httpClientMock.sendJSON).toHaveBeenCalledWith(
43-
jasmine.objectContaining({params: {ignore_missing: true}}),
43+
expect.objectContaining({params: {ignore_missing: true}}),
4444
true,
4545
);
4646

4747
await conversationApi.postOTRMessage('client-id', 'conv-id', undefined, ['user1', 'user2']);
4848
expect(httpClientMock.sendJSON).toHaveBeenCalledWith(
49-
jasmine.objectContaining({
49+
expect.objectContaining({
5050
params: {ignore_missing: 'user1,user2'},
5151
}),
5252
true,

packages/api-client/src/http/BackendErrorMapper.test.node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('BackendErrorMapper', () => {
3434
};
3535

3636
const userError = BackendErrorMapper.map(userIdError);
37-
expect(userError).toEqual(jasmine.any(UserIsUnknownError));
37+
expect(userError).toEqual(expect.any(UserIsUnknownError));
3838

3939
const conversationIdError = {
4040
code: StatusCode.BAD_REQUEST,
@@ -44,7 +44,7 @@ describe('BackendErrorMapper', () => {
4444
};
4545

4646
const conversationError = BackendErrorMapper.map(conversationIdError);
47-
expect(conversationError).toEqual(jasmine.any(ConversationIsUnknownError));
47+
expect(conversationError).toEqual(expect.any(ConversationIsUnknownError));
4848
});
4949
});
5050
});

packages/api-client/src/tcp/ReconnectingWebsocket.test.node.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ describe('ReconnectingWebsocket', () => {
9090
});
9191

9292
it('calls "onReconnect", "onOpen" and "onClose"', done => {
93-
const onReconnect = jasmine.createSpy().and.returnValue(getServerAddress());
93+
const onReconnect = jest.fn().mockReturnValue(getServerAddress());
9494
const RWS = new ReconnectingWebsocket(onReconnect);
9595
RWS.setOnOpen(() => {
96-
expect(onReconnect.calls.count()).toBe(1);
96+
expect(onReconnect).toHaveBeenCalledTimes(1);
9797
RWS.disconnect();
9898
});
9999
RWS.setOnClose(event => {
@@ -104,15 +104,15 @@ describe('ReconnectingWebsocket', () => {
104104
});
105105

106106
it('closes the connection without reconnecting when server terminates the connection', done => {
107-
const onReconnect = jasmine.createSpy().and.returnValue(getServerAddress());
107+
const onReconnect = jest.fn().mockReturnValue(getServerAddress());
108108
const RWS = new ReconnectingWebsocket(onReconnect);
109109
RWS.setOnOpen(() => {
110-
expect(onReconnect.calls.count()).toBe(1);
110+
expect(onReconnect).toHaveBeenCalledTimes(1);
111111
RWS.send('terminate');
112112
});
113113
RWS.setOnClose(event => {
114114
expect(event.wasClean).toBe(true);
115-
expect(onReconnect.calls.count()).toBe(1);
115+
expect(onReconnect).toHaveBeenCalledTimes(1);
116116
done();
117117
});
118118
RWS.connect();
@@ -124,14 +124,14 @@ describe('ReconnectingWebsocket', () => {
124124
*/
125125
it('reconnects', done => {
126126
let reconnectCalls = 0;
127-
const onReconnect = jasmine.createSpy().and.returnValue(getServerAddress());
127+
const onReconnect = jest.fn().mockReturnValue(getServerAddress());
128128
const RWS = new ReconnectingWebsocket(onReconnect);
129129

130130
RWS.connect();
131131

132132
RWS.setOnOpen(() => {
133133
reconnectCalls++;
134-
expect(onReconnect.calls.count()).toBe(reconnectCalls);
134+
expect(onReconnect).toHaveBeenCalledTimes(reconnectCalls);
135135
if (reconnectCalls === 1) {
136136
RWS['socket']!.reconnect();
137137
} else {

packages/api-client/src/tcp/WebSocketClient.test.node.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,76 +59,76 @@ describe('WebSocketClient', () => {
5959
describe('handler', () => {
6060
it('calls "onOpen" when WebSocket opens', async () => {
6161
const websocketClient = new WebSocketClient('url', fakeHttpClient);
62-
const onOpenSpy = spyOn<any>(websocketClient, 'onOpen').and.callThrough();
62+
const onOpenSpy = jest.spyOn(websocketClient as any, 'onOpen');
6363
const socket = websocketClient['socket'];
64-
spyOn<any>(socket, 'getReconnectingWebsocket').and.returnValue(fakeSocket);
64+
jest.spyOn(socket as any, 'getReconnectingWebsocket').mockReturnValue(fakeSocket);
6565

6666
await websocketClient.connect();
6767
fakeSocket.onopen();
6868

69-
expect(onOpenSpy.calls.count()).toBe(1);
69+
expect(onOpenSpy).toHaveBeenCalledTimes(1);
7070
});
7171

7272
it('calls "onClose" when WebSocket closes', async () => {
7373
const websocketClient = new WebSocketClient('url', fakeHttpClient);
74-
const onCloseSpy = spyOn<any>(websocketClient, 'onClose').and.callThrough();
74+
const onCloseSpy = jest.spyOn(websocketClient as any, 'onClose');
7575
const socket = websocketClient['socket'];
76-
spyOn<any>(socket, 'getReconnectingWebsocket').and.returnValue(fakeSocket);
76+
jest.spyOn(socket as any, 'getReconnectingWebsocket').mockReturnValue(fakeSocket);
7777

7878
await websocketClient.connect();
7979
fakeSocket.onclose();
8080

81-
expect(onCloseSpy.calls.count()).toBe(1);
81+
expect(onCloseSpy).toHaveBeenCalledTimes(1);
8282
});
8383

8484
it('calls "onError" when WebSocket received error', async () => {
8585
const websocketClient = new WebSocketClient('url', fakeHttpClient);
86-
const onErrorSpy = spyOn<any>(websocketClient, 'onError').and.callThrough();
87-
const refreshTokenSpy = spyOn<any>(websocketClient, 'refreshAccessToken').and.callThrough();
86+
const onErrorSpy = jest.spyOn(websocketClient as any, 'onError');
87+
const refreshTokenSpy = jest.spyOn(websocketClient as any, 'refreshAccessToken');
8888
const socket = websocketClient['socket'];
89-
spyOn<any>(socket, 'getReconnectingWebsocket').and.returnValue(fakeSocket);
89+
jest.spyOn(socket as any, 'getReconnectingWebsocket').mockReturnValue(fakeSocket);
9090

9191
await websocketClient.connect();
9292
fakeSocket.onerror(new Error('error'));
9393

94-
expect(onErrorSpy.calls.count()).toBe(1);
95-
expect(refreshTokenSpy.calls.count()).toBe(1);
94+
expect(onErrorSpy).toHaveBeenCalledTimes(1);
95+
expect(refreshTokenSpy).toHaveBeenCalledTimes(1);
9696
});
9797

9898
it('calls "onMessage" when WebSocket received message', async () => {
9999
const message = 'hello';
100100
const websocketClient = new WebSocketClient('url', fakeHttpClient);
101-
const onMessageSpy = spyOn<any>(websocketClient, 'onMessage').and.callThrough();
101+
const onMessageSpy = jest.spyOn(websocketClient as any, 'onMessage');
102102
const socket = websocketClient['socket'];
103-
spyOn<any>(socket, 'getReconnectingWebsocket').and.returnValue(fakeSocket);
103+
jest.spyOn(socket as any, 'getReconnectingWebsocket').mockReturnValue(fakeSocket);
104104

105105
await websocketClient.connect();
106106
fakeSocket.onmessage({data: Buffer.from(JSON.stringify({message}), 'utf-8')});
107107

108-
expect(onMessageSpy.calls.count()).toBe(1);
108+
expect(onMessageSpy).toHaveBeenCalledTimes(1);
109109
});
110110

111-
it('calls "onConnect" when websocket is connected or re-connected', async () => {
112-
const onConnectResult = jasmine.createSpy().and.returnValue(Promise.resolve());
111+
it('calls "onConnect" when "onReconnect" is called', async () => {
112+
const onConnectResult = jest.fn().mockReturnValue(Promise.resolve());
113113
const onConnect = () => {
114114
return onConnectResult();
115115
};
116116
const websocketClient = new WebSocketClient('url', fakeHttpClient);
117117
const socket = websocketClient['socket'];
118-
spyOn<any>(socket, 'getReconnectingWebsocket').and.returnValue(fakeSocket);
118+
jest.spyOn(socket as any, 'getReconnectingWebsocket').mockReturnValue(fakeSocket);
119119

120120
await websocketClient.connect(undefined, onConnect);
121121
fakeSocket.onopen();
122122
await websocketClient['onReconnect']();
123-
expect(onConnectResult.calls.count()).toBe(1);
123+
expect(onConnectResult).toHaveBeenCalledTimes(1);
124124
});
125125
});
126126

127127
describe('refreshAccessToken', () => {
128128
it('emits the correct message for invalid tokens', async done => {
129129
const websocketClient = new WebSocketClient('url', invalidTokenHttpClient);
130130
const socket = websocketClient['socket'];
131-
spyOn<any>(socket, 'getReconnectingWebsocket').and.returnValue(fakeSocket);
131+
jest.spyOn(socket as any, 'getReconnectingWebsocket').mockReturnValue(fakeSocket);
132132

133133
await websocketClient.connect();
134134

@@ -155,15 +155,15 @@ describe('WebSocketClient', () => {
155155

156156
it('does not lock websocket by default', async done => {
157157
const websocketClient = new WebSocketClient('url', fakeHttpClient);
158-
const onMessageSpy = spyOn<any>(websocketClient, 'onMessage').and.callThrough();
158+
const onMessageSpy = jest.spyOn(websocketClient as any, 'onMessage');
159159
const socket = websocketClient['socket'];
160-
spyOn<any>(socket, 'getReconnectingWebsocket').and.returnValue(fakeSocket);
160+
jest.spyOn(socket as any, 'getReconnectingWebsocket').mockReturnValue(fakeSocket);
161161

162162
await websocketClient.connect();
163163
expect(websocketClient.isLocked()).toBe(false);
164164

165165
websocketClient.on(WebSocketClient.TOPIC.ON_MESSAGE, notification => {
166-
expect(onMessageSpy.calls.count()).toBe(1);
166+
expect(onMessageSpy).toHaveBeenCalledTimes(1);
167167
expect(websocketClient['bufferedMessages'].length).toBe(0);
168168
expect(notification).toEqual(fakeNotification);
169169
done();
@@ -173,21 +173,21 @@ describe('WebSocketClient', () => {
173173

174174
it('emits buffered messages when unlocked', async done => {
175175
const websocketClient = new WebSocketClient('url', fakeHttpClient);
176-
const onMessageSpy = spyOn<any>(websocketClient, 'onMessage').and.callThrough();
176+
const onMessageSpy = jest.spyOn(websocketClient as any, 'onMessage');
177177
const socket = websocketClient['socket'];
178-
spyOn<any>(socket, 'getReconnectingWebsocket').and.returnValue(fakeSocket);
178+
jest.spyOn(socket as any, 'getReconnectingWebsocket').mockReturnValue(fakeSocket);
179179

180180
websocketClient.lock();
181181
await websocketClient.connect();
182182
expect(websocketClient.isLocked()).toBe(true);
183183

184184
fakeSocket.onmessage({data: Buffer.from(JSON.stringify(fakeNotification), 'utf-8')});
185-
expect(onMessageSpy.calls.count()).toBe(1);
185+
expect(onMessageSpy).toHaveBeenCalledTimes(1);
186186
expect(websocketClient['bufferedMessages'].length).toBe(1);
187187

188188
websocketClient.on(WebSocketClient.TOPIC.ON_MESSAGE, notification => {
189189
expect(notification).toEqual(notification);
190-
expect(onMessageSpy.calls.count()).toBe(2);
190+
expect(onMessageSpy).toHaveBeenCalledTimes(2);
191191
done();
192192
});
193193

0 commit comments

Comments
 (0)