mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
test(sample-02): added unit tests
This commit is contained in:
14
sample/02-gateways/jest.json
Normal file
14
sample/02-gateways/jest.json
Normal file
@@ -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
sample/02-gateways/package-lock.json
generated
25
sample/02-gateways/package-lock.json
generated
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
40
sample/02-gateways/src/events/events.gateway.spec.ts
Normal file
40
sample/02-gateways/src/events/events.gateway.spec.ts
Normal file
@@ -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 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);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user