Skip to content

Instantly share code, notes, and snippets.

@smitpatelx
Last active May 24, 2021 18:28
Show Gist options
  • Save smitpatelx/1295f03d3e59642437d7267881ab3cf2 to your computer and use it in GitHub Desktop.
Save smitpatelx/1295f03d3e59642437d7267881ab3cf2 to your computer and use it in GitHub Desktop.
Nest.js end-to-end test for JWT auth
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: 'asdasd@dsdas.oore', 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