用Jest模拟需要语句

时间:2017-03-10 23:24:50

标签: javascript unit-testing jestjs

sum.js

module.exports = function sum(a, b){
    return a + b;
};

Thing.js

var sum = require("./sum");

module.exports = class Thing {
    add(a, b){
        return sum(a, b);
    }
}

Thing.test.js

test('1 + 2 = 3', () => {
    //Arrange
    var Thing = require('./Thing');
    var thing = new Thing();

    //Act
    var result = thing.add(1, 2);

    //Assert
    expect(result).toBe(3);
});

test('sum mocked', () => {
    //Arrange
    jest.mock('./sum', () => {
        return jest.fn(() => 42);
    });

    var Thing = require('./Thing');
    var thing = new Thing();

    //Act
    var result = thing.add(1, 2);

    //Assert
    expect(result).toBe(42);
});

如何在测试时模拟总和'require'依赖项?我收到以下错误。

sum mocked

    expect(received).toBe(expected)

    Expected value to be (using ===):
      42
    Received:
      3

如果我使用.only单独运行每个测试,有趣的是,它们都可以自行运行。

过去我曾使用proxyquire做过这样的事情但我想尽可能避免使用它。

3 个答案:

答案 0 :(得分:9)

在测试中,我添加了

beforeEach(() =>  {
    jest.resetModules();
});

并且测试按预期通过。

答案 1 :(得分:4)

我觉得每个测试文件的模拟工作。不要问我为什么¯\ _(ツ)_ /¯

对我来说最有效的方法是设置这样的测试:

// sum.test.js
//Arrange
const sum = require('./sum');

test('1 + 2 = 3', () => {
    //Act
    const result = sum(1, 2);

    //Assert
    expect(result).toBe(3);
});

// Thing.test.js
//Arrange
const Thing = require('./Thing');

// jest.mock are hoisted so you can keep imports/require on top
const sumSpy = jest.fn(() => 42);
jest.mock('./sum', () => sumSpy);

test('Thing', () => {
    const thing = new Thing();

    //Act
    const result = thing.add(1, 2);

    //Assert
    expect(sumSpy).toHaveBeenCalledTimes(1);
    expect(result).toBe(42);
});

您甚至可以为每个测试提供不同的模拟实现,例如:

sumSpy.mockImplementation(() => NaN);

答案 2 :(得分:2)

取自Jest Docs。

beforeEach(() => {
  jest.resetModules();
});

test('moduleName 1', () => {
  jest.doMock('../moduleName', () => {
    return jest.fn(() => 1);
  });
  const moduleName = require('../moduleName');
  expect(moduleName()).toEqual(1);
});

test('moduleName 2', () => {
  jest.doMock('../moduleName', () => {
    return jest.fn(() => 2);
  });
  const moduleName = require('../moduleName');
  expect(moduleName()).toEqual(2);
});

https://facebook.github.io/jest/docs/en/jest-object.html#jestdomockmodulename-factory-options