-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Add docs section to highlight jest.requireActual #6651
Conversation
docs/BypassingModuleMocks.md
Outdated
// AFTER | ||
jest.mock("node-fetch") | ||
import fetch from "node-fetch" | ||
const { Response } = require.requireActual("node-fetch") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you change all require.requireActual
to jest.requireActual
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nice! Is that something jest supports? I can't see any reference to it in https://jestjs.io/docs/en/jest-object. Is it worth changing that part of the docs too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jest.requireActual
was added in #4260 as an alias - and we want to encourage people to use the jest
object.
Absolutely worth updating the docs, yeah 🙂
@bdrobinson you can see a preview with your pr here: https://deploy-preview-6651--jest-preview.netlify.com/ (to be specific: https://deploy-preview-6651--jest-preview.netlify.com/docs/en/next/bypassing-module-mocks) |
Also, please run |
I've run the linting and updated it to refer to |
Codecov Report
@@ Coverage Diff @@
## master #6651 +/- ##
=======================================
Coverage 63.73% 63.73%
=======================================
Files 235 235
Lines 8938 8938
Branches 3 4 +1
=======================================
Hits 5697 5697
Misses 3240 3240
Partials 1 1 Continue to review full report at Codecov.
|
import {createUser} from './createUser'; | ||
|
||
test('createUser calls fetch with the right args and returns the user id', async () => { | ||
fetch.mockReturnValue(Promise.resolve(new Response('4'))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could use fetch.mockResolvedValue(new Response('4'))
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, didn't know that existed!
Great doc, thanks! |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
First – thanks so much for this project, I've been using Jest for a couple of years and have benefited from it a lot!
Summary
I've been in the situation a few times where I've had a problem that could be solved by
require.requireActual
but didn't know about it because it seems to be quite hidden away in the docs (as far as I could tell, it's only really documented in the "Globals" section of the API reference). As a result of not knowing about it, I've wasted quite a lot of hours writing my own manual mocks rather than using Jest's automocking features. This PR adds a section to the docs entitled "Bypassing module mocks" which presents an issue similar to one I encountered recently, and talks the reader through howrequire.requireActual
can solve the problem.Let me know what you think. I've tried to match the tone of the rest of Jest's docs, and tried to pick an example that's simple enough to quickly make sense of but also resonates with the sort of tests people would actually write in real life.
I'm not at all wedded to the particular example I use – I'm mostly just keen that something gets added to the docs that draws attention to
require.requireActual
.When I built the docs locally (by running
yarn start
in thewebsite
folder) it didn't seem reflect my changes as I made them, so I haven't been able to verify if the markdown gets rendered into the web page correctly. But I imagine that's a simple task.Thanks very much.
Test plan
N/A