From b3370b1d74b497accad61cc363690ed54019a469 Mon Sep 17 00:00:00 2001 From: Jay McDoniel Date: Mon, 14 Feb 2022 09:23:39 -0800 Subject: [PATCH] fix: add optional decorator for property based injection --- jest.config.js | 5 +++-- lib/auth.guard.ts | 1 + test/{ => common}/app.controller.ts | 2 +- test/{ => common}/app.e2e-spec.ts | 9 +++++++-- test/{ => common}/app.service.ts | 0 test/{ => common}/jwt.strategy.ts | 2 +- test/{ => common}/local.strategy.ts | 2 +- test/{ => with-register}/app.module.ts | 10 +++++----- test/without-register/app.module.ts | 19 +++++++++++++++++++ 9 files changed, 38 insertions(+), 12 deletions(-) rename test/{ => common}/app.controller.ts (94%) rename test/{ => common}/app.e2e-spec.ts (79%) rename test/{ => common}/app.service.ts (100%) rename test/{ => common}/jwt.strategy.ts (89%) rename test/{ => common}/local.strategy.ts (89%) rename test/{ => with-register}/app.module.ts (54%) create mode 100644 test/without-register/app.module.ts diff --git a/jest.config.js b/jest.config.js index 09143778..55340dcc 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,10 +1,11 @@ module.exports = { moduleFileExtensions: ['js', 'json', 'ts'], rootDir: '.', - testMatch: ['/test/*.e2e-spec.ts'], + testMatch: ['/test/**/*.e2e-spec.ts'], transform: { '^.+\\.ts$': 'ts-jest' }, testEnvironment: 'node', - collectCoverage: true + collectCoverage: true, + collectCoverageFrom: ['lib/**/*.ts'] }; diff --git a/lib/auth.guard.ts b/lib/auth.guard.ts index c714852f..b6165df9 100644 --- a/lib/auth.guard.ts +++ b/lib/auth.guard.ts @@ -31,6 +31,7 @@ const NO_STRATEGY_ERROR = `In order to use "defaultStrategy", please, ensure to function createAuthGuard(type?: string | string[]): Type { class MixinAuthGuard implements CanActivate { + @Optional() @Inject(AuthModuleOptions) protected options: AuthModuleOptions; constructor(@Optional() options?: AuthModuleOptions) { diff --git a/test/app.controller.ts b/test/common/app.controller.ts similarity index 94% rename from test/app.controller.ts rename to test/common/app.controller.ts index d827ebaf..0ce9b5c1 100644 --- a/test/app.controller.ts +++ b/test/common/app.controller.ts @@ -1,5 +1,5 @@ import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common'; -import { AuthGuard } from '../lib'; +import { AuthGuard } from '../../lib'; import { AppService } from './app.service'; diff --git a/test/app.e2e-spec.ts b/test/common/app.e2e-spec.ts similarity index 79% rename from test/app.e2e-spec.ts rename to test/common/app.e2e-spec.ts index 65cb18ad..7dd2e5f9 100644 --- a/test/app.e2e-spec.ts +++ b/test/common/app.e2e-spec.ts @@ -1,9 +1,14 @@ import { INestApplication } from '@nestjs/common'; import { Test } from '@nestjs/testing'; import { spec, request } from 'pactum'; -import { AppModule } from './app.module'; +import { AppModule as WithRegisterModule } from '../with-register/app.module'; +import { AppModule as WithoutRegisterModule } from '../without-register/app.module'; -describe('Passport Module', () => { +describe.each` + AppModule | RegisterUse + ${WithRegisterModule} | ${'with'} + ${WithoutRegisterModule} | ${'without'} +`('Passport Module $RegisterUse register()', ({ AppModule }) => { let app: INestApplication; beforeAll(async () => { diff --git a/test/app.service.ts b/test/common/app.service.ts similarity index 100% rename from test/app.service.ts rename to test/common/app.service.ts diff --git a/test/jwt.strategy.ts b/test/common/jwt.strategy.ts similarity index 89% rename from test/jwt.strategy.ts rename to test/common/jwt.strategy.ts index 42767f39..7eeb4f68 100644 --- a/test/jwt.strategy.ts +++ b/test/common/jwt.strategy.ts @@ -1,6 +1,6 @@ import { Injectable } from '@nestjs/common'; import { ExtractJwt, Strategy } from 'passport-jwt'; -import { PassportStrategy } from '../lib'; +import { PassportStrategy } from '../../lib'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { diff --git a/test/local.strategy.ts b/test/common/local.strategy.ts similarity index 89% rename from test/local.strategy.ts rename to test/common/local.strategy.ts index 0d61a5ad..917e33b4 100644 --- a/test/local.strategy.ts +++ b/test/common/local.strategy.ts @@ -1,6 +1,6 @@ import { Injectable } from '@nestjs/common'; import { Strategy } from 'passport-local'; -import { PassportStrategy } from '../lib'; +import { PassportStrategy } from '../../lib'; import { AppService } from './app.service'; @Injectable() diff --git a/test/app.module.ts b/test/with-register/app.module.ts similarity index 54% rename from test/app.module.ts rename to test/with-register/app.module.ts index a88a2776..011db27b 100644 --- a/test/app.module.ts +++ b/test/with-register/app.module.ts @@ -1,10 +1,10 @@ import { Module } from '@nestjs/common'; import { JwtModule } from '@nestjs/jwt'; -import { PassportModule } from '../lib'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; -import { JwtStrategy } from './jwt.strategy'; -import { LocalStrategy } from './local.strategy'; +import { PassportModule } from '../../lib'; +import { AppController } from '../common/app.controller'; +import { AppService } from '../common/app.service'; +import { JwtStrategy } from '../common/jwt.strategy'; +import { LocalStrategy } from '../common/local.strategy'; @Module({ controllers: [AppController], diff --git a/test/without-register/app.module.ts b/test/without-register/app.module.ts new file mode 100644 index 00000000..3ddc5b2c --- /dev/null +++ b/test/without-register/app.module.ts @@ -0,0 +1,19 @@ +import { Module } from '@nestjs/common'; +import { JwtModule } from '@nestjs/jwt'; +import { PassportModule } from '../../lib'; +import { AppController } from '../common/app.controller'; +import { AppService } from '../common/app.service'; +import { JwtStrategy } from '../common/jwt.strategy'; +import { LocalStrategy } from '../common/local.strategy'; + +@Module({ + controllers: [AppController], + imports: [ + JwtModule.register({ + secret: 's3cr3t' + }), + PassportModule + ], + providers: [AppService, LocalStrategy, JwtStrategy] +}) +export class AppModule {}