Giter VIP home page Giter VIP logo

Comments (8)

ifl0w avatar ifl0w commented on May 5, 2024 10

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`);

from docs.nestjs.com.

orange1337 avatar orange1337 commented on May 5, 2024 6

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

from docs.nestjs.com.

khpatel4991 avatar khpatel4991 commented on May 5, 2024 4

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

from docs.nestjs.com.

maitrungduc1410 avatar maitrungduc1410 commented on May 5, 2024 4

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

from docs.nestjs.com.

Benny739 avatar Benny739 commented on May 5, 2024 1

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

from docs.nestjs.com.

agomezlyte avatar agomezlyte commented on May 5, 2024

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.

from docs.nestjs.com.

yuedai0531 avatar yuedai0531 commented on May 5, 2024

Any updates on this?

from docs.nestjs.com.

coaxial avatar coaxial commented on May 5, 2024

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

from docs.nestjs.com.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.