score:4
this is what i ended up using:
it('should redirect if no rights', () => {
// this way we mock only one method: redirectto
jest.mock('lib/utils', () => {
const original = require.requireactual('lib/utils');
original.default.redirectto = jest.fn();
return original;
});
const redirectto = require.requiremock('lib/utils').default.redirectto;
mount(
<somecomponent />,
);
expect(redirectto).tohavebeencalled();
});
score:10
you have to mock the lib/utils
module like this:
import utils from '../../lib/utils';
jest.mock('../../lib/utils', () => ({
redirect: jest.fn()
}))
it('should redirect if no rights', () => {
mount(
<somecomponent />,
);
expect(utils.redirect).tohavebeencalledwith();
});
this will replace the module with a mock that just returns {redirect:jest.fn()}
. this module is also imported into you test where you then can access the spy for redirect
and test on this that it was called with the correct parameter.
Source: stackoverflow.com
Related Query
- How to mock external module method?
- Using Jest, how do I mock a method of a module used in a component to see how many times it's been called and with what values
- How can I mock the window.alert method in jest?
- Jest: How to mock default export Component when same module also has named export?
- How to mock a React component lifecycle method with Jest and Enzyme?
- How do you mock just certain parts of a module with jest?
- How to make svg react component imports from external module working with create-react-app?
- How to properly mock a node module for jest unit testing
- How to correctly mock React Navigation's getParam method using Jest
- How to pass an external method to a stateless functional component in React
- How to mock a React Component static method with Jest
- How to test a promise returned by an external module using axios.create in Jest?
- How do I mock this method chain in Jest?
- How to import external data fetch api into react componentDidMount method
- How do I mock a const method IN a component as part of an integration test?
- How to properly load CSS from an external module in React?
- How to mock react custom hook return value as a module with Jest
- How to override external module class with css module pattern in react
- React-Jest Unit test an external module method that affects state
- How to structure my npm module to use external config file if present?
- Cannot mock external node module
- How to mock an object/function inside a module
- How to mock component method using Jest and Enzyme
- How to reset a specific method of a module with resetAllMocks in Jest?
- How to mock a module import with Sinon and ReactJS
- how to use jest to mock method of react class
- Jest - how to mock a function which is invoked in module creation
- How to mock a method which returns response object using Jest
- How to mock a method that is returning a subscribe
- How should I use external component module that does not include @type in React?
More Query from same tag
- How to connect from the edge of a circle instead of the center
- Slideshow using React hooks and setInterval
- Dynamically render a react component with react routing, based on value given in url
- The result fall into an infinite cycle in React.js
- Integrating jQuery plugin with React
- How access Redux store on service worker
- From mapStateToProps to setState
- How to render images with webpack + react?
- How to get a reference to react component from a document event?
- Styling rendered iframe in react
- ReactJS: How do I go the next page after condition is true?
- React render does not update
- Hide or display targeted elements, after mapping, in React (functional components)
- CSP and PDF ad data-blob in iFrame causes rejection
- useEffect dependency exceptions
- how to stop rerendering in react on checkbox check?
- React Component not displayed in HTML
- How to add radio button to react-sortable-tree?
- Display React component Source code using <code> tag
- Mapping from useState question - react/Js
- Extend JSS style class instead of overwriting it
- React global state update local state
- How to useContext hook in Nextjs Layout
- Can I res.send an entire page in a react class for nodejs isomorphic react?
- How can Redux guarantee no race condition?
- loosing old data when updating the array in zustand
- Invariant Violation in React Render OR the proper way to iterate and return in React
- Updating documents of mongodb using react nodejs and ajax
- Temporarily disable react-loadable
- I am facing problem using Simple React Lightbox Next js?