Skip to content
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

MikroORM does not discover entities when running from jest in my nestjs app #2006

Closed
tma-isbx opened this issue Jul 3, 2021 · 4 comments
Closed
Assignees

Comments

@tma-isbx
Copy link

tma-isbx commented Jul 3, 2021

I get a MetadataError: No entities were discovered when I try to use jest to run my tests.

If I build first, then MikroORM seems to be able to discover my entities from my dist directory. But I get a

ValidationError: Trying to persist not discovered entity of type User. Entity with this name was discovered, but not the prototype you are passing to the ORM. If using EntitySchema, be sure to point to the implementation via `class`.

I am able to reproduce the No entities were discovered issue with https://github.com/mikro-orm/nestjs-realworld-example-app when I try to run yarn testy

To Reproduce
Steps to reproduce the behavior:

  1. Clone https://github.com/mikro-orm/nestjs-realworld-example-app
  2. Copy mikro-orm.config.ts.example to mikro-orm.config.ts
  3. Run yarn test

Expected behavior
I expect the tests to run with entities discovered from my src directory as the dist entities may not be current.

Versions
Using the versions specified in the yarn.lock

Dependency Version
node 14.15.2
typescript ?
mikro-orm ?
your-driver ?
@B4nan
Copy link
Member

B4nan commented Jul 3, 2021

It works fine for me, this is not enough to reproduce anything.

Using the versions specified in the yarn.lock

Well, then just upgrade, that lock file is very outdated. Always first upgrade to latest before filing a bug report.

Also that second error when you build the app points to using different entity than you discover, in other words, you discover JS entity but execute via ts-node or vice versa. You always need to discover the same files you will use - so for ts-node you need to discover TS files from src, for node you need to discover JS files from dist.

Closing this, as there is nothing to reproduce and this is definitely just a misconfiguration. Will update the example project and add a CI pipeline to make sure it works fine, but again, I am using the project locally quite often and it works just fine.

Worth nothing that sometimes you need to explicitly enable the TS mode in the ORM config as it might not be detected properly. But for me that works also just fine in that project - maybe you are on a different platform (I am on macos and CI works on ubuntu). So if you want to run the tests in TS mode and it is not detected properly, just add tsNode: true to the config (but only for tests, not for production).

@B4nan B4nan closed this as completed Jul 3, 2021
@B4nan
Copy link
Member

B4nan commented Jul 3, 2021

Ok so apparently this is working fine on macos but not on linux as the CI failed - but still, what I said above applies, correct way is to enfore TS mode via tsNode: true in the ORM config for tests.

mikro-orm/nestjs-realworld-example-app@7ceea8f#diff-f57a902481ea8bc7a2ff9d6b216cdebec531ba08edb691443b6b4ca2647e5adbR15

@tma-isbx
Copy link
Author

tma-isbx commented Jul 3, 2021

Hi!

Odd. I am on MacOS Big Sur. Adding the tsNode: true when running the tests through ts-jest seems to solve the problem.

Thanks!

@B4nan
Copy link
Member

B4nan commented Jul 3, 2021

Apparently it was not working even for me, but the tests were passing because I had the project built and the test did not really required anything ORM specific to pass, so discovering JS files was enough.

Anyway, asked the ts-jest guys if we can detect this somehow, and apparently we can't, so I PR'ed it (kulshekhar/ts-jest#2717) - expect some future ts-jest and MikroORM versions to support this automatically :]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants