nestjs-request-context icon indicating copy to clipboard operation
nestjs-request-context copied to clipboard

RequestContext.currentContext.req is undefined

Open JuTiger-Lee opened this issue 2 years ago • 5 comments

Hi When running the test code in the unit jest environment, the RequestContext.currentContext.req in req property remains undefined.

JuTiger-Lee avatar Aug 02 '23 02:08 JuTiger-Lee

You can use something like this

jest.mock('nestjs-request-context', () => ({ RequestContext: { currentContext: { req: { user: {}, }, }, }, }));

and then in test suite, just mock the currentContext.req to what you want

RequestContext.currentContext.req.user = ....;

It's my case with mocking user, but you can use it as you want

sebastian-xicay avatar Sep 03 '23 13:09 sebastian-xicay

just mock the currentContext.req to what you want

@sebastian-xicay - how do you access this to mock it?

gregoryorton-ws avatar Apr 27 '24 07:04 gregoryorton-ws

just mock the currentContext.req to what you want

@sebastian-xicay - how do you access this to mock it?

I've attached the example

jest.mock('nestjs-request-context', () => ({
 RequestContext: {
  currentContext: {
   req: {
    user: {},
   },
  },
 },
}));

sebastian-xicay avatar Apr 27 '24 08:04 sebastian-xicay

Oh. But you said 'and then in test suite, just mock the currentContext.req to what you want' - I assumed there was some future mocking beyond what you're saying here @sebastian-xicay

gregoryorton-ws avatar Apr 27 '24 17:04 gregoryorton-ws

Anyway, I found my problem - it was caused by not have my extended prisma client used in my tests.

gregoryorton-ws avatar Apr 27 '24 17:04 gregoryorton-ws