-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
}); | ||
}); | ||
}); | ||
}); |