Skip to content

Commit

Permalink
Merge pull request #10228 from nathanArseneau/test/sample-02-gateways
Browse files Browse the repository at this point in the history
test(sample-02): added unit tests
  • Loading branch information
kamilmysliwiec authored Apr 5, 2023
2 parents c885a89 + 9b6b00f commit 402a389
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 4 deletions.
53 changes: 53 additions & 0 deletions sample/02-gateways/e2e/events-gateway/gateway.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { AppModule } from '../../src/app.module';
import { io, Socket } from 'socket.io-client';

describe('EventsGateway', () => {
let app: INestApplication;
let socket: Socket;

beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
}).compile();

app = moduleRef.createNestApplication();
await app.listen(3000);
});

beforeEach(() => {
socket = io('http://localhost:3000');
socket.connect();
});

describe('findAll', () => {
it('should receive 3 numbers', done => {
let eventCount = 1;
socket.emit('events', { test: 'test' });
socket.on('events', data => {
expect(data).toBe(eventCount);
if (++eventCount > 3) {
done();
}
});
});
});

describe('identity', () => {
it('should return the same number has what was sent', done => {
socket.emit('identity', 0, response => {
expect(response).toBe(0);
done();
});
});
});

afterEach(() => {
socket.disconnect();
});

afterAll(async () => {
await app.close();
});
});
14 changes: 14 additions & 0 deletions sample/02-gateways/e2e/jest-e2e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "/e2e/.*\\.(e2e-test|e2e-spec).(ts|tsx|js)$",
"collectCoverageFrom" : ["src/**/*.{js,jsx,tsx,ts}", "!**/node_modules/**", "!**/vendor/**"],
"coverageReporters": ["json", "lcov"]
}
14 changes: 14 additions & 0 deletions sample/02-gateways/jest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "/src/.*\\.(test|spec).(ts|tsx|js)$",
"collectCoverageFrom" : ["src/**/*.{js,jsx,tsx,ts}", "!**/node_modules/**", "!**/vendor/**"],
"coverageReporters": ["json", "lcov"]
}
25 changes: 22 additions & 3 deletions sample/02-gateways/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion sample/02-gateways/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "echo 'No e2e tests implemented yet.'"
"test:e2e": "jest --config ./e2e/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "9.3.12",
Expand All @@ -37,6 +37,7 @@
"@nestjs/schematics": "9.0.4",
"@nestjs/testing": "9.3.12",
"@types/express": "4.17.13",
"@types/jest": "28.1.4",
"@types/node": "18.0.3",
"@types/supertest": "2.0.12",
"@types/ws": "8.5.3",
Expand All @@ -54,5 +55,22 @@
"ts-node": "10.8.2",
"tsconfig-paths": "4.0.0",
"typescript": "4.9.5"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
40 changes: 40 additions & 0 deletions sample/02-gateways/src/events/events.gateway.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Test, TestingModule } from '@nestjs/testing';
import { reduce } from 'rxjs/operators';
import { EventsGateway } from './events.gateway';

describe('EventsGateway', () => {
let gateway: EventsGateway;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [EventsGateway],
}).compile();

gateway = module.get<EventsGateway>(EventsGateway);
});

it('should be defined', () => {
expect(gateway).toBeDefined();
});

describe('findAll', () => {
it('should return 3 numbers', done => {
gateway
.findAll({})
.pipe(reduce((acc, item) => [...acc, item], []))
.subscribe(results => {
expect(results.length).toBe(3);
results.forEach((result, index) =>
expect(result.data).toBe(index + 1),
);
done();
});
});
});

describe('identity', () => {
it('should return the same number has what was sent', async () => {
await expect(gateway.identity(1)).resolves.toBe(1);
});
});
});

0 comments on commit 402a389

Please sign in to comment.