-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Jest mock/spy returns undefined even when set up #9131
Comments
I think you need to check something in your config, because everything is Ok.
|
@joelbarbosa thanks for chiming it. The example you provided doesn't appear to actually be mocking anything: the actual implementation is itself just a mock: // someUtil.js
module.exports = {m1: jest.fn(() => 42)} Not sure if that matters, but I would guess it does. Also not quite apples-to-apples: As I understand it, you are not meant to import the mock: |
I saw that it is logging a mock. I think it's not the same because My project's jest configuration is coming from react-scripts. The only thing I've done is a setupConfig where I globally add the enzyme adapter for react 16. |
Yes, I think |
In your repl example, if I remove the |
Oh yeah, of course it is doing. as you can see, I'll change the behavior of my function. jest.mock('./someUtil', () => ({
m1: jest.fn(() => 'mock me again')
}));
console.log(someUtil.m1); // [Function: mockConstructor].....
console.log(someUtil.m1()); // mock me again |
I created a representative example (https://repl.it/repls/GaseousAcrobaticMainframe), but it does not behave the same locally: Locally, calling the mocked I'm using the same version of Jest (24.9.0). The only difference I can think of is my local was generated from |
@jacob-fueled have you fixed this issue? |
@MateuszS Yes: I stopped using Jest. I could not figure out what the problem was; it quite possibly was coming from create-react-app, but I'd already sank far too many hours trying to get an out-of-the-box feature to work :( |
A little late here, but I was just having this exact issue. I discovered that someone had added Regarding the original issue build environment, it looks like |
mock implementations can work with If you set up the mocks at the top level of the module, in the describe callback (but outside of any it/test callback), or in beforeAll, they get overwritten by resetMocks AFAICT |
@dstapleton92 Thank you so much, I sank a ton of time into google trying to find this answer! |
In my case I wanted to mock a value that got evaluated upon loading the file: export const store = createStore(...); This was always returning undefined, no Instead I had to do: export const store = () => {
return createStore(...);
} |
@dstapleton92 thank you! I've spent hours on this. Could we have something on this somewhere in the documentation ? |
@dstapleton92 - thank you again! I've spent half a day on this and I'm glad I targetted for CRA as the culprit and not Jest! |
Spent one day until I landed here and figured out that CRA is causing mocks to be reset by default. That should definitely not be a default behaviour. |
This also caught me totally by surprise. We've been using But I would have never have expected the restoration of mock functions to restore their original implementations to affect mock functions created on-the-fly with And it's probably consistent as both |
For me it was |
I have been having the same issue. The examples do not work. I get errors of "cannot call .then of undefined". I too am using react-scripts. putting "resetMocks": false into the "jest" portion of my package.json file fixed the problem immediately. I've been pulling my hair out over this for 2 days! I agree that this "attribute" should be more widely published. Perhaps, the jest.fn and jest.mock() documentation can mention that if the mocks get reset then the mocks set up in the top of the test file will no longer work. Lots of people create apps via react scripts and this has to be happening to lots more people. |
Just lost a heap of hours on this also. I really feel like |
Had a similar issue where I couldn't figure out why the import I was trying to mock was ending up
but in my component, logging
So in other words, you have to access the mocked import by actually importing it in the test rather than using the variable that you assigned the import to be mocked by.... |
@HenryCharlesAnderson thanks for providing this information. I tried putting Here's some sample code I've written to test this for more context. In this example,
|
I had the same problem in my React project. Putting I have found that I can implement the supposed jest.mock behavior by using jest.spyon, mockImplementation, and importing from mocks directory. Broken Codeimport {dummyPromise} from '../services/DummyService';
jest.mock('../services/DummyService');
describe('grasping at straws why mocking service promises is not working', ()=>{
beforeEach(()=>{
//jest.mock('../services/DummyService.js'); // still does not work
})
test('can get a mock', ()=>{
//jest.mock('../services/DummyService.js'); // still does not work
expect(jest.isMockFunction(dummyPromise)).toBeTruthy();
})
}) As you can see, putting the Working Code/* eslint-disable jest/no-mocks-import */
import * as DummyService from '../services/DummyService';
import * as DummyServiceMocks from '../services/__mocks__/DummyService';
const mockDummyPromise = jest.spyOn(DummyService, 'dummyPromise');
mockDummyPromise.mockImplementation(DummyServiceMocks.dummyPromise);
describe('grasping at straws why mocking service promises is not working', ()=>{
test('can get a mock', ()=>{
expect(jest.isMockFunction(mockDummyPromise)).toBeTruthy();
})
test('can call the mock', async ()=>{
expect(jest.isMockFunction(mockDummyPromise)).toBeTruthy();
mockDummyPromise().then(data=>{
expect(data).not.toBeNull();
expect(data).toEqual('dummy dummy dummy');
})
})
}) Yes, esLint complains about the import from services/DummyService.jsexport const dummyPromise = () =>{
return new Promise((resolve)=>{
resolve('real dummy real dummy');
});
} services/mocks/DummyService.jsexport const dummyPromise = () =>{
return new Promise( resolve => resolve('dummy dummy dummy'));
} |
None of the above worked and in my case the problem was that I was adding a mock in a |
My issue was the |
@thisismydesign your solution helped me! Thanks |
Solution: Append |
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. |
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🐛 Bug Report
Mocking spy return does nothing
I'm about to lose my mind here; it seems the entire mock system is b0rked. I copy-paste the examples from the docs, and they don't work.
To Reproduce
Other variations that also do nothing
Expected behavior
As documented: it should return
42
envinfo
jest version:
24.7.1
The text was updated successfully, but these errors were encountered: