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();
  });
});

Related Query

More Query from same tag