admin管理员组

文章数量:1129102

I am using nestjs-pino in my NestJS project, and I'd like to test that the customProps I am assigning actually work to either use the value of a req header I send in or use a generated uuid.

import { v4 as uuidv4 } from 'uuid';
import { LoggerModule } from 'nestjs-pino';

@Module({
  imports: [
    LoggerModule.forRoot({
      pinoHttp: {
        customProps: (req) => ({
          requestId: req.headers['some-header'] ?? uuidv4(),
        }),
      },
    }),
    // Other module imports
  ],
  controllers: [ /* controller here */ ],
})
export class AppModule {}

I have a test file set up that tests the LoggerModule is of the correct type, but I'm not sure how to delve deeper into it, to test the actual usage?

import { Test, TestingModule } from '@nestjs/testing';
import { LoggerModule } from 'nestjs-pino';
import { AppModule } from './app.module';

describe('AppModule', () => {
  let appModule: TestingModule;

  beforeEach(async () => {
    appModule = await Test.createTestingModule({
      imports: [AppModule],
    })pile();
  });

  // This test passes
  it('should load LoggerModule', () => {
    const loggerModule = appModule.get(LoggerModule);
    expect(loggerModule).toBeInstanceOf(LoggerModule);
  });

  // Attempt to at least check that "forRoot" had been called - FAILS
  fit('should load LoggerModule with custom props', () => {
    const loggerModule = appModule.get(LoggerModule);
    expect(LoggerModule.forRoot).toHaveBeenCalled();
  });
});

本文标签: unit testingHow to test my pino customprops implementationStack Overflow