Skip to content

Instantly share code, notes, and snippets.

@paztek
Created August 23, 2020 10:31
Show Gist options
  • Save paztek/af3384bc9a2e7d28e6ce5de91c791269 to your computer and use it in GitHub Desktop.
Save paztek/af3384bc9a2e7d28e6ce5de91c791269 to your computer and use it in GitHub Desktop.
nestjs-authentication-example-3
import { HttpModule, Module } from '@nestjs/common';
import { AuthenticationGuard } from './authentication.guard';
import { AuthenticationService } from './authentication.service';
import { AUTHENTICATION_STRATEGY_TOKEN } from './authentication.strategy';
import { KeycloakAuthenticationStrategy } from './strategy/keycloak.strategy';
@Module({
imports: [
HttpModule,
],
providers: [
AuthenticationGuard,
AuthenticationService,
{
provide: AUTHENTICATION_STRATEGY_TOKEN,
useClass: KeycloakAuthenticationStrategy, // <-- No more test-related code
},
],
exports: [
AuthenticationService,
],
})
export class AuthenticationModule {}
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { AppModule } from './app.module';
import { INestApplication } from '@nestjs/common';
import { createToken } from '../test/helpers/authentication/create-token';
import { AUTHENTICATION_STRATEGY_TOKEN } from './authentication/authentication.strategy';
import { FakeAuthenticationStrategy } from './authentication/strategy/fake.strategy';
describe('Hello E2E', () => {
let app: INestApplication;
let server: any;
const token = createToken();
beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [
AppModule
],
})
// This line is now required is every E2E test
.overrideProvider(AUTHENTICATION_STRATEGY_TOKEN).useClass(FakeAuthenticationStrategy)
.compile();
app = module.createNestApplication();
await app.init();
server = app.getHttpServer();
});
...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment