Skip to content

Commit dd31d8c

Browse files
committed
Switch to node test runner and assert
1 parent eacafc1 commit dd31d8c

File tree

1 file changed

+49
-84
lines changed

1 file changed

+49
-84
lines changed

test/client.js

Lines changed: 49 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict';
22

33
const { Metacom } = require('metacom/lib/client');
4-
const metatests = require('metatests');
4+
5+
const test = require('node:test');
6+
const assert = require('node:assert/strict');
57

68
const { Blob } = require('node:buffer');
79
const fs = require('node:fs');
@@ -17,122 +19,110 @@ const START_DELAY = 4000;
1719

1820
const runTests = async (wsClient, wsToken, wsApi, url) => {
1921
const tests = {
20-
'system/introspect': async (test) => {
22+
'system/introspect': async () => {
2123
const units = ['auth', 'console', 'example', 'files', 'test'];
2224
const introspect = await wsClient.scaffold('system')('introspect')(units);
23-
test.strictSame(introspect?.auth?.restore?.[0], 'token');
24-
test.end();
25+
assert.strictEqual(introspect?.auth?.restore?.[0], 'token');
2526
},
2627

27-
'example/add': async (test) => {
28+
'example/add': async () => {
2829
const add = await wsApi.example.add({ a: 1, b: 2 });
29-
test.strictSame(add, 3);
30-
test.end();
30+
assert.strictEqual(add, 3);
3131
},
3232

33-
'example/citiesByCountry': async (test) => {
33+
'example/citiesByCountry': async () => {
3434
const cities = await wsApi.example.citiesByCountry({
3535
countryId: 1,
3636
});
37-
test.strictEqual(cities?.result, 'success');
38-
test.strictEqual(Array.isArray(cities?.data), true);
39-
test.end();
37+
assert.strictEqual(cities?.result, 'success');
38+
assert.strictEqual(Array.isArray(cities?.data), true);
4039
},
4140

42-
'example/customError': async (test) => {
41+
'example/customError': async () => {
4342
try {
4443
await wsApi.example.customError();
4544
} catch (customError) {
46-
test.errorCompare(customError, new Error('Return custom error', 12345));
47-
test.strictEqual(customError?.code, 12345);
48-
} finally {
49-
test.end();
45+
assert.strictEqual(customError.message, 'Return custom error');
46+
assert.strictEqual(customError?.code, 12345);
5047
}
5148
},
5249

53-
'example/customException': async (test) => {
50+
'example/customException': async () => {
5451
try {
5552
await wsApi.example.customException();
5653
} catch (customError) {
57-
test.errorCompare(customError, new Error('Custom ecxeption', 12345));
58-
test.strictEqual(customError?.code, 12345);
59-
} finally {
60-
test.end();
54+
assert.strictEqual(customError.message, 'Custom ecxeption');
55+
assert.strictEqual(customError?.code, 12345);
6156
}
6257
},
6358

64-
'example/error': async (test) => {
59+
'example/error': async () => {
6560
try {
6661
await wsApi.example.error();
6762
} catch (err) {
68-
test.errorCompare(err, new Error('Return error'));
69-
} finally {
70-
test.end();
63+
assert.strictEqual(err.message, 'Return error');
7164
}
7265
},
7366

74-
'example/exception': async (test) => {
67+
'example/exception': async () => {
7568
try {
7669
await wsApi.example.exception();
7770
} catch (err) {
78-
test.errorCompare(err, new Error('Example exception'));
79-
} finally {
80-
test.end();
71+
assert.strictEqual(err.message, 'Internal Server Error');
8172
}
8273
},
8374

84-
'example/getClientInfo': async (test) => {
75+
'example/getClientInfo': async () => {
8576
const info = await wsApi.example.getClientInfo();
86-
test.strictEqual(info?.result?.ip, HOST);
87-
test.strictEqual(info?.result?.token, wsToken);
88-
test.strictEqual(info?.result?.accountId, ACCOUNT_ID);
89-
test.end();
77+
assert.strictEqual(info?.result?.ip, HOST);
78+
assert.strictEqual(info?.result?.token, wsToken);
79+
assert.strictEqual(info?.result?.accountId, ACCOUNT_ID);
9080
},
9181

92-
'example/redisSet + redisGet': async (test) => {
82+
'example/redisSet + redisGet': async () => {
9383
const setting = await wsApi.example.redisSet({
9484
key: 'MetarhiaExampleTest',
9585
value: 1,
9686
});
9787
const getting = await wsApi.example.redisGet({
9888
key: 'MetarhiaExampleTest',
9989
});
100-
test.strictEqual(setting?.result, 'OK');
101-
test.strictEqual(getting?.result, '1');
102-
103-
test.end();
90+
assert.strictEqual(setting?.result, 'OK');
91+
assert.strictEqual(getting?.result, '1');
10492
},
10593

106-
'example/resources': async (test) => {
94+
'example/resources': async () => {
10795
const resources = await wsApi.example.resources();
108-
test.strictEqual(resources?.total, null);
109-
test.end();
96+
assert.strictEqual(resources?.total, null);
11097
},
11198

112-
hook: async (test) => {
99+
hook: async () => {
113100
const hook = await testHook({
114101
url,
115102
path: '/api/hook',
116103
argsString: 'arg1=2&mem=3',
117104
});
118105

119-
test.strictEqual(hook?.success, true);
120-
test.end();
106+
assert.strictEqual(hook?.success, true);
121107
},
122108

123-
'example/subscribe': async (test) => {
124-
const wait = await wsApi.example.wait({ delay: 1000 });
125-
test.strictEqual(wait, 'done');
126-
test.end();
109+
'example/subscribe': async () => {
110+
const res = await wsApi.example.subscribe({ test: true });
111+
assert.deepEqual(res, { subscribed: 'resmon' });
112+
await new Promise((resolve) => {
113+
wsApi.example.once('resmon', (event) => {
114+
console.log({ event });
115+
resolve();
116+
});
117+
});
127118
},
128119

129-
'example/wait': async (test) => {
120+
'example/wait': async () => {
130121
const wait = await wsApi.example.wait({ delay: 1000 });
131-
test.strictEqual(wait, 'done');
132-
test.end();
122+
assert.strictEqual(wait, 'done');
133123
},
134124

135-
'file/upload': async (test) => {
125+
'file/upload': async () => {
136126
const file = 'sunset.jpg';
137127
const path = './test/uploading/' + file;
138128
const content = await fsp.readFile(path);
@@ -145,13 +135,12 @@ const runTests = async (wsClient, wsToken, wsApi, url) => {
145135
streamId: uploader.id,
146136
name: file,
147137
});
148-
test.strictEqual(res?.result, 'Stream initialized');
138+
assert.strictEqual(res?.result, 'Stream initialized');
149139
// Start uploading stream and wait for its end
150140
await uploader.upload();
151-
test.end();
152141
},
153142

154-
'example/uploadFile': async (test) => {
143+
'example/uploadFile': async () => {
155144
const file = 'sunset.jpg';
156145
const path = './test/uploading/' + file;
157146
const content = await fsp.readFile(path);
@@ -160,32 +149,17 @@ const runTests = async (wsClient, wsToken, wsApi, url) => {
160149
data: content.toJSON(),
161150
name: file,
162151
});
163-
test.strictEqual(res?.uploaded, content?.length);
164-
test.end();
152+
assert.strictEqual(res?.uploaded, content?.length);
165153
},
166154
};
167155

168-
const results = [];
156+
const prom = [];
169157
console.log(`Run ${Object.entries(tests).length} tests`);
170158
for (const [caption, func] of Object.entries(tests)) {
171-
results.push(metatests.testAsync(caption, func));
159+
prom.push(test.test(caption, func));
172160
}
173161

174-
await new Promise((resolve) => {
175-
const timer = setInterval(() => {
176-
let done = true;
177-
for (const res of results) {
178-
if (!res.done) {
179-
done = false;
180-
break;
181-
}
182-
}
183-
if (done) {
184-
clearInterval(timer);
185-
resolve();
186-
}
187-
}, 1000);
188-
});
162+
await Promise.allSettled(prom);
189163
};
190164

191165
const main = async () => {
@@ -202,15 +176,6 @@ const main = async () => {
202176
const res = await wsApi.auth.signin({ login: LOGIN, password: PASSWORD });
203177
const wsToken = res.token;
204178

205-
// const httpClient = Metacom.create(url + '/api');
206-
// const httpApi = httpClient.api;
207-
// await httpClient.load('auth', 'console', 'example', 'files');
208-
// const httpSignin = await httpApi.auth.signin({
209-
// login: LOGIN,
210-
// password: PASSWORD,
211-
// });
212-
// const httpToken = httpSignin?.token;
213-
214179
wsClient.on('close', process.exit);
215180
setTimeout(() => {
216181
console.info('Stop tests by timeout');

0 commit comments

Comments
 (0)