jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.01k stars 6.42k forks source link

Less ambiguous jest.mock #10951

Open bisubus opened 3 years ago

bisubus commented 3 years ago

🚀 Feature Proposal

The proposal is to allow to specify that jest.mock('my-module') should use __mocks__ or not.

Motivation

jest.mock is too magical, it's nearly always known if it's automocking or __mocks__ that is expected in a test. Silently switching between auto and manual mock in jest.mock('my-module') results in a undetermined behaviour that may not be evident and needs to be debugged.

A notable example is facebook/create-react-app#7539

Example

Uses manual mock if it exists, throws an error that contains full path to expected __mocks__/my-module.js otherwise:

import myModule from 'my-module';
jest.mock('my-module', { manual: true })

Ignores __mocks__/my-module.js and does automocking:

import myModule from 'my-module';
jest.mock('my-module', { manual: false })

Pitch

Module mocking is core Jest feature and needs to be practical out of the box.

johanbook commented 3 years ago

I second this. I have just spent an hour trying to debug why a manual mock isn't being used and this would be immensely useful.

github-actions[bot] commented 2 years ago

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

bisubus commented 2 years ago

The issue is still relevant.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

bisubus commented 1 year ago

The issue is still relevant.

github-actions[bot] commented 1 month ago

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

bisubus commented 1 month ago

The issue is still relevant.