Typescript circular reference when testing prisma with jest-mock-extended
Asked Answered
T

3

18

I started writting tests revolving around prisma(v3.6.0) usage in my application.

To do so I followed the official prisma page Unit testing with prisma and I am using jest-mock-extended.

My issue is that I have a typescript error when using the mocked prisma functions :

describe('User routes', () => {
    it('should respond success with array of users', async () => {
        prismaMock.user.findMany.mockResolvedValue([]); // <- here is the error
    }
}
Type of property 'AND' circularly references itself in mapped type 

There is some discussion about this issue on github Testing with prisma. I got 3 options from this discussion :

  • Adding "skipLibCheck": true in tsconfig.json. This breaks some things in my code and doesn't resolve my issue
  • Adding "strictNullChecks": true, no effect either
  • //@ts-ignore the line. This effectively remove the error, and the test runs smoothly

While I am able to do my tests, I would like not to have to ignore this error everywhere in my tests, and ignoring errors is only a good idea until it's not.

Does someone have more information or recommendations about this issue ?

Tool answered 21/12, 2021 at 10:6 Comment(0)
S
1

Set the "stricNullChecks": true in your tsconfig.json, it is highlighted on prisma documentation for the circular dependency error.

https://www.prisma.io/docs/orm/prisma-client/testing/unit-testing

Songful answered 20/6 at 14:57 Comment(0)
N
0

Try rewriting the line

prismaMock.user.findMany.mockResolvedValue([]);

like this

(prismaMock.user as any).findMany.mockResolvedValue([]);
Neb answered 27/12, 2023 at 13:32 Comment(0)
P
-1

Thanks for the thread i was about to open one myself, my understanding is that this is due to the TS check on the prisma lib, the fix that worked for me was to add these to your TS config:

"skipLibCheck": true,
"strictNullChecks": true

I am aware that "skipLibCheck" may break things in your code, but i think that it may be the correct approach, as it will reduce compilation time and a check against 3rd parties lib's (assuming they are 3rd party d.ts files) is not "usually" necessary - as if you are importing the 3rd party library you should trust it works, and it's outside your app.

Pavyer answered 17/2, 2023 at 8:17 Comment(1)
I already have these settings. Still getting the error.Bussey

© 2022 - 2024 — McMap. All rights reserved.