-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Improve TypeORM Testing Documentation #246
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
Comments
You saved my day, it seems to me that docs must have a scetion that describes what to do if you just want to use existing database in tests |
Workaround: https://stackoverflow.com/a/59483875/5172890 |
I spent most of yesterday figuring out how to get my e2e tests working. The documentation really wasn't very helpful. The auto-generated e2e test imports the app module in the testing module, so I thought that was the way to do it. However, my app module includes both TypeORM and BullMQ, so it tries to connect to both the database and Redis. I figured I'd just In the end, the solution ended up being to not test the app module itself, but to create a testing module that includes only the parts of the app module that I intend to test and then auto-mock all dependencies I don't need custom mocks for. I also created an e2e test where instead of mocking the TypeORM repository, I simply provide a I'm happy to open a PR for this, but I thought I'd start by checking if my understanding is correct and this is in fact the way to do it. |
This was requested in nestjs#246 and since I was recently struggling to set this up in one of my own projects, I have it fresh in memory how to do this. If this was in the documentation when I was implementing this, it would have saved me a lot of time. There are many dodgy approaches to testing NestJS applications with a test database on the Internet and an official recommended way would be most helpful.
This was requested in nestjs#246 and since I was recently struggling to set this up in one of my own projects, I have it fresh in memory how to do this. If this was in the documentation when I was implementing this, it would have saved me a lot of time. There are many dodgy approaches to testing NestJS applications with a test database on the Internet and an official recommended way would be most helpful. Resolves nestjs#246
I'm submitting a...
[X] Documentation issue or request
Current behavior
When testing classes that use TypeORM using Jest, there is inconsistent behavior if the TypeORM database connection is established in a Jest setupFile, like:
This worked for most for all of our entities except for one where it gave the dreaded "no metadata found" TypeORM error. The same entity worked fine when not running inside a TestModule so the entity definition wasn't the issue. It's possible there was a race condition but all of the other entities worked consistently.
Eventually, I figured out that the proper way to use TypeORM inside of a Jest Spec is like this:
I.e. always inject the Nest.Js TypeORM module into your testing module.
Expected behavior
Update the testing section of these docs to show an example using a real database as well as the mocked example.
The text was updated successfully, but these errors were encountered: