-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10228 from nathanArseneau/test/sample-02-gateways
test(sample-02): added unit tests
- Loading branch information
Showing
6 changed files
with
162 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |