Skip to content

Integration testing with NestJS #2056

Answered by B4nan
jacobtipp asked this question in Q&A
Discussion options

You must be logged in to vote

With RequestContext helper and DI containers, you work with the global instances. The ORM will internally pick the right "contextual" one where needed, e.g. when working with UoW. This works thanks to the domain API (or in v5 via AsyncLocalStorage) that provides a way to access the EM fork created in a middleware, in any method that gets executed insite the domain handler - that is what RequestContext.getEntityManager() does.

Check the tests to see how it works if this is not enough:

https://github.com/mikro-orm/mikro-orm/blob/master/tests/RequestContext.test.ts

You can check the currently available fork via em.getContext() which under the hood just calls RequestContext.getEntityManager()

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jacobtipp
Comment options

Answer selected by jacobtipp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #2055 on July 21, 2021 08:07.