Skip to content

window.URL.createObjectURL jest fail #115

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

Closed
juliehubs opened this issue Dec 18, 2018 · 11 comments
Closed

window.URL.createObjectURL jest fail #115

juliehubs opened this issue Dec 18, 2018 · 11 comments

Comments

@juliehubs
Copy link

juliehubs commented Dec 18, 2018

When running a jest test, you will receive an error that window.URL.createObjectURL is not a function.

Any ideas of a way to fix this?

I have tried mocking it in jest, as I have found that this is supposedly a mapbox issue, but none of those solutions seem to work.

@GamalielCrdz
Copy link

the same here.
image

@antoinerg
Copy link
Contributor

Thank you for reporting this issue.

@juliehubs You're right that the problem lies in mapbox. Did you try mocking the function as suggested in the following comment mapbox/mapbox-gl-js#3436 (comment)?

@juliehubs
Copy link
Author

juliehubs commented Dec 19, 2018

@antoinerg I tried adding that in my test, didn't seem to work, but I am probably doing something wrong to be honest.

@GamalielCrdz
Copy link

Thank you for reporting this issue.

@juliehubs You're right that the problem lies in mapbox. Did you try mocking the function as suggested in the following comment mapbox/mapbox-gl-js#3436 (comment)?

I added to my test and also install jest-canvas-mock, this works form me

@juliehubs
Copy link
Author

juliehubs commented Dec 19, 2018

What am I doing wrong?! I feel so confused! @GamalielCrdz

window.URL.createObjectURL = function() {};
configure({ adapter: new Adapter(), });

test('renders plotly', () => {
    const wrapper = shallow(
        <MyChart/>
    );
    expect(wrapper).toMatchSnapshot();
    expect(wrapper.state().data).toEqual(null);
});

@GamalielCrdz
Copy link

What am I doing wrong?! I feel so confused! @GamalielCrdz

window.URL.createObjectURL = function() {};
configure({ adapter: new Adapter(), });

test('renders plotly', () => {
    const wrapper = shallow(
        <MyChart/>
    );
    expect(wrapper).toMatchSnapshot();
    expect(wrapper.state().data).toEqual(null);
});

@juliehubs in my case, I added the follow line
window.URL.createObjectURL = function() {};
in my setupTest.js file

@juliehubs
Copy link
Author

juliehubs commented Dec 19, 2018

@juliehubs in my case, I added the follow line
window.URL.createObjectURL = function() {};
in my setupTest.js file

That did it as well as your tip to add jest-canvas-mock! Thanks so much!

@antoinerg
Copy link
Contributor

Thank you @GamalielCrdz!

@ped59430
Copy link

Still getting an error after applying the 2 tips above, any idea ?
(Using vue jest but seems related)

image

@ped59430
Copy link

ped59430 commented Dec 27, 2019

All precisions here hustcc/jest-canvas-mock#2

The tests are passing, but the error is logged due to a jsdom peculiarity on the getContext function.

As I do not need to test getContext and want a clean console (as mentioned in this comment hustcc/jest-canvas-mock#2 (comment)), I added this line window.HTMLCanvasElement.prototype.getContext = () => {} to my jest.setup.js file.

(Trying to install canvas or canvas-prebuilt lead to installation errors)

@enua
Copy link

enua commented Mar 26, 2021

@juliehubs in my case, I added the follow line
window.URL.createObjectURL = function() {};
in my setupTest.js file

If you come here from Vue, I solve this from your solution doing this:

window.URL.createObjectURL = jest.fn();

Hope to helps someone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants