nest: E2E tests broken when registering websockets

TL;DR: The issue was due to the order of the import statements in the test file (@nestjs/* stuff must be imported first) 😱

I’m submitting a…


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

Current behavior

Following the E2E Testing documentation, it works perfectly well to test controllers.

But as soon as you register a websocket (using providers as describe in documentation), and even if no controller is registered, we end up with the following error:

TypeError: Cannot read property 'create' of null

      23 | 
      24 | 		app = moduleFixture.createNestApplication();
    > 25 | 		await app.init();
         | 		          ^
      26 | 	});
      27 | 
      28 | 	afterAll(async () => {

      at SocketServerProvider.createSocketServer (node_modules/@nestjs/websockets/socket-server-provider.js:28:34)
      at SocketServerProvider.scanForSocketServer (node_modules/@nestjs/websockets/socket-server-provider.js:23:20)
      at WebSocketsController.subscribeObservableServer (node_modules/@nestjs/websockets/web-sockets-controller.js:35:60)
      at WebSocketsController.hookGatewayIntoServer (node_modules/@nestjs/websockets/web-sockets-controller.js:27:14)
      at SocketModule.hookGatewayIntoServer (node_modules/@nestjs/websockets/socket-module.js:40:35)
      at components.forEach.wrapper (node_modules/@nestjs/websockets/socket-module.js:29:44)
          at Map.forEach (<anonymous>)
      at SocketModule.hookGatewaysIntoServers (node_modules/@nestjs/websockets/socket-module.js:29:20)
      at modules.forEach (node_modules/@nestjs/websockets/socket-module.js:26:62)
          at Map.forEach (<anonymous>)
      at SocketModule.register (node_modules/@nestjs/websockets/socket-module.js:26:17)
      at NestApplication.registerModules (node_modules/@nestjs/core/nest-application.js:77:31)
      at NestApplication.init (node_modules/@nestjs/core/nest-application.js:87:20)
      at Object.beforeAll (tests/e2e/something.e2e-spec.ts:25:13)

Expected behavior

E2E testing of the whole application should work as expected even with websockets registered. Moreover, we should be able to setup E2E testing for websockets just like we do for controllers.

Minimal reproduction of the problem with instructions

https://github.com/Oliboy50/reproduce-issue-nestjs-websockets-test-e2e

What is the motivation / use case for changing the behavior?

I don’t want to have to “mock” my Websocket providers in e2e tests.

Environment


Nest version: 5.2.2 ~ 5.3.9

 
For Tooling issues:
- Node version: 10  
- Platform:  Mac and Linux

Others:

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Lol. 😅Glad it works now! 😃