score:62
this would appear to be as simple as setting up url
on the global in jest. something like
describe('download', () => {
const documentintial = { content: 'aaa' };
global.url.createobjecturl = jest.fn();
it('mssaveoropenblob should not have been called when navigao is undefined', () => {
global.url.createobjecturl = jest.fn(() => 'details');
window.navigator.mssaveoropenblob = jest.fn(() => 'details');
download(documentintial);
expect(window.navigator.mssaveoropenblob).tohavebeencalledtimes(1);
});
});
this should result in a test that you can also use for checking if global.url.createobjecturl
was called. as a side note: you may also run into a similar issue with window.open
i would suggest mocking that as well if this becomes the case.
score:-1
use webkiturl.createobjecturl instead of url.createobjecturl its work for me
score:0
the package jsdom-worker happens to provide this method, as well as adding support for web workers. the following worked for me:
npm install -d jsdom-worker
then in package.json, edit or add a jest
key:
{
...
"jest": {
"setupfiles": [
"jsdom-worker"
]
}
}
score:3
just mocking the function global.url.createobjecturl
did not work for me, because the function was used by some modules during import and i got the error jest url.createobjecturl is not a function during import.
instead it did help to create a file mockjsdom.js
object.defineproperty(url, 'createobjecturl', {
writable: true,
value: jest.fn()
})
then import this file as the first import in your file containing the test
import './mockjsdom'
import { myobjects} from '../../src/lib/mylib'
test('my test', () => {
// test code
}
found here: https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
score:10
you just have to write this in your setuptest.js
window.url.createobjecturl = function() {};
score:18
jsdom, the javascript implementation of the whatwg dom used by jest doesn't implement this method yet.
you can find an open ticket about this exact issue on their github page where some workarounds are provided in comments. but if you need the bloburl to actually work you'll have to wait this fr is solved.
workaround proposed in the comments of the issue for jest:
function noop () { }
if (typeof window.url.createobjecturl === 'undefined') {
object.defineproperty(window.url, 'createobjecturl', { value: noop})
}
score:19
since window.url.createobjecturl
is not (yet) available in jest-dom, you need to provide a mock implementation for it.
don't forget to reset the mock implementation after each test.
describe("your test suite", () => {
window.url.createobjecturl = jest.fn();
aftereach(() => {
window.url.createobjecturl.mockreset();
});
it("your test case", () => {
expect(true).tobetruthy();
});
});
Source: stackoverflow.com
Related Query
- Jest test fails : TypeError: window.matchMedia is not a function
- Jest URL.createObjectURL is not a function
- Jest Expected mock function to have been called, but it was not called
- JEST throws .finally is not a function
- Jest : TypeError: require(...) is not a function
- Jest onSpy does not recognize React component function
- In Jest test it says withRouter () is not a function
- Jest error TypeError: (0 , _jest.test) is not a function
- Jest failing with TypeError: (0 , _user.default) is not a function
- ag-grid Jest testing - this.btFirst.insertAdjacentElement is not a function
- classnames_1.default is not a function when testing with Jest
- Function call inside useEffect not firing in jest test
- TypeError: dispatch is not a function when testing with react-create-app jest and enzyme
- JEST Test suite failed to run: TypeError: (0 , _reactRedux.connect) is not a function
- Mock function not getting called in jest
- Mocking Axios with Jest in React - mock function not being called
- Jest spyOn is not a function
- Update not wrapped in act(...) and the mock function not firing. react-testing-library, jest
- history.createBrowserHistory is not a function in Jest test using React
- Jest TypeError: e.preventDefault is not a function
- Jest mock function is called. but the mock's state is not updated?
- Why does the jest mock function not getting executed in my test with react testing library?
- React jest testing Google Maps Api , Uncaught TypeError: this.autocomplete.addListener is not a function
- TypeError: firebase.analytics is not a function when mocking with jest
- Mock function not being called in Jest + Enzyme test
- Jest not detecting function call
- Jest mock function does not register call
- event.persist is not a function - JEST & Enzyme
- Jest testing onClick() gets error 'Expected mock function to have been called, but it was not called.'
- JEST test suite fails with TypeError: environment.setup is not a function
More Query from same tag
- React Components to SvelteKit - Objects with HTML in attribute (JSX rendering) to Script tag
- using data from context before is available
- React Hooks - useState value cannot change
- electron with react error bin sh build command not found
- Async / Await on React not working. Nothing works
- what happens to http request when route changes in SPA
- Build React component from string with placeholders
- OnClick toggles all items
- Dotnet React Template - is it possible to use /index.html
- setState inside a useEffect not working in enzyme
- How to setup sublime 3 with react native so that when you click on the error and it jump to your code?
- ReactJS: onClick handler not firing when placed on a child component
- Higher-order components in Hyperstack
- How to setup dynamic routing with React Router based on props received?
- Proper way to use function properties in React components
- Expected an identifier and instead saw '<'. React.js Project
- How to capture option selected event in Material UI autocomplete component?
- counting on setState in a loop, rendering li with key
- Problem trying to display a list of inputs when updating a record
- async await issue from express api to front end issue
- Meteor ReactLink integration
- Trying to write a function to determine if a complex object's deeply nested sub array has values
- How to check if the element is present in the dom and add some style to another element in dom using react?
- OnsenUI Navigator + React failed to navigate
- Reactjs antd defaultValue return undefined
- pass object to setState fails
- ReactJS: How to have multiple SPA's on the same website
- In React, how to divert the scrolling to another element?
- Redux-react connect function does not work for root component
- intl.formatMessage not working - react-intl