score:0
add a wrapper
(function (factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
factory()();
}
}(function() {
// you code
return function () {}
}));
score:1
an iife will always run when the file gets imported.
you can mock the entire file so it never runs, but then you can't access the functions defined in it.
there isn't a way to mock just the iife while still being able to access the other functions defined in the same file since anything that imports the code (including jest.requireactual
) will also run the iife.
sounds like the two options you have are to set up your testing environment so that the iife doesn't throw an error when it runs, or move the functions to their own module so you can import and test them independently from the iife.
Source: stackoverflow.com
Related Query
- How to mock an IIFE while importing in jest test
- How to get test coverage from Jest while testing React.js App?
- How to replace global function while doing unit test with jest
- Jest unit test with Luxon: how do I mock .setZone('local')
- How to mock jest function importing from another file as default
- How to test a return value of a mock function in jest
- I have a props with JSX.Element. How do I mock the same in the jest test
- Jest manual mocks with create-react-app: How to use mock values in test
- How to mock and test scrollBy with Jest and React-Testing-Library?
- How to mock a "<script>" in react jest unit test
- How can I mock multiple get axios request in Jest unit test for an async action that calls a few more async actions?
- How to test and/or mock ref callback nodes and test .querySelector with Jest mocking + Enzyme shallow?
- Jest How to test error while submitting data to server from form?
- How can I mock an imported React hook/module and test that it's being called properly on different test cases using Jest
- How do I test a polling react hook using Jest with mock timers?
- how to test component while passing boolean value with react testing library and jest
- Jest + React Testing Library: how do I mock out a method from some library that only affects one test block
- How to ignore a jest mock after the first test when testing react reducer
- How do you test for the non-existence of an element using jest and react-testing-library?
- How to test a className with the Jest and React testing library
- how to change jest mock function return value in each test?
- How to mock an exported const in jest
- How do I test a jest console.log
- Importing images breaks jest test
- How to mock React component methods with jest and enzyme
- How to mock history.push with the new React Router Hooks using Jest
- Figuring out how to mock the window size changing for a react component test
- How to use Jest to test functions using crypto or window.msCrypto
- How to mock window.navigator.language using jest
- How do you mock useLocation() pathname using shallow test enzyme Reactjs?
More Query from same tag
- Google Maps: Restrict the user to move the marker maximum of 800 meters from the position on load
- Passing data through props and then setting to state becomes undefined?
- Status of React Navigation drawer? (open or closed)
- type error = assignment to constant variable react.js
- Converting a class to a functional component
- Web socket constantly disconnecting in react app. Backend is Nestjs
- How to solve Support for the experimental syntax 'jsx' isn't currently enabled
- Put calls in a queue and waiting for updating the state in React.js
- Callback function from parent to child in React
- Dynamic loading of react components
- How to set a variable in a parent component from a child component
- next-mdx-remote doesn't pass the component
- How to access ref that was set in render
- How can I set react-particles-js as the background while using React?
- React call a function inside return to insert a component
- React routing - id params in the URL is undefined when I pass it with history.push
- How to remove NavLink styling for unselected Routes
- Do I have to return state in a Redux Reducer?
- Scrollable div to stick to bottom, when outer div changes in size
- Go back 2 pages - React-Router - ReactJS -
- <input> value prop is applied without re-rendering
- How to set buildId to config (publicRuntimeConfig) or environment variable?
- Array .map() is returning HTML [object Object]
- passing a parameter from input text through onClick on reactjs
- How to set state to filtered item react
- React useState didn't catch previous state
- Response not shown on setting the maxlength in Textfield of Material UI
- push to new page within function react
- reactjs + webpack + babel 7 Syntax error: The rest element has to be the last element when destructing
- React js this.props.match.params empty when using protected routes