Nestjs请求,测试中的拦截器

时间:2020-08-26 03:48:17

标签: testing request nestjs

我有RateUpInterceptor,并且此添加计数率为1

@Injectable()
export class RateUpInterceptor implements NestInterceptor {
  constructor() {}

  intercept(context: ExecutionContext, next: CallHandler<any>): Observable<any> | Promise<Observable<any>> {
    const http = context.switchToHttp();
    const req = http.getRequest<IRequestAugmented>();
    const res = http.getResponse<Response>();
    return next.handle().pipe(
      tap(async () => {
        /** COUNT PLUS 1 **/
        console.log('A');
      }),
      catchError((e) => {
        return throwError(e);
      }),
    );
  }
}

describe('RateUpInterceptor', () => {
  let app: INestApplication;
  let module: TestingModule;

  beforeAll(async () => {
    module = await Test.createTestingModule({
      imports: [UserModule],
      providers: [
        {
          provide: APP_INTERCEPTOR,
          useFactory: () => {
            return new RateUpInterceptor();
          },
        },
      ],
    }).compile();

    app = module.createNestApplication();
    await app.init();

  });
  
  it('test', async () => {
    request(app.getHttpServer())
      .get(`valid url`)
      .auth(token, { type: 'bearer' })
      .expect(200).end(async () => {
        console.log('B');      
      });
  });  
});

我根据要求通过e2e测试对该拦截器进行了测试。

我认为在全局拦截器“ console.log('B')”执行之后

但结果不同... 结果等一下

B
A

如何解决此订单?

0 个答案:

没有答案