Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Jest中覆盖不同测试套件中模块的模拟数据_Javascript_Unit Testing_Mocking_Jestjs - Fatal编程技术网

Javascript 如何在Jest中覆盖不同测试套件中模块的模拟数据

Javascript 如何在Jest中覆盖不同测试套件中模块的模拟数据,javascript,unit-testing,mocking,jestjs,Javascript,Unit Testing,Mocking,Jestjs,我有一个场景,在我用Jest编写的一个单元测试用例中,我必须在不同的测试套件中使用不同的值集更新模块的模拟响应。下面是我的测试文件,看起来像: // test.spec.js jest.mock('../../Service', () => ({ getState: jest.fn().mockReturnValue({ x: 'x', y: 'y', z: 'z' }) }) describe('Test suite 1

我有一个场景,在我用Jest编写的一个单元测试用例中,我必须在不同的测试套件中使用不同的值集更新模块的模拟响应。下面是我的测试文件,看起来像:

// test.spec.js

  jest.mock('../../Service', () => ({
    getState: jest.fn().mockReturnValue({
      x: 'x',
      y: 'y',
      z: 'z' 
    })
  })

describe('Test suite 1 - with same mocked data for Service', () => ({
   // expected Service.getState() --> { x: 'x', y: 'y', z: 'z' }
})

describe('Test suite 2 - with different mocked data for Service', () => ({
 // expected Service.getState() --> { a: 'a', b: 'b' } 
})
如何使用第二个测试套件中的另一组值更新以下模块的模拟值,如下所示

jest.mock('../../Service', () => ({ 
  getState: jest.fn().mockReturnValue ({ 
    a: 'a',
    b: 'b'
  })
})
是否可以在第二个测试套件中使用beforeach()方法覆盖模拟值?有人能告诉我处理这种情况的正确方法吗

在此方面的任何帮助都将不胜感激。

description('testsuite1-使用与服务相同的模拟数据',()=>({
//预期的服务。getState()-->{x:'x',y:'y',z:'z'}
jest.mock('../../Service',()=>({
getState:jest.fn().mockReturnValue({
x:‘x’,
y:‘y’,
z:‘z’
})
})
})
描述('测试套件2-使用不同的模拟数据进行服务',()=>({
//预期的服务。getState()-->{a:'a',b:'b'}
jest.mock('../../Service',()=>({
getState:jest.fn().mockReturnValue({
a:‘a’,
b:‘b’,
})
})

})
假设您有一个测试文件
SomeComponent.jsx
,其中包括模拟
。/../Service
依赖项,那么您可以这样做:

import { Service } from "../../Service";

jest.mock("../../Service", ({
  getState: jest.fn(),
}));

describe("SomeComponent", () => {
  describe('Test suite 1 - with same mocked data for Service', () => {

    it('should expect mock response 1', () => {
      getState.mockImplementation(() => {
        return {
          x: 'x',
          y: 'y',
          z: 'z'
        }
      })
      expect(Service.getState()).toEqual({x: 'x', y: 'y', z: 'z'});

    });
  });

  describe('Test suite 2 - with different mocked data for Service', () => {

    it('should expect mock response 2', () => {
      getState.mockImplementation(() => {
        return {a: 'a', b: 'b'}
      })
      expect(Service.getState()).toEqual({a: 'a', b: 'b'});

    });
  });
});
您希望测试getState的两个对象,因此使用mockReturnValueOnce使模拟函数在第一次调用中返回object1,在第二次调用中返回object2

您可以从“./../Service”;导入本例中的模拟模块及其
导入{Service},以获得对模拟函数的访问权。然后,在不同测试套件的测试体内调用mockImplementation以设置正确的返回值