tinovyatkin / nest-puppeteer

Puppeteer (Headless Chrome) provider for Nest.js
MIT License
106 stars 23 forks source link

Nestjs test in controller #533

Open lucaspouchot opened 2 years ago

lucaspouchot commented 2 years ago

Hello, I would like to know if anyone is successful in using this add-on using Nestjs tests?

My controller is working fine but my tests are failing. I don't really know how to do it but I have an Error: A circular dependency has been detected when I run me tests.

In my app.module.ts, I have

@Module({
    imports: [
        [....]
        PuppeteerModule.forRoot({
            isGlobal: true,
        }),
        AreaModule,
        [....]
    ],
    [....]
})
export class AppModule {}

In my area.controller.ts, I have

export class AreaController {
    constructor(
        private areaService: AreaService,
        @InjectBrowser()
        private readonly browser: Browser,
    ) {
    }

    [....]
}

and in my area.controller.spec.ts, I have

describe('AreaController', () => {
    let module: TestingModule;
    let controller: AreaController;
    let service: AreaService;
    let browser: Browser;

    beforeAll(async () => {
        module = await Test.createTestingModule({
            controllers: [ AreaController ],
            providers: [ AreaService, Browser ],
        })
            .overrideProvider(AreaService).useValue(new AreaServiceMock())
            .overrideProvider(Browser).useValue(jest.fn())
            .compile();

        controller = module.get<AreaController>(AreaController);
        service = module.get<AreaService>(AreaService);
        browser = module.get<Browser>(Browser);
    });

    [....]
})

And if I don't set the Browser as a provider in the test, I will have : Error: Nest can't resolve dependencies of the AreaController (AreaService, ?)

sebacruz commented 2 years ago

Hello!

I'm stuck with the same problem. Were you able to fix it?

lucaspouchot commented 2 years ago

Hello, Sadly, I ended up not using this module but using Puppeteer directly :( (And, shame on me, I've isolated use of Puppeteer in a module that does not have tests :O )