Last active
May 24, 2021 18:28
-
-
Save smitpatelx/1295f03d3e59642437d7267881ab3cf2 to your computer and use it in GitHub Desktop.
Nest.js end-to-end test for JWT auth
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
import { INestApplication } from '@nestjs/common'; | |
import { Test } from '@nestjs/testing'; | |
import * as request from 'supertest'; | |
import { AppModule } from '../src/app.module'; | |
import { AppService } from '../src/app.service'; | |
import { UserModule } from '../src/users/users.module'; | |
import { UsersService } from '../src/users/users.service'; | |
import { TypeOrmModule } from '@nestjs/typeorm'; | |
import { UserEntity } from '../src/models/users/user.entity'; | |
describe('E2E JWT Sample', () => { | |
let app: INestApplication; | |
beforeAll(async () => { | |
const modRef = await Test.createTestingModule({ | |
imports: [AppModule, UserModule, TypeOrmModule.forFeature([UserEntity])], | |
providers: [AppService, UsersService], | |
}).compile(); | |
app = modRef.createNestApplication(); | |
await app.init(); | |
}); | |
it('should get a JWT then successfully make a call', async () => { | |
const loginReq = await request(app.getHttpServer()) | |
.post('/auth/login') | |
.send({ username: '[email protected]', password: 'utjjdh' }) | |
.expect(201); | |
const token = loginReq.body.access_token; | |
return await request(app.getHttpServer()) | |
.get('/user') | |
.set('Authorization', 'Bearer ' + token) | |
.expect(200); | |
}); | |
afterAll(async () => { | |
await app.close(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment