momi
momi

Reputation: 449

nestjs e2e testing no logger output

I am using default built-in logger in NestJS form simple logging during development. But when I run e2e tests there is no logger output on console. A really need this, to see what is happening when test fails. Snippet from package.json:

"test:e2e": "jest --config ./test/jest-e2e.json"

And jest-e2e.json:

{
  "moduleFileExtensions": ["js", "json", "ts"],
  "rootDir": ".",
  "testEnvironment": "node",
  "testRegex": ".e2e-spec.ts$",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  },
  "verbose": true
}

Upvotes: 8

Views: 4634

Answers (3)

sezanzeb
sezanzeb

Reputation: 1139

It is possible to overwrite the logger on the chain after createTestingModule

const module_: TestingModule = await Test.createTestingModule({
    ...
})
    .setLogger(new Logger())
    .compile();

which is luckily a bit shorter than the method with app.useLogger.

This feature was added in https://github.com/nestjs/nest/pull/6071

As far as I can tell you'll have to do this for every single test setup.

I personally think this is ridiculous. Of course I need my logs when fixing my tests, including debug logs, and I want them without having to add some extra code

Upvotes: 12

momi
momi

Reputation: 449

I created a my logger that extends ConsoleLogger and nothing changed. But I figured out that I need to tell TestingModule to use it:

    describe('App (e2e)', () => {
      let app: INestApplication;
    
      beforeAll(async () => {
        const moduleFixture: TestingModule = await Test.createTestingModule({
          imports: [AppModule],
        }).compile();
    
        app = moduleFixture.createNestApplication();
        await app.init();
        app.useLogger(new Logger());
      });

Upvotes: 6

Jay McDoniel
Jay McDoniel

Reputation: 70450

The default testing logger has no implementation for log, debug, warn or verbose. You'd need to pass your own logger to get those logs to show up again. Generally it's to keep the test output clean

Upvotes: 3

Related Questions