Comments (4)
@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.
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.
@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.
@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)
- About import {DocumentQuery} from 'mongoose' at mongo-sample
- Suggestion: convert testing-nestjs into monorepo with Lerna HOT 3
- [Question] How I mock another service method? HOT 8
- Suggestion: create tests for Prisma (alternative to TypeORM) HOT 7
- [FAILING TEST] Unit testing a NestJS Service that consumes a TypeORM Repository HOT 6
- [NEW TEST] Testing for interceptor failure HOT 3
- [NEW TEST] KnexJs Sample test HOT 1
- Multiple mocked dependencies with the same content HOT 5
- [NEW TEST] Bull HOT 4
- Prisma transactions and rollbacks now available HOT 2
- [NEW TEST] - (Contract Testing) - Incorporate NestJS Consumer Driven Contract testing with nestjs-pact HOT 6
- README refers to non-existing cat.integration.spec.ts HOT 3
- Test against Solidity smart contracts
- [NEW TEST] Tests for mikro-orm
- [NEW TEST] Sequelize transactions HOT 1
- Add code comment for documenting the tests. HOT 1
- Unable to commit the file in file-up-and-down-sample HOT 1
- Can't push to sidebranch for PR - getting permission denied HOT 1
- I can't seem to make the tests fails anywhere so far HOT 3
- npm install doesn't work
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from testing-nestjs.