Skip to content

Commit 67f10b6

Browse files
committed
test: 💍 gracefully exit from tests
1 parent 571aad3 commit 67f10b6

File tree

5 files changed

+48
-24
lines changed

5 files changed

+48
-24
lines changed

src/__demos__/json-crdt-server/__tests__/setup.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export const setup = async () => {
1414
});
1515
const call = client.call.bind(client);
1616
const call$ = client.call$.bind(client);
17-
return {call, call$};
17+
const stop = () => {};
18+
return {call, call$, stop};
1819
};
1920

2021
export type JsonCrdtTestSetup = typeof setup;

src/__tests__/json-crdt-server/block.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const runBlockTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
1414
describe('block.*', () => {
1515
describe('block.new', () => {
1616
test('can create an empty block', async () => {
17-
const {call} = await setup();
17+
const {call, stop} = await setup();
1818
const id = getId();
1919
await call('block.new', {id, patches: []});
2020
const {model} = await call('block.get', {id});
@@ -27,10 +27,11 @@ export const runBlockTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
2727
});
2828
const model2 = Model.fromBinary(model.blob);
2929
expect(model2.view()).toBe(undefined);
30+
stop();
3031
});
3132

3233
test('can create a block with value', async () => {
33-
const {call} = await setup();
34+
const {call, stop} = await setup();
3435
const model = Model.withLogicalClock();
3536
const id = getId();
3637
model.api.root({
@@ -66,12 +67,13 @@ export const runBlockTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
6667
name: 'Super Woman',
6768
age: 26,
6869
});
70+
stop();
6971
});
7072
});
7173

7274
describe('block.remove', () => {
7375
test('can remove an existing block', async () => {
74-
const {call} = await setup();
76+
const {call, stop} = await setup();
7577
const id = getId();
7678
await call('block.new', {id, patches: []});
7779
const {model} = await call('block.get', {id});
@@ -83,12 +85,13 @@ export const runBlockTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
8385
} catch (err: any) {
8486
expect(err.errno).toBe(RpcErrorCodes.NOT_FOUND);
8587
}
88+
stop();
8689
});
8790
});
8891

