nestjs / docs.nestjs.com

The official documentation https://docs.nestjs.com 📕
MIT License
1.2k stars 1.74k forks source link

Testing documentation #97

Open kamilmysliwiec opened 6 years ago

kamilmysliwiec commented 6 years ago

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request (new chapter/page)
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

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


For Tooling issues:
- Node version: XX  
- Platform:  

Others:

khpatel4991 commented 5 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

ifl0w commented 5 years ago

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`);
agomezlyte commented 4 years ago

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.

yuedai0531 commented 4 years ago

Any updates on this?

maitrungduc1410 commented 4 years ago

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

coaxial commented 3 years ago

An example of how to unit test guards would be nice, in particular the canActivate method which requires an ExecutionContext.

Benny739 commented 3 years ago

Anybody knows how to test SSE? I'm getting req.socket.setKeepAlive is not a function

orange1337 commented 2 years ago

Any news about socket unit testing ??? Still can't find a proper solution 🤷‍♂️