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

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 https://github.com/facebook/jest/pull/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

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 6
  • Comments: 15 (8 by maintainers)

Most upvoted comments

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

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

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?

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

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) 😄