jest not implemented window.alert()

前端 未结 3 437
春和景丽
春和景丽 2021-01-03 18:50

i have written test for my api with jest . i added function that calls my api in test file as below:

import AuthManager from \"../Client/Modules/Auth/AuthMan         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-03 19:14

    How I solved this problem is actually define the window.alert method at the top of the test file as a jest spy. This should work for any window method (in my case I was actually testing window.open).

    Be sure to call mockClear() in your test, since this is a global object and it's calls will persist across tests.

    window.alert = jest.fn();
    
    test("login api resolves true", () => {
      window.alert.mockClear();
      /* ... */
    })
    

提交回复
热议问题