Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nestjs test in controller #533

Open
lucaspouchot opened this issue Nov 23, 2021 · 2 comments
Open

Nestjs test in controller #533

lucaspouchot opened this issue Nov 23, 2021 · 2 comments

Comments

@lucaspouchot
Copy link

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
Copy link

sebacruz commented Sep 8, 2022

Hello!

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

@lucaspouchot
Copy link
Author

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 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants