Coder Social home page Coder Social logo

[NEW TEST] WebSockets about testing-nestjs HOT 4 CLOSED

jmcdo29 avatar jmcdo29 commented on May 22, 2024
[NEW TEST] WebSockets

from testing-nestjs.

Comments (4)

ShGKme avatar ShGKme commented on May 22, 2024 2

@jmcdo29 Check out this repository:
https://github.com/ShGKme/nestjs-ws-e2e

Or this repl (run it in browser):
https://repl.it/github/ShGKme/nestjs-ws-e2e

from testing-nestjs.

ShGKme avatar ShGKme commented on May 22, 2024

Tere is an issue with e2e testing.
Jest did not exit one second after the test run has completed.

To be honest, I can't understand, why this:
https://github.com/jmcdo29/testing-nestjs/blob/master/apps/websocket-sample/test/app.e2e-spec.ts#L23
will connect exacly to the testing app http server... How it works.

And what about adding about more "websocket" test. In this test event just return result like usual http endpoint. It'd be more interesting to return WsResponse (emit another event as an answer to subscribed event).

from testing-nestjs.

jmcdo29 avatar jmcdo29 commented on May 22, 2024

@ShGKme I'll be 100% honest, I'm not completely sure how it works myself. I believe it has something to do with socket.io scanning already made connections via socket.io, and creating a client for it. If you have an idea for better tests I'd be happy to accept a PR for them. Websockets are still a bit out of my realm of knowledge so I went with a simple test for now. I'll re-open the issue for now as a reminder that better tests are needed. Maybe even one for non-socket.io clients

from testing-nestjs.

ShGKme avatar ShGKme commented on May 22, 2024

@jmcdo29 io.connect(); doesn't really work.
It's very funny. Let's look to the test:

// It's an async test, so, it except Promise to be resolved.
it('should call message', async () => {
  // Connect to socket server (what server? does it know an address?)
  const socket = io.connect();
  // Does we connect successfully? connect never throw error itself...

  //Ok, let's emit a message
  socket.emit('message', { name: 'Test' }, (data) => {
    // (!)When(!) it'll receive an acknowledgement, we will check, that it's correct
    expect(data).toBe('Hello, Test!');
  });
  // (!) If we won't receive any acknowledgement from message event, we will check nothing :D

  // By the way, disconnect doesn't return Promise.
  return socket.disconnect();
});

The test will finish earler, than we will receive anything and won't check for any errors.
At the end, we have passed test with a Jest warning:

Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests.

More fun? This will pass :D

it('should call message', async () => {
  const socket = io.connect('http://THE_URL_THAT_DOES_NOT_EXISTS:67000');
  socket.emit('NO-EVENT', { name: 'Test' }, (data) => {
    expect(data).toBe('LITERALLY ANY VALUE');
    throw new Error('JUST GO THROW AN ERROR HERE');
  });
  return socket.disconnect();
});

I will make my example soon today.

from testing-nestjs.

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.