/
fastify.spec.ts
86 lines (71 loc) 路 2.22 KB
/
fastify.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { Test } from '@nestjs/testing';
import { expect } from 'chai';
import * as EventSource from 'eventsource';
import { AppModule } from '../src/app.module';
describe('Sse (Fastify Application)', () => {
let app: NestFastifyApplication;
let eventSource: EventSource;
describe('without forceCloseConnections', () => {
beforeEach(async () => {
const moduleFixture = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication<NestFastifyApplication>(
new FastifyAdapter(),
);
await app.listen(3000);
const url = await app.getUrl();
eventSource = new EventSource(url + '/sse', {
headers: { connection: 'keep-alive' },
});
});
// The order of actions is very important here. When not using `forceCloseConnections`,
// the SSe eventsource should close the connections in order to signal the server that
// the keep-alive connection can be ended.
afterEach(async () => {
eventSource.close();
await app.close();
});
it('receives events from server', done => {
eventSource.addEventListener('message', event => {
expect(JSON.parse(event.data)).to.eql({
hello: 'world',
});
done();
});
});
});
describe('with forceCloseConnections', () => {
beforeEach(async () => {
const moduleFixture = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication<NestFastifyApplication>(
new FastifyAdapter({
forceCloseConnections: true,
}),
);
await app.listen(3000);
const url = await app.getUrl();
eventSource = new EventSource(url + '/sse', {
headers: { connection: 'keep-alive' },
});
});
afterEach(async () => {
await app.close();
eventSource.close();
});
it('receives events from server', done => {
eventSource.addEventListener('message', event => {
expect(JSON.parse(event.data)).to.eql({
hello: 'world',
});
done();
});
});
});
});