Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: TypeError: Cannot read property 'prototype' of undefined when using on nodejs #407

Closed
cesarjr opened this issue Dec 31, 2018 · 7 comments

Comments

@cesarjr
Copy link

@cesarjr cesarjr commented Dec 31, 2018

Hi guys!

I'm almost sure it's my mistake, but I spent one entire day trying to solve and I've being failed 馃槥.

I'm trying to configure the fetch-mock on node to use with jest. I tried a lot of things and the best result that I have is this:

image

I'm sure my mock is working because if I pass it through parameter to the "Myclass.query" it works perfectly.

I'm also sure that the mock is arriving in my test file, because the mock function is present in the fetch module.

But... all together aren't working 馃槶.

I create a very simple and small project to see this problem happening:

https://github.com/cesarjr/test-node-fetch

Can anyone help me?

@wheresrhys

This comment has been minimized.

Copy link
Owner

@wheresrhys wheresrhys commented Jan 1, 2019

Ooooh, this is an interesting case.

I think that because you are setting a global mock for node-fetch, then when fetch-mock requires node-fetch internally (in order to use e.g. the Request constructor), the mock is applied here too, so fetch-mock is unable to access anything in the node-fetch module. I'm not sure what the idiomatic jest way would be to work around this, other than to explicitly declare the mock in each test suite rather than relying on the __mocks__ directory convention.

I advise raising an issue with jest (or maybe there's a jest community site?). Please can you also reply here to let me know of any solution(s) you're given. I will definitely need to update the docs here to expand on the advice I currently give for use with jest

@cesarjr

This comment has been minimized.

Copy link
Author

@cesarjr cesarjr commented Jan 2, 2019

Hi! @wheresrhys! If I declare the mock in each test suite, I have a different problem!

If I do this locally, the fetch in my implementation is not mocked. In other words, I think the line nodeFetch.default = fetchMock; in your example does not work.

image

Actually, this was my first try 馃槥

I created here another branch with the implementation as you recommended:

https://github.com/cesarjr/test-node-fetch/pull/1/files

Could you try to help me again? Unfortunately I'm not able to find a solution for my problem.

Thank you!

@wheresrhys

This comment has been minimized.

Copy link
Owner

@wheresrhys wheresrhys commented Jan 6, 2019

Sorry for the slow response.

The way I suggested in the docs works if you're transpiling your server side code using babel, because nodeFetch.default gets used as the main export. But does not work if you're just using the files 'straight'... which is obviously the main use case. I've raised an issue https://stackoverflow.com/questions/54065941/how-to-mock-in-jest-using-an-implementation-which-has-the-original-implementat to ask for advice

@cesarjr

This comment has been minimized.

Copy link
Author

@cesarjr cesarjr commented Jan 7, 2019

Hi @wheresrhys !

Someone answered to me at StackOverflow.

https://stackoverflow.com/questions/54000152/jest-with-fetch-mock-generating-error-typeerror-cannot-read-property-prototyp/54029024#54029024

I think the solution is not good enough, because it will force me to bypass the node injection system.

But he explains the reason of the problem.

@wheresrhys

This comment has been minimized.

Copy link
Owner

@wheresrhys wheresrhys commented Jan 13, 2019

I've done some further investigation. I think there is a bug/undocumented limitation in jest.requestActual which means an approach that should work to get around the global manual mock isn't working. facebook/jest#7620

@wheresrhys

This comment has been minimized.

Copy link
Owner

@wheresrhys wheresrhys commented Jan 19, 2019

I finally cracked it @cesarjr 馃帀

__mocks__/node-fetch.js

const nodeFetch = jest.requireActual('node-fetch');
const fetchMock = require('fetch-mock').sandbox();
Object.assign(fetchMock.config, nodeFetch, {
	fetch: nodeFetch
});
module.exports = fetchMock;
@wheresrhys

This comment has been minimized.

Copy link
Owner

@wheresrhys wheresrhys commented Jan 19, 2019

Thanks for raising the issue. I'm updating the docs now and will close this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can鈥檛 perform that action at this time.