Definitelytyped: [@types/jest] 26.0.5: esModule field typing error

Created on 23 Jul 2020  路  6Comments  路  Source: DefinitelyTyped/DefinitelyTyped

Hello!

I have an issue with the latest @types/jest version (26.0.5)
The problem occurs also with latest typescript version (3.9.7), but I'm currently using 3.8.3

I'm attempting to mock a module (partially) and I get an error in tsc.

The mock

jest.mock('multer', () => {
  const fields = jest.fn()
  const defaultFunction: any = jest.fn()
  const memoryStorage = {
    isMemoryStorage: true,
  }
  defaultFunction.memoryStorage = jest.fn(() => memoryStorage)
  return {
    MulterError: class extends Error {
      public code?: string
      constructor(codeParam: string) {
        super()
        this.code = codeParam
      }
    },
    __esModule: true,
    default: defaultFunction,
    fields,
    memoryStorage: () => memoryStorage,
  }
})

The error
Type '{ MulterError: typeof MulterError; __esModule: boolean; default: any; fields: jest.Mock<any, any>; memoryStorage: () => { isMemoryStorage: boolean; }; }' is not assignable to type '{ __esModule: true; }'

Only quickfix I found so far
__esModule: true as true

This happened following latest update on this part
https://github.com/DefinitelyTyped/DefinitelyTyped/pull/46072#pullrequestreview-448772973
I tried understanding what the issue was in my code by reading the explanations but can't pinpoint it (also it seems like a breaking change to me since it was working before? 馃)

cc. @devanshj any help would be kindly appreciated 馃檹

All 6 comments

Hey thanks for reporting, your code is fine the problem is true is inferred as boolean instead of true. We just need to type mock in a way that makes the inference work. I think I know a way let me check if it works (afk) I'll get back in some time. In worst case we would revert it.

@devanshj Thanks for the quick answer, I'll hold on the previous version until then 馃檪

Reproduction, ~Fix, Better Fix. I'll send a PR soon.~ Ugh kills the whole point of it, because not passing __esModule: true doesn't give a compiler error. Let me try a few more things haha.

Yay, Finally! Sending a PR

@devanshj Nice that was fast! Thanks for the work 馃挭

You're welcome! But unfortunately this is also not apt because a mock factory can also return a function, a string, module without default etc etc there's a lot of complexity so I'm reverting before we can find a good fix.

Was this page helpful?
0 / 5 - 0 ratings