question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
SimenBcommented, Dec 15, 2021

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

2reactions
domdomeggcommented, Dec 15, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Spying on a non-exported node.js function using jest not ...
mock doesn't work as expected. Am I missing something here or Is there an easy way to achieve the same ? The error...
Read more >
Testing Non-Exported Functions in JavaScript - Samantha Ming
To test exported function, you can simply import it. But what about non-exported functions? Well that's easy too, thanks to Rewire!
Read more >
Mock Functions - Jest
The mocked() helper method wraps types of the source object and its deep nested members with type definitions of Jest mock function. You...
Read more >
How to mock imported functions with Jest - DEV Community ‍ ‍
jest.mock() is called with one required argument - the import path of the module we're mocking. It can also be called with an...
Read more >
3 Ways to Mock Named Exports in Jest - Webtips
In order to mock named exports in Jest, you need to import all named exports from a file and assign jest.fn() to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found