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
Custom TypeORM Repository undefined in NestJS integration tests #13095
Comments
have you tried this: https://docs.nestjs.com/fundamentals/testing#testing-request-scoped-instances ? |
How would I apply that solution to my problem? |
nvm this is wrong: productService = module.get<ProductService>(ProductService); you must use |
Thank you for the answer, it worked :) |
How to solve it, I also encountered the same problem |
|
Is there an existing issue for this?
Current behavior
I'm trying to write integration tests for my Nest.js application, but something in the test context is making my custom TypeORM repositories undefined. This is probably happening because I'm not using
@InjectRepository
. Instead, I create request scoped injectables that extends a BaseRepository in order to manage the transactions in my requests. (I create and commit/rollback one transaction per request using interceptors. More about that in this article: https://medium.com/@dev.muhammet.ozen/advanced-transaction-management-with-nestjs-typeorm-43a839363491)When I run the tests, I get the following error:
The
productRepo
insideProductService.getProductByName
is undefined. I've already tried changing the scope to default to verify whether that was the issue, but it wasn't.I also wrote a test with the same logic as calling getProductByName, but using the repository directly. Strangely, it works.
Just by running the tests in the minimal repository I provided shall reproduce the error. When you run the application using
yarn start
oryarn start:dev
, it works as expected.Minimum reproduction code
https://github.com/JoaoP12/nest-typeorm-issue
Steps to reproduce
Expected behavior
I expected the tests to pass.
Package
@nestjs/common
@nestjs/core
@nestjs/microservices
@nestjs/platform-express
@nestjs/platform-fastify
@nestjs/platform-socket.io
@nestjs/platform-ws
@nestjs/testing
@nestjs/websockets
Other package
@nestjs/typeorm
NestJS version
10.0.0
Packages versions
Node.js version
18.17.1
In which operating systems have you tested?
Other
Using Postgres 16.1
The text was updated successfully, but these errors were encountered: