score:0
you can return a mock function jest.fn
in your sweetalert.js
mock:
module.exports = jest.fn();
and write your test like this:
import { dosomething } from './dosomething';
import swal from 'sweetalert';
describe('login container', () => {
it('calls swal', () => {
expect(swal).tohavebeencalledtimes(0);
dosomething();
expect(swal).tohavebeencalledtimes(1);
});
});
note that i'm using sweetalert
in my example code not sweetalert2
.
hope this help!
score:0
i would expect that the mock factory needs to return an object with default
(because import swal is importing the default module). something like this (demoing sweetalert v1):
// extract mocked function
const mockalert = jest.fn()
// export mocked function as default module
jest.mock('sweetalert', () => ({
default: mockalert,
}))
// import the module that you are testing after mocking
import dosomethingthatalerts from './dosomethingthatalerts'
// test suite loosely copied from op
describe('login container', () => {
it('calls swal', () => {
dosomethingthatalerts();
// test mocked function here
expect(mockalert).tohavebeencalled();
});
});
Source: stackoverflow.com
Related Query
- How do I mock a third party package with Jest?
- How to mock React component methods with jest and enzyme
- How to mock history.push with the new React Router Hooks using Jest
- How to mock out sub-components when unit testing a React component with Jest
- How do I properly mock third party libraries (like jQuery and Semantic UI) using Jest?
- How to mock DataTransfer with Jest
- How do you mock a react component with Jest that has props?
- How to mock API calls made within a React component being tested with Jest
- React: How to mock Auth0 for testing with Jest
- How to mock a React component lifecycle method with Jest and Enzyme?
- How to mock or assert whether window.alert has fired in React & Jest with typescript?
- How to mock window.location.href with Jest in React?
- how do you mock a default exported object with jest per test?
- How can I mock axios API calls? with using jest
- How to mock the formik useFormikContext hook when writing unit tests with jest
- Jest unit test with Luxon: how do I mock .setZone('local')
- How to replace a React component with a mock when testing with Jest
- How can we implement an Open with Postman button to open a postman collection link sent from a third party react application?
- How to mock redux actions with Jest
- How to get a Keycloak mock to work with Jest
- How to mock a third party React component using Jest?
- How to get mock utility function with jest
- How to mock properly with Jest a React component using render props
- How to mock a React Component static method with Jest
- How to mock React Context for testing component with jest
- I have a props with JSX.Element. How do I mock the same in the jest test
- How to mock React component events with jest and enzyme
- How to mock a class exported from a third party library?
- How to mock react custom hook return value as a module with Jest
- How to declare third party modules as globals so i wont need to import?(Webpack5 with typescript)
More Query from same tag
- Unable to affect child elements with css
- TypeError: this.activateMode is not a function (Gutenberg WordPress)
- How to set Material UI (withStyle) style on an external file in React?
- Configuring react-bootstrap with Rails 4.2
- Force scroll down to a particular point after some user scrolling [React]
- value is not updating in useEffect of reactjs hooks?
- ReactJS ES6 adding logic to the component
- Error: "You must provide an app access token, or a user access token that is an owner or developer of the app" using Facebook Login SDK
- Destructing an object and using it - React prop
- Check Jwt token Expiry react drf
- ComponentWillMount and ComponentDidMount not synced
- how to add slash in react router path
- How to style Link component when on active page on Next.js
- i18n for array elements of react component
- How do I extract the file names from an "upload file" component and display them in a list on the UI?
- React Router Switch routes not being matched
- Getting compile error from React Component useState
- Reactjs: using updated state data (from store) immediately
- Why does my Get request sometimes work, but most of the time 404?
- REACTJS .join() not working in app.js but working in other .js components ERROR:TypeError: Cannot read property 'join' of undefined
- How do I implement a functional so that you can interact with the form only once for each user?
- Uncaught (in promise) Error:createError.js:16 Uncaught (in promise) Error: Request failed with status code 404
- Accessing event trigger button onclick function
- Applying conditional style on click on single element in collection
- Undefined when selecting div with information
- How to set defaultValue for react-select Async and not reset when onBlur at v2.0.0?
- Redux: altering different parts of the initial state in Reducer according to Actions
- Distributing ReactJS components written in ES6 and usnig them inside ES5 source code
- requestAnimationFrame not working as expected
- Error while dispatching from onChange event inside the return function