Created
August 23, 2020 10:31
-
-
Save paztek/af3384bc9a2e7d28e6ce5de91c791269 to your computer and use it in GitHub Desktop.
nestjs-authentication-example-3
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 { 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 {} |
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 { 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