NestJs Testing - Is REPOSITORY part of the relevant providers/imports within module

1.3k views Asked by At

I am trying to write the unit test cases for the mono-repo based application using nestjs.

I'm facing the following error

Nest cannot export a provider/module that is not a part of the currently processed module (CacheManagerModule). Please verify whether the exported CACHE_MANAGER_REPOSITORY is available in this particular context.

Possible Solutions:
    - Is CACHE_MANAGER_REPOSITORY part of the relevant providers/imports within CacheManagerModule?

Used Packages as follows

"@nestjs/testing": "6.11.7",
"jest": "25.1.0",
"ts-jest": "25.0.0"

Please refer the sample code below

apps\api\src\modules\enduse\enduse.controller.spec.ts

import { INestApplication } from "@nestjs/common";
import { Test } from "@nestjs/testing";
import { CacheManagerModule } from "@app/cache-manager";
import * as request from "supertest";
import { EnduseController } from "./enduse.controller";
import { EnduseService } from "./enduse.service";

describe("Enduse", () => {
    let app: INestApplication;
    beforeAll(async () => {
        const module = await Test.createTestingModule({
            imports: [CacheManagerModule],
            controllers: [EnduseController],
            providers: [EnduseService],
        }).compile();

        app = module.createNestApplication();
        await app.init();
    });
    it("/ (Get Enduse)", async () => {
        const response = await request(app.getHttpServer()).get("enduse/mapping?enduse=123").expect(200);
        return response;
    });
});

libs\cache-manager\src\cache-manager.module.ts

import { Module } from "@nestjs/common";
import { CacheManagerService } from "./cache-manager.service";
import { CacheManagerProviders } from "./cache-manager.provider";

@Module({
    providers: [CacheManagerService, ...CacheManagerProviders],
    exports: [CacheManagerService, ...CacheManagerProviders],
})
export class CacheManagerModule {}

libs\cache-manager\src\cache-manager.provider.ts

import { ReviewModel } from "@app/database/models";
import { CACHEMANAGERCONST } from "./cache-manager.const";

export const CacheManagerProviders = [
    {
        provide: CACHEMANAGERCONST.CACHE_MANAGER_REPOSITORY,
        useValue: ReviewModel,
    },
];

jest.config.js

moduleNameMapper: {
        "^@app/cache-manager": resolve(__dirname, "./libs/cache-manager/src")
}

How to solve the issue?

Thanks.

0

There are 0 answers