[Bug]: `jest.mocked` function is not exported from `jest` object
See original GitHub issueVersion
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:
- Created 2 years ago
- Reactions:6
- Comments:15 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can use
import { jest } from '@jest/globals';
(which is what you must use for native ESM anyways)Related discussion: https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/57716