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

autoLoadEntities does not work when using Test.createTestingModule #17

Closed
roderik opened this issue Jan 31, 2021 · 8 comments
Closed

autoLoadEntities does not work when using Test.createTestingModule #17

roderik opened this issue Jan 31, 2021 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@roderik
Copy link

roderik commented Jan 31, 2021

Describe the bug

I recently switched to the autoLoadEntities way to load entities.

Imagine the following "flow"

  • MainAppModule
    • imports AuthModule
      • imports UserModule
        • imports MikroOrmModule.forFeature([User])

This works perfectly while running the application.

But when testing this, the entities do not get loaded.

  beforeEach(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [MainAppModule],
    }).compile();

Gives me the following stack trace

    No entities found, please use `entities` option

      at Configuration.validateOptions (../../node_modules/@mikro-orm/core/utils/Configuration.js:146:19)
      at new Configuration (../../node_modules/@mikro-orm/core/utils/Configuration.js:22:18)
      at new MikroORM (../../node_modules/@mikro-orm/core/MikroORM.js:20:27)
      at Function.init (../../node_modules/@mikro-orm/core/MikroORM.js:38:21)
      at InstanceWrapper.useFactory [as metatype] (../../node_modules/@mikro-orm/nestjs/mikro-orm.providers.js:21:32)
      at Injector.instantiateClass (../../node_modules/@nestjs/core/injector/injector.js:289:55)
      at callback (../../node_modules/@nestjs/core/injector/injector.js:42:41)
      at Injector.resolveConstructorParams (../../node_modules/@nestjs/core/injector/injector.js:114:24)
      at Injector.loadInstance (../../node_modules/@nestjs/core/injector/injector.js:46:9)

I can work around it by putting a MikroOrmModule.forFeature for all entities referenced when initializing the testing module but this is less than optimal.

Versions

Dependency Version
node v14.15.1
typescript 4.1.3
mikro-orm 4.4.0
mongo 3.6.3
@roderik roderik added the bug Something isn't working label Jan 31, 2021
@anlauren
Copy link

Hello! any news on that? from what i'm reading in the module code it would be because this line:

 const key = (preferTsNode && this.config.get('tsNode', Utils_1.Utils.detectTsNode()) && this.config.get('entitiesTs').length > 0) ? 'entitiesTs' : 'entities';

returns "entities" instead of "entitiesTs". Would it come from the fact that jest config says ts-jest
I'm not sure if there's a way around to enforce ts node in a test config, i see the preferTsNode value there 🤔

@B4nan
Copy link
Member

B4nan commented Mar 16, 2021

You can control that just by passing tsNode: true to the ORM config - that way the detection is skipped and entitiesTs will be used.

@anlauren
Copy link

Beautiful i'll try this out!

@anlauren
Copy link

Works nicely!
Don't know if it's worth mentioning somewhere in the docs for people using jest in that way 🤔

@B4nan
Copy link
Member

B4nan commented Jul 3, 2021

ts-jest detection will land soon hopefully, related PR adding support for detecting ts-jest: kulshekhar/ts-jest#2717

@B4nan
Copy link
Member

B4nan commented Aug 28, 2021

Closing, should work out of box with ts-jest 27.0.4+ and MikroORM v4.5.9+

@B4nan B4nan closed this as completed Aug 28, 2021
@xenoterracide
Copy link

tried ts-node: true in the latest, though I'm not actually certain that's what we're doing. didn't work, same problem. Feels like a hokey fix though.

@xenoterracide
Copy link

❯ yarn why ts-jest
└─ services-core@workspace:.
   └─ ts-jest@npm:27.1.5 [8316d] (via npm:27.1.5 [8316d])
❯ yarn why @mikro-orm/core
└─ services-core@workspace:.
   └─ @mikro-orm/core@npm:5.6.3 [8316d] (via npm:^5.6.3 [8316d])
❯ yarn why jest
├─ jest-cucumber@npm:2.0.12
│  └─ jest@npm:27.5.1 [8875a] (via npm:27.5.1 [8875a])
│
└─ services-core@workspace:.
   └─ jest@npm:27.5.1 [8875a] (via npm:27.5.1 [8875a])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants