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

[Bug]: jest.mocked function is not exported from jest object #12131

Closed
hudochenkov opened this issue Dec 7, 2021 · 15 comments · Fixed by #12133
Closed

[Bug]: jest.mocked function is not exported from jest object #12131

hudochenkov opened this issue Dec 7, 2021 · 15 comments · Fixed by #12133

Comments

@hudochenkov
Copy link

Version

27.4.3

Steps to reproduce

Try to use jest.mocked. E. g.:

// foo.ts
export const foo = { };
// foo.spec.ts
import {foo} from './foo';
jest.mock('./foo');

const mockedFoo = jest.mocked(foo);

Expected behavior

jest.mocked is defined and working.

Actual behavior

Error: TypeError: jest.mocked is not a function.

Also documentation is missing on https://jestjs.io/docs/jest-object, but present in source code: https://github.com/facebook/jest/blob/main/docs/JestObjectAPI.md#jestmockedtitem-t-deep--false

Additional context

jest.mocked was added in #12089, but seems it was not added to jest object, and only to jest-mock package.

Tagging author of original PR @k-rajat19

Environment

System:
    OS: macOS 11.6.1
    CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
  Binaries:
    Node: 16.13.0 - /usr/local/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 8.2.0 - /usr/local/bin/npm
  npmPackages:
    jest: ^27.4.3 => 27.4.3
@SimenB
Copy link
Member

SimenB commented Dec 7, 2021

Ah, missed adding it to jest object. We need to add to https://github.com/facebook/jest/blob/c76f9a7c8eb2fab1a15dfe8952d125a8607d1bbe/packages/jest-environment/src/index.ts#L51 at which point we'll get a type error in jest-runtime which should lead the way

/cc @k-rajat19

@cduff
Copy link

cduff commented Dec 13, 2021

Just updated to jest 27.4.4 which includes a fix for this issue but still can't use it as the function isn't in @types/jest? Do we have to wait for it to be added there or is there some other way we're supposed to get TS definitions for jest?

@k-rajat19
Copy link
Contributor

Hi @cduff would be better if you share what exactly error you are getting while using mocked function because it is already added in jest object.

@SimenB
Copy link
Member

SimenB commented Dec 13, 2021

@types/jest isn't maintained in this repo, so open up an issue (or PR) to https://github.com/DefinitelyTyped/DefinitelyTyped

@domdomegg
Copy link

Related discussion: DefinitelyTyped/DefinitelyTyped#57716

@cduff
Copy link

cduff commented Dec 15, 2021

Hi @cduff would be better if you share what exactly error you are getting while using mocked function because it is already added in jest object.

Hi @k-rajat19, I run my jest tests using ts-jest and get jest types from @types/jest as suggested in the official jest documentation. I realise the mocked function has been added to the jest object but I can't use it as it doesn't exist according to @types/jest.

The DefinitelyTyped/DefinitelyTyped#57716 discussion will hopefully get this resolved.

One thing I don't understand though is how anyone can use the new mocked function without it being added to @types/jest? My understanding is that the function just returns the input object and all the magic/purpose happens in the typing. Thus I was querying if there's some other way (other than @types/jest) we're supposed to be getting jest types?

@SimenB
Copy link
Member

SimenB commented Dec 15, 2021

You can use import { jest } from '@jest/globals'; (which is what you must use for native ESM anyways)

@k-rajat19
Copy link
Contributor

these test helper are not depend on @types/jest for their types, all their types are defined in jest-mock.

@samhwang
Copy link

samhwang commented Dec 17, 2021

One thing I don't understand though is how anyone can use the new mocked function without it being added to @types/jest? My understanding is that the function just returns the input object and all the magic/purpose happens in the typing. Thus I was querying if there's some other way (other than @types/jest) we're supposed to be getting jest types?

hey @cduff, So for the time being, what you can do is this, since the mocked function is already exported in jest-mock anyway.

import { mocked } from 'jest-mock';

these test helper are not depend on @types/jest for their types, all their types are defined in jest-mock.

@k-rajat19 I agree that it's not dependent on @types/jest for their types, but it would be easier (DX-wise) to make it available in the @types/jest package, so the usage can be consistent in TS/JS usage, and also with what is described in the Docs (i.e. you shouldn't need to import jest-mock for the mocked function to work in TS, while it can already work in JS). And then again, this is an issue for the @types/jest package in DefinitelyTyped, not here (like @SimenB said above) :D

@k-rajat19
Copy link
Contributor

@samhwang sorry for being late reply I had not get much time to look here, my exams are going on.
I understand what you want to say that the definitions of mocked function should be included in @types/jest for better DX.

import { mocked } from 'jest-mock';

I don't think you explicitly need to import mocked from jest-mock once you imported jest object.

@samhwang
Copy link

samhwang commented Dec 20, 2021

I understand what you want to say that the definitions of mocked function should be included in @types/jest for better DX.

I don't think you explicitly need to import mocked from jest-mock once you imported jest object.

@k-rajat19 yes it is like you said: it all depends on how jest's types are being interpreted in the first place, honestly.

If the jest object is being imported from @jest/globals then yeah it's not needed to import mocked from jest-mock explicitly.

But the original issue is for when that's not the case (like if you're not working with ESModules/or just transpile into CommonJS anyway), TS will fill in from using types: ['jest'] in TSConfig, and it will throw an error while doing so. Some rough examples here. And again, this is an issue for @types/jest, not here.

Screen Shot 2021-12-20 at 11 22 09 am

Screen Shot 2021-12-20 at 11 21 04 am

@SimenB
Copy link
Member

SimenB commented Dec 20, 2021

I don't know why you keep discussing this here - there is absolutely nothing to do in this repo. @types/jest comes from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/jest/index.d.ts, jest.mocked just needs to be added there

@satanTime
Copy link

I'm getting an error: https://app.circleci.com/pipelines/github/ike18t/ng-mocks/2810/workflows/cad5fba3-e1c9-4e59-9d20-daf923c60c31/jobs/101406

    TypeError: Cannot read properties of undefined (reading 'bind')

      at Runtime._createJestObjectFor (node_modules/jest-runtime/build/index.js:2193:46)

is it possible that mocked isn't present in some cases?

@SimenB
Copy link
Member

SimenB commented Dec 25, 2021

Can you open a new issue with a minimal reproduction?

@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 Jan 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants