-
I have a BullMQ worker in my NestJS application. I would like to tests this worker: @Processor('myQueue')
export class MyProcessor implements WorkerHost {
constructor(private readonly orm: MikroORM) {}
public async process(job: Job) {
await this.handleJob(job.data);
}
@UseRequestContext()
private async handleJob(data: unknown) {
this.orm.em.findOneOrFail(Author, { name: 'tolgap' })
}
} describe('MyProcessor', () => {
let app: INestApplication;
let processor: MyProcessor;
beforeEach(async () => {
// This creates MikroORM with options:
// `{ allowGlobalContext:true, implicitTransactions: false, registerRequestContext: false }`
app = await createTestApp();
processor = app.get(MyProcessor);
await app.get(EntityManager).begin();
// Create an Author with a specific name to use in my tests
await app.get(EntityManager).insert(Author, { name: 'tolgap' });
});
afterEach(async () => {
await app.get(EntityManager).rollback();
await app.close();
})
it('should not error', async () => {
await expect(processor.process({} as Job)).resolves.toBeUndefined();
}) But this fails with: FAIL tests/my.processor.spec.ts
MyProcessor
✕ should not error (287 ms)
● MyProcessor › should not error
NotFoundError: Author not found ({ name: 'tolgap' })
at Function.findOneFailed (../../node_modules/@mikro-orm/core/errors.js:186:16)
at Object.findOneOrFailHandler [as failHandler] (../../node_modules/@mikro-orm/core/utils/Configuration.js:259:73)
at SqlEntityManager.findOneOrFail (../../node_modules/@mikro-orm/core/EntityManager.js:369:27)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 3.294 s, estimated 4 s
Ran all test suites. I'm think it's because it's forking the EM when running the process function, thus I'm losing my test-managed EM. Any suggestions? BTW: before asking: I'm having this issue only with the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
v6 adds await RequestContext.create(em, async () => {
// ...
}); |
Beta Was this translation helpful? Give feedback.
v6 adds
EnsureRequestContext
, you could copy that to your project and use it instead - it will create new context only if not already available. you would still need to create the async context, i dont think you have that in your test right now.