7

I'm adding tests on a project and improving coverage. I would like to know how can I test a module definition (mymodule.module.ts file) in NestJs. In particular, I'm testing a main module that imports other modules, one of them inits a db connection, this means that I need to mock the service on the other module, to avoid a real db connection. At this moment I have something like this:

beforeEach(async () => {
    const instance: express.Application = express();

    module = await NestFactory.create(MyModule, instance);
});
describe('module bootstrap', () => {
    it('should initialize entities successfully', async () => {
        controller = module.get(MyController);
        ...

        expect(controller instanceof MyController).toBeTruthy();
    });
});

This works, but I'm sure this can get improvements :)

The ideal thing would be something like overrideComponent method provided by Test.createTestingModule.

P.S: I'm using 4.5.2 version

3 Answers 3

10

Improved my tests based on @laurent-thiebault answer:

import { Test } from '@nestjs/testing';
import { ThingsModule } from './things.module';
import { ThingsResolver } from './things.resolver';
import { ThingsService } from './things.service';

describe('ThingsModule', () => {
  it('should compile the module', async () => {
    const module = await Test.createTestingModule({
      imports: [ThingsModule],
    }).compile();

    expect(module).toBeDefined();
    expect(module.get(ThingsResolver)).toBeInstanceOf(ThingsResolver);
    expect(module.get(ThingsService)).toBeInstanceOf(ThingsService);
  });
});
5

You can now test your module (at least for code coverage) by building it that way:

describe('MyController', () => {
    let myController: MyController;

    beforeEach(async () => {
        const module = await Test.createTestingModule({
           imports: [MyModule],
        }).compile();

        myController = module.get<MyController>(MyController);
    });

    it('should the correct value', () => {
        expect(myController.<...>).toEqual(<...>);
    });
});
4

We usually didn't test .module.ts files directly.


we do this in e2e testing.

but I wonder why one should test the the module ! and you are trying to test if the module can initialize it's components , it should.

but i recommend you do this in e2e testing. in my opinion, in unit testing you should focus on testing the services or other components behaviors not the modules.

3
  • 2
    Yes, I'm trying to test if a module can initialize all its dependencies successfully. Improving coverage would be a collateral benefit. Something like this medium.com/spektrakel-blog/… but in NestJs version.
    – Eric
    Commented Feb 28, 2018 at 14:20
  • 4
    The other reason is to show coverage on code coverage report, if we don't test it it's red
    – Reza
    Commented Jun 4, 2021 at 14:24
  • 4
    @Reza Adding this to package.json jest: {"collectCoverageFrom": ["**/*.{!(module),}.(t|j)s"]} fixes the problem with "red" coverage.
    – rawonstack
    Commented Oct 21, 2022 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.