Created
October 14, 2021 13:20
-
-
Save roger-dev-br/b70003e763bc966166d5c7def9365809 to your computer and use it in GitHub Desktop.
NestJS e2e test
This file contains hidden or 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
import { Test, TestingModule } from '@nestjs/testing'; | |
import { INestApplication } from '@nestjs/common'; | |
import * as request from 'supertest'; | |
import { AppModule } from './../src/app.module'; | |
import { UserService } from '@src/modules/registrations/features/users/user.service'; | |
describe('Users (e2e)', () => { | |
let app: INestApplication; | |
let token: string; | |
const luid = 'ez-volt'; | |
let userService: UserService; | |
beforeAll(async () => { | |
const moduleFixture: TestingModule = await Test.createTestingModule({ | |
imports: [AppModule], | |
}).compile(); | |
app = moduleFixture.createNestApplication(); | |
await app.init(); | |
userService = moduleFixture.get<UserService>(UserService); | |
const data = await request(app.getHttpServer()).post('/auth').send({ | |
email: '[email protected]', | |
password: '101010', | |
}); | |
token = data.body.data.token; | |
const user = await userService.getByEmail('[email protected]'); | |
if (user) { | |
await userService.delete(user.id, luid); | |
} | |
}); | |
beforeEach(async () => { | |
const moduleFixture: TestingModule = await Test.createTestingModule({ | |
imports: [AppModule], | |
}).compile(); | |
app = moduleFixture.createNestApplication(); | |
await app.init(); | |
}); | |
afterEach(async () => { | |
await app?.close(); | |
}); | |
it('/users (POST) - Should create a new user', async () => { | |
return request(app.getHttpServer()) | |
.post('/users') | |
.set('Authorization', `Bearer ${token}`) | |
.send({ | |
name: 'Test User', | |
email: '[email protected]', | |
password: '101010', | |
phone: '', | |
level: 1, | |
}) | |
.expect(201) | |
.then((data) => { | |
expect(data.body.success).toBe(true); | |
expect(data.body.message).toBe('OK'); | |
expect(data.body.data.name).toBe('Test User'); | |
return data; | |
}) | |
.catch((error) => console.log(error)); | |
}); | |
it('/users (POST) - Should show error user_already_exists', () => { | |
return request(app.getHttpServer()) | |
.post('/users') | |
.set('Authorization', `Bearer ${token}`) | |
.send({ | |
name: 'Test User', | |
email: '[email protected]', | |
password: 'test', | |
phone: '', | |
level: 1, | |
}) | |
.expect(400) | |
.then((data) => { | |
expect(data.body.success).toBe(false); | |
expect(data.body.message).toBe('E-mail já cadastrado'); | |
expect(data.body.msgCode).toBe('USER_ALREADY_EXISTS'); | |
return data; | |
}); | |
}); | |
it('/users/id (PUT) - Should update user data', async () => { | |
const user = await userService.getByEmail('[email protected]'); | |
return request(app.getHttpServer()) | |
.put(`/users/${user.id}`) | |
.set('Authorization', `Bearer ${token}`) | |
.send({ | |
name: 'Test User', | |
email: '[email protected]', | |
password: '123456', | |
phone: '', | |
level: 1, | |
status: 0, | |
}) | |
.expect(200) | |
.then((data) => { | |
expect(data.body.success).toBe(true); | |
expect(data.body.msgCode).toBe('OK'); | |
expect(data.body.data.level).toBe(1); | |
expect(data.body.data.status).toBe(0); | |
return data; | |
}); | |
}); | |
it('/users/id (PUT) - Should return not found when update an invalid user ID', async () => { | |
return request(app.getHttpServer()) | |
.put(`/users/b2201fac-b764-4cc8-84b6-e8bf458671c1`) | |
.set('Authorization', `Bearer ${token}`) | |
.send({ | |
name: 'Test User', | |
email: '[email protected]', | |
password: null, | |
phone: '', | |
level: 1, | |
status: 0, | |
}) | |
.expect(404) | |
.then((data) => { | |
expect(data.body.success).toBe(false); | |
expect(data.body.msgCode).toBe('USER_NOT_FOUND'); | |
expect(data.body.message).toBe('Usuário não encontrado'); | |
return data; | |
}); | |
}); | |
describe('get registred users', () => { | |
it('should return a list of users', async () => { | |
return request(app.getHttpServer()) | |
.get(`/users`) | |
.set('Authorization', `Bearer ${token}`) | |
.expect(200) | |
.then((data) => { | |
expect(data.body.success).toBe(true); | |
expect(data.body.msgCode).toBe('OK'); | |
expect(data.body.data.length).toBeGreaterThan(0); | |
return data; | |
}); | |
return; | |
}); | |
it('should return a user by id', async () => { | |
return request(app.getHttpServer()) | |
.get(`/users/roger`) | |
.set('Authorization', `Bearer ${token}`) | |
.expect(200) | |
.then((data) => { | |
expect(data.body.success).toBe(true); | |
expect(data.body.msgCode).toBe('OK'); | |
expect(data.body.data.email).toBe('[email protected]'); | |
expect(data.body.data.name).toBe('Roger Medeiros'); | |
return data; | |
}); | |
return; | |
}); | |
}); | |
it('should return a Not Found error', async () => { | |
return request(app.getHttpServer()) | |
.get(`/users/b2203fac-b764-4cc8-84b6-e8bf458671c1`) | |
.set('Authorization', `Bearer ${token}`) | |
.expect(404) | |
.then((data) => { | |
expect(data.body.success).toBe(false); | |
expect(data.body.msgCode).toBe('USER_NOT_FOUND'); | |
expect(data.body.data).toBe(null); | |
return data; | |
}); | |
return; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment