Mohammad.Kaab
Mohammad.Kaab

Reputation: 1105

How to write a test, without mocking the repository in nestjs

I've started working on a small project and I'm now in the process of writing tests for this project, But the problem now I'm facing is related to injecting the repository. I have the following code in beforeEach function

const moduleFixture = await Test.createTestingModule({
            controllers:[CustomersController],
            providers:[
                CustomersService,
                {
                    provide: getRepositoryToken(File),
                    useValue: Repository
                },
            ]
        }).compile();

And I have a test which it calls an endpoint, Now every time I run the tests, This error is showing up

Cannot read property 'find' of undefined

And it's obvious because I have a code in my CustomerService class, this.fileRepository.find(), And the reason it throws the error because the file repository has no methods which any other repository should have. enter image description here

I'll appreciate any help, Thanks.

Upvotes: 0

Views: 813

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70450

If you're wanting to use Repository you should be using useClass instead of useValue. You can see several examples of Repository mocking in this GitHub repo

Upvotes: 1

Related Questions