Open kamilmysliwiec opened 6 years ago
Any progress? With gateway example provided, I added EventsGateway in my e2e or controller test provider and when I emit a message from my service, it says websocket server is null
Based on nestjs/nest#1368, I was able to connect to the io server in my e2e tests with the following code. It allows testing a WebSocket implementation with the socket.io-client.
const address = app.getHttpServer().listen().address();
const baseAddress = `http://[${address.address}]:${address.port}`;
const client = io.connect(`${baseAddress}/some-namespace`);
I'm having similar issues. In my test I do can connect to the server but the socket.id on server and client sides is undefined, therefore I can't emit any message from the client or the server.
Any updates on this?
Solution of @ifl0w works perfectly. Here's my test case:
import * as WebSocket from 'ws'
beforeAll(async () => {
const moduleFixture = await Test.createTestingModule({
imports: [
SocketModule,
],
})
.compile()
app = moduleFixture.createNestApplication()
app.useWebSocketAdapter(new WsAdapter(app))
await app.init()
})
it('should connect successfully', (done) => {
const address = app.getHttpServer().listen().address()
const baseAddress = `http://[${address.address}]:${address.port}`
const socket = new WebSocket(baseAddress)
socket.on('open', () => {
console.log('I am connected! YEAAAP')
done()
})
socket.on('close', (code, reason) => {
done({ code, reason })
})
socket.on ('error', (error) => {
done(error)
})
})
But I observe there's a problem if I have more than 1 test case inside same describe
block, it won't work, I try debug using beforeAll, beforeEach, afterAll, afterEach
and it doesn't help. Then I need to separate each test case to be run inside 1 separate describe
block
An example of how to unit test guards would be nice, in particular the canActivate
method which requires an ExecutionContext
.
Anybody knows how to test SSE? I'm getting req.socket.setKeepAlive is not a function
Any news about socket unit testing ??? Still can't find a proper solution 🤷♂️
I'm submitting a...
Current behavior
Expected behavior
As a user, I would like to see higher test coverage of the provided examples (tests as a first class citizen).
Minimal reproduction of the problem with instructions
What is the motivation / use case for changing the behavior?
Environment