From 9139ccfb930d6bac1edae808415728312680e42a Mon Sep 17 00:00:00 2001 From: Nathan Arseneau <10428205+nathanArseneau@users.noreply.github.com> Date: Sat, 3 Sep 2022 20:18:29 -0400 Subject: [PATCH 1/3] test(sample-02): added unit tests --- sample/02-gateways/jest.json | 14 +++++++ sample/02-gateways/package-lock.json | 25 ++++++++++-- sample/02-gateways/package.json | 18 +++++++++ .../src/events/events.gateway.spec.ts | 40 +++++++++++++++++++ 4 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 sample/02-gateways/jest.json create mode 100644 sample/02-gateways/src/events/events.gateway.spec.ts diff --git a/sample/02-gateways/jest.json b/sample/02-gateways/jest.json new file mode 100644 index 00000000000..591de12b6ec --- /dev/null +++ b/sample/02-gateways/jest.json @@ -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"] +} \ No newline at end of file diff --git a/sample/02-gateways/package-lock.json b/sample/02-gateways/package-lock.json index 321c7ce4589..cd4dbaa7075 100644 --- a/sample/02-gateways/package-lock.json +++ b/sample/02-gateways/package-lock.json @@ -27,6 +27,7 @@ "@nestjs/schematics": "9.0.1", "@nestjs/testing": "9.0.1", "@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", @@ -1947,6 +1948,16 @@ "@types/istanbul-lib-report": "*" } }, + "node_modules/@types/jest": { + "version": "28.1.4", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-28.1.4.tgz", + "integrity": "sha512-telv6G5N7zRJiLcI3Rs3o+ipZ28EnE+7EvF0pSrt2pZOMnAVI/f+6/LucDxOvcBcTeTL3JMF744BbVQAVBUQRA==", + "dev": true, + "dependencies": { + "jest-matcher-utils": "^28.0.0", + "pretty-format": "^28.0.0" + } + }, "node_modules/@types/json-schema": { "version": "7.0.11", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.11.tgz", @@ -10166,6 +10177,16 @@ "@types/istanbul-lib-report": "*" } }, + "@types/jest": { + "version": "28.1.4", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-28.1.4.tgz", + "integrity": "sha512-telv6G5N7zRJiLcI3Rs3o+ipZ28EnE+7EvF0pSrt2pZOMnAVI/f+6/LucDxOvcBcTeTL3JMF744BbVQAVBUQRA==", + "dev": true, + "requires": { + "jest-matcher-utils": "^28.0.0", + "pretty-format": "^28.0.0" + } + }, "@types/json-schema": { "version": "7.0.11", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.11.tgz", @@ -10577,9 +10598,7 @@ "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-2.1.1.tgz", "integrity": "sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA==", "dev": true, - "requires": { - "ajv": "^8.0.0" - } + "requires": {} }, "ansi-colors": { "version": "4.1.1", diff --git a/sample/02-gateways/package.json b/sample/02-gateways/package.json index 0317efb0435..219a3853df1 100644 --- a/sample/02-gateways/package.json +++ b/sample/02-gateways/package.json @@ -37,6 +37,7 @@ "@nestjs/schematics": "9.0.1", "@nestjs/testing": "9.0.1", "@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", @@ -54,5 +55,22 @@ "ts-node": "10.8.2", "tsconfig-paths": "4.0.0", "typescript": "4.7.4" + }, + "jest": { + "moduleFileExtensions": [ + "js", + "json", + "ts" + ], + "rootDir": "src", + "testRegex": ".*\\.spec\\.ts$", + "transform": { + "^.+\\.(t|j)s$": "ts-jest" + }, + "collectCoverageFrom": [ + "**/*.(t|j)s" + ], + "coverageDirectory": "../coverage", + "testEnvironment": "node" } } diff --git a/sample/02-gateways/src/events/events.gateway.spec.ts b/sample/02-gateways/src/events/events.gateway.spec.ts new file mode 100644 index 00000000000..060e631eaaf --- /dev/null +++ b/sample/02-gateways/src/events/events.gateway.spec.ts @@ -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); + }); + + it('should be defined', () => { + expect(gateway).toBeDefined(); + }); + + describe('findAll', () => { + it('should return an array of items', 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); + }); + }); +}); From 938005d8e1211eaa42133a0a8a32890bf6670d14 Mon Sep 17 00:00:00 2001 From: Nathan Arseneau <10428205+nathanArseneau@users.noreply.github.com> Date: Sat, 3 Sep 2022 21:50:05 -0400 Subject: [PATCH 2/3] test(sample-02): added e2e tests --- .../e2e/events-gateway/gateway.e2e-spec.ts | 53 +++++++++++++++++++ sample/02-gateways/e2e/jest-e2e.json | 14 +++++ sample/02-gateways/package.json | 4 +- .../src/events/events.gateway.spec.ts | 2 +- 4 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 sample/02-gateways/e2e/events-gateway/gateway.e2e-spec.ts create mode 100644 sample/02-gateways/e2e/jest-e2e.json diff --git a/sample/02-gateways/e2e/events-gateway/gateway.e2e-spec.ts b/sample/02-gateways/e2e/events-gateway/gateway.e2e-spec.ts new file mode 100644 index 00000000000..26b9f42ed4b --- /dev/null +++ b/sample/02-gateways/e2e/events-gateway/gateway.e2e-spec.ts @@ -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(); + }); +}); diff --git a/sample/02-gateways/e2e/jest-e2e.json b/sample/02-gateways/e2e/jest-e2e.json new file mode 100644 index 00000000000..72eb5c3c0a3 --- /dev/null +++ b/sample/02-gateways/e2e/jest-e2e.json @@ -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"] +} \ No newline at end of file diff --git a/sample/02-gateways/package.json b/sample/02-gateways/package.json index 219a3853df1..f3f7deb7e85 100644 --- a/sample/02-gateways/package.json +++ b/sample/02-gateways/package.json @@ -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.0.1", @@ -37,7 +37,7 @@ "@nestjs/schematics": "9.0.1", "@nestjs/testing": "9.0.1", "@types/express": "4.17.13", - "@types/jest": "^28.1.4", + "@types/jest": "28.1.4", "@types/node": "18.0.3", "@types/supertest": "2.0.12", "@types/ws": "8.5.3", diff --git a/sample/02-gateways/src/events/events.gateway.spec.ts b/sample/02-gateways/src/events/events.gateway.spec.ts index 060e631eaaf..e40de3debf0 100644 --- a/sample/02-gateways/src/events/events.gateway.spec.ts +++ b/sample/02-gateways/src/events/events.gateway.spec.ts @@ -18,7 +18,7 @@ describe('EventsGateway', () => { }); describe('findAll', () => { - it('should return an array of items', done => { + it('should return 3 numbers', done => { gateway .findAll({}) .pipe(reduce((acc, item) => [...acc, item], [])) From 53cd547004d78f9b825cb544bb5c65d5cb0585a0 Mon Sep 17 00:00:00 2001 From: Nathan Arseneau <10428205+nathanArseneau@users.noreply.github.com> Date: Sat, 3 Sep 2022 21:55:38 -0400 Subject: [PATCH 3/3] test(sample-02): remove version increment in package-lock.json --- sample/02-gateways/package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sample/02-gateways/package-lock.json b/sample/02-gateways/package-lock.json index cd4dbaa7075..29e0f76b4bb 100644 --- a/sample/02-gateways/package-lock.json +++ b/sample/02-gateways/package-lock.json @@ -27,7 +27,7 @@ "@nestjs/schematics": "9.0.1", "@nestjs/testing": "9.0.1", "@types/express": "4.17.13", - "@types/jest": "^28.1.4", + "@types/jest": "28.1.4", "@types/node": "18.0.3", "@types/supertest": "2.0.12", "@types/ws": "8.5.3",