8992
describe('block.upd', () => {
9093
test('can edit a document sequentially', async () => {
91-
const {call} = await setup();
94+
const {call, stop} = await setup();
9295
const id = getId();
9396
const model = Model.withLogicalClock();
9497
model.api.root({
@@ -149,10 +152,11 @@ export const runBlockTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
149152
expect(Model.fromBinary(block3.model.blob).view()).toStrictEqual({
150153
text: 'Hello, World!',
151154
});
155+
stop();
152156
});
153157

154158
test('can edit a document concurrently', async () => {
155-
const {call} = await setup();
159+
const {call, stop} = await setup();
156160
const id = getId();
157161

158162
// User 1
@@ -215,10 +219,11 @@ export const runBlockTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
215219
const block4 = await call('block.get', {id});
216220
const model4 = Model.fromBinary(block4.model.blob).fork();
217221
expect(model4.view()).not.toStrictEqual({text: 'Hell yeah!'});
222+
stop();
218223
});
219224

220225
test('returns patches that happened concurrently', async () => {
221-
const {call} = await setup();
226+
const {call, stop} = await setup();
222227
const id = getId();
223228

224229
// User 1
@@ -278,13 +283,14 @@ export const runBlockTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
278283
expect(patches[2].seq).toBe(3);
279284
expect(patches[1].blob).toStrictEqual(patch2.toBinary());
280285
expect(patches[2].blob).toStrictEqual(patch3.toBinary());
286+
stop();
281287
});
282288
});
283289

284290
if (!params.staticOnly) {
285291
describe('block.listen', () => {
286292
test('can listen for block changes', async () => {
287-
const {call, call$} = await setup();
293+
const {call, call$, stop} = await setup();
288294
const id = getId();
289295
await call('block.new', {id, patches: []});
290296
await tick(11);
@@ -321,10 +327,11 @@ export const runBlockTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
321327
expect(emits[1][0]).toBe('upd');
322328
expect(emits[1][1].patches.length).toBe(1);
323329
expect(emits[1][1].patches[0].seq).toBe(1);
330+
stop();
324331
});
325332

326333
test('can subscribe before block is created', async () => {
327-
const {call, call$} = await setup();
334+
const {call, call$, stop} = await setup();
328335
const emits: any[] = [];
329336
const id = getId();
330337
call$('block.listen', {id}).subscribe((data) => emits.push(data));
@@ -349,10 +356,11 @@ export const runBlockTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
349356
expect(emits[0][1].patches.length).toBe(1);
350357
expect(emits[0][1].patches[0].seq).toBe(0);
351358
expect(emits[0][1].patches[0].blob).toStrictEqual(patch1.toBinary());
359+
stop();
352360
});
353361

354362
test('can receive deletion events', async () => {
355-
const {call, call$} = await setup();
363+
const {call, call$, stop} = await setup();
356364
const emits: any[] = [];
357365
const id = getId();
358366
call$('block.listen', {id}).subscribe((data) => {
@@ -365,13 +373,14 @@ export const runBlockTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
365373
await call('block.del', {id});
366374
await until(() => emits.length === 2);
367375
expect(emits[1][0]).toBe('del');
376+
stop();
368377
});
369378
});
370379
}
371380

372381
describe('block.scan', () => {
373382
test('can retrieve change history', async () => {
374-
const {call} = await setup();
383+
const {call, stop} = await setup();
375384
const id = getId();
376385
const model = Model.withLogicalClock();
377386
model.api.root({
@@ -428,12 +437,13 @@ export const runBlockTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
428437
},
429438
],
430439
});
440+
stop();
431441
});
432442
});
433443

434444
describe('block.get', () => {
435445
test('returns whole history when block is loaded', async () => {
436-
const {call} = await setup();
446+
const {call, stop} = await setup();
437447
const id = getId();
438448
const model = Model.withLogicalClock();
439449
model.api.root({
@@ -496,6 +506,7 @@ export const runBlockTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
496506
},
497507
],
498508
});
509+
stop();
499510
});
500511
});
501512
});

