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

jest.mock has invalid assignment on recent changes at TypeScript. MockModuleFactory<T> #10291

Closed
gencer opened this issue Jul 20, 2020 · 2 comments

Comments

@gencer
Copy link

gencer commented Jul 20, 2020

💥 Regression Report

Before 26.1.0 my tests using mock with ES6 declarations was working. On 26.1.0 and type script types 26.0.5 it is no longer working.

Last working version

Worked up to version: 26.0.0

Stopped working in version: 26.1.0

To Reproduce

Steps to reproduce the behavior:

jest.mock('../../../modules/i18n', () => {
  return {
    __esModule: true,
    default: { t: () => '[redacted]' },
  }
})

Expected behavior

Tests should pass with this mock. However, I am getting TS Errors for this:

 error TS2345: Argument of type '() => { __esModule: boolean; default: { t: () => string; }; }' is not assignable to parameter of type 'MockModuleFactory<{ __esModule: boolean; default: { t: () => string; }; }>'.
      Type '{ __esModule: boolean; default: { t: () => string; }; }' is not assignable to type '{ __esModule: boolean; default: { t: () => string; }; } & { __esModule: true; }'.
        Type '{ __esModule: boolean; default: { t: () => string; }; }' is not assignable to type '{ __esModule: true; }'.
          Types of property '__esModule' are incompatible.
            Type 'boolean' is not assignable to type 'true'.

    1 jest.mock('../../../modules/i18n', () => {
                                         ~~~~~~~

Link to repl or repo (highly encouraged)

I will provide one as soon as possible. But I'm positively sure, this issue is clear even without a demo.

Suspected change:

DefinitelyTyped/DefinitelyTyped#46072

Run npx envinfo --preset jest

npx: installed 1 in 1.188s

  System:
    OS: Linux 5.4 Ubuntu 20.04 LTS (Focal Fossa)
    CPU: (4) x64 AMD Ryzen 3 1300X Quad-Core Processor
  Binaries:
    Node: 14.5.0 - /usr/bin/node
    Yarn: 1.22.4 - /usr/bin/yarn
    npm: 6.14.5 - /usr/bin/npm
  npmPackages:
    jest: ^26.1.0 => 26.1.0 

@gencer
Copy link
Author

gencer commented Jul 20, 2020

Hmm.. After investigated test cases on types repo, I see that we should emit esModule and default.

So, solution is:

jest.mock('../../../modules/i18n', () => ({
  t: () => '[redacted]',
}))

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant