小编wit*_*s14的帖子

为什么这些Jest Mocks不会重置?

我有测试代码影响其他测试并导致它们失败.当我孤立地运行测试用例时,一切都过去了,但是当我运行整个套装时,会有很多失败.如果你看下面的两个测试,你可以看到我在测试中覆盖了一个模拟模块,导致抛出异常.

HttpService.post = jest.fn(() => {
   return Promise.reject({ payload: 'rejected' });
});
Run Code Online (Sandbox Code Playgroud)

在运行此行之后,所有需要原始HttpService.post模拟的测试都会失败,因为它们未被重置.在测试之后,如何正确地将我的模拟恢复到导入的模拟?我已经尝试jest.resetMock过beforeEach和类似于每个jest方法,但没有任何效果.我知道答案可能是直截了当的,但我对我在网上读到的有关如何导入代码的所有差异感到困惑(es6 import,commonJs).谢谢!

import HttpService from '../../services/httpService';
import handleErrors from '../../utilities/handleErrors';

jest.mock('../../services/httpService');
jest.mock('../../utilities/handleErrors');

describe('async actions', () => {

  beforeEach(() => {
    store = mockStore({});
  });

  describe('some describe that wraps both tests', () => {

    describe('a describe that wraps just the first test', () => {
      test(`creates ${constants.actions.REQUEST_SAVE_NOTE_FAILURE}`, () => {
        HttpService.post = jest.fn(() => {
          return Promise.reject({ payload: 'rejected' });
        });
        const expectedActions = [ …
Run Code Online (Sandbox Code Playgroud)

javascript testing unit-testing jestjs redux

9
推荐指数
1
解决办法
3791
查看次数

标签 统计

javascript ×1

jestjs ×1

redux ×1

testing ×1

unit-testing ×1