src/__tests__/json-crdt-server/presence.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const runPresenceTests = (_setup: ApiTestSetup, params: {staticOnly?: tru
88
describe('presence', () => {
99
if (!params.staticOnly) {
1010
test('can subscribe and receive published presence entries', async () => {
11-
const {call, call$} = await setup();
11+
const {call, call$, stop} = await setup();
1212
const emits: any[] = [];
1313
call$('presence.listen', {room: 'my-room'}).subscribe((res) => {
1414
emits.push(res);
@@ -34,10 +34,11 @@ export const runPresenceTests = (_setup: ApiTestSetup, params: {staticOnly?: tru
3434
},
3535
],
3636
});
37+
stop();
3738
});
3839

3940
test('can receive an existing record when subscribing after it was created', async () => {
40-
const {call, call$} = await setup();
41+
const {call, call$, stop} = await setup();
4142
const emits: any[] = [];
4243
call$('presence.listen', {room: 'my-room'}).subscribe((res) => {
4344
emits.push(res);
@@ -68,10 +69,11 @@ export const runPresenceTests = (_setup: ApiTestSetup, params: {staticOnly?: tru
6869
},
6970
],
7071
});
72+
stop();
7173
});
7274

7375
test('can remove existing entries', async () => {
74-
const {call, call$} = await setup();
76+
const {call, call$, stop} = await setup();
7577
const emits: any[] = [];
7678
call$('presence.listen', {room: 'my-room'}).subscribe((res) => {
7779
emits.push(res);
@@ -92,10 +94,11 @@ export const runPresenceTests = (_setup: ApiTestSetup, params: {staticOnly?: tru
9294
});
9395
await tick(50);
9496
expect(emits2.length).toBe(0);
97+
stop();
9598
});
9699

97100
test('emits entry deletion messages', async () => {
98-
const {call, call$} = await setup();
101+
const {call, call$, stop} = await setup();
99102
await call('presence.update', {
100103
room: 'my-room',
101104
id: 'user-1',
@@ -115,6 +118,7 @@ export const runPresenceTests = (_setup: ApiTestSetup, params: {staticOnly?: tru
115118
validUntil: 0,
116119
data: expect.any(Object),
117120
});
121+
stop();
118122
});
119123
}
120124
});

src/__tests__/json-crdt-server/pubsub.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const runPubsubTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
77

88
describe('pubsub', () => {
99
test('throws error on invalid input', async () => {
10-
const {call} = await setup();
10+
const {call, stop} = await setup();
1111
try {
1212
await call('pubsub.publish', {
1313
channel2: 'INVALID KEY',
@@ -17,11 +17,12 @@ export const runPubsubTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
1717
} catch (err: any) {
1818
expect(err.meta.path).toStrictEqual(['channel2']);
1919
}
20+
stop();
2021
});
2122

2223
if (!params.staticOnly) {
2324
test('can subscribe and receive published messages', async () => {
24-
const {call, call$} = await setup();
25+
const {call, call$, stop} = await setup();
2526
const emits: any[] = [];
2627
const subscription = call$('pubsub.listen', {channel: 'my-channel'}).subscribe((res) => {
2728
emits.push(res.message);
@@ -33,10 +34,11 @@ export const runPubsubTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
3334
await until(() => emits.length === 1);
3435
expect(emits).toStrictEqual(['hello world']);
3536
subscription.unsubscribe();
37+
stop();
3638
});
3739

3840
test('does not receive messages after un-subscription', async () => {
39-
const {call, call$} = await setup();
41+
const {call, call$, stop} = await setup();
4042
const emits: any[] = [];
4143
const sub = call$('pubsub.listen', {channel: 'my-channel'}).subscribe((res) => {
4244
emits.push(res.message);
@@ -59,10 +61,11 @@ export const runPubsubTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
5961
await tick(50);
6062
expect(emits.indexOf('msg1') > -1).toBe(true);
6163
expect(emits.indexOf('msg2') > -1).toBe(true);
64+
stop();
6265
});
6366

6467
test('multiple multiple subscribers can subscribe to multiple channels', async () => {
65-
const {call, call$} = await setup();
68+
const {call, call$, stop} = await setup();
6669
const user1: any[] = [];
6770
const user2: any[] = [];
6871
const user3: any[] = [];
@@ -112,6 +115,7 @@ export const runPubsubTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
112115
expect(user3.indexOf('msg1') > -1).toBe(true);
113116
expect(user3.indexOf('msg2') > -1).toBe(true);
114117
expect(user3.indexOf('msg3') > -1).toBe(true);
118+
stop();
115119
});
116120
}
117121
});

src/__tests__/json-crdt-server/util.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,32 @@ export const runUtilTests = (_setup: ApiTestSetup, params: {staticOnly?: true} =
77
describe('util.*', () => {
88
describe('util.ping', () => {
99
test('returns pong', async () => {
10-
const {call} = await setup();
10+
const {call, stop} = await setup();
1111
const res = await call('util.ping', {});
1212
expect(res).toBe('pong');
13+
stop();
1314
});
1415
});
1516

1617
describe('util.echo', () => {
1718
test('returns strings', async () => {
18-
const {call} = await setup();
19+
const {call, stop} = await setup();
1920
const res = await call('util.echo', 'hello world');
2021
expect(res).toBe('hello world');
22+
stop();
2123
});
2224

2325
test('returns objects', async () => {
24-
const {call} = await setup();
26+
const {call, stop} = await setup();
2527
const res = await call('util.echo', {foo: 'bar'});
2628
expect(res).toStrictEqual({foo: 'bar'});
29+
stop();
2730
});
2831
});
2932

3033
describe('util.info', () => {
3134
test('returns stats object', async () => {
32-
const {call} = await setup();
35+
const {call, stop} = await setup();
3336
const res = await call('util.info', {});
3437
expect(res).toMatchObject({
3538
now: expect.any(Number),
@@ -49,6 +52,7 @@ export const runUtilTests = (_setup: ApiTestSetup, params: {staticOnly?: true} =
4952
},
5053
},
5154
});
55+
stop();
5256
});
5357
});
5458
});

0 commit comments

Comments
 (0)