首页 > javascript > Jest模拟内部函数

Jest模拟内部函数 (Jest mock inner function)

2019-03-16 javascriptjestjs

问题

我有一个名为helper.js的文件,它由两个函数组成

export const funcA = (key) => {
   return funcB(key)
};

export const funcB = (key,prop) => {
   return someObj;
};

我有我的helper.spec.js来测试helper.js文件函数。

import {funcA,funcB} from 'helper';

describe('helper', () => {
   test('testFuncB', () => {

   }
   test('testFuncA', () => {

   }
}

funcB的测试非常简单我只是调用它并期望someObj
问题是测试funcA,为了测试它我想模拟funcB的响应。

我希望testFuncB调用实际的funcBtestFuncA调用mocked funcB

如何才能在我的两个测试中实现funcB被嘲笑和原创?

这不是重复的。这是一个不同的情况:它们只模拟内部调用函数,如果我删除testFuncB然后它将是相同的但我必须在testFuncB上执行测试。

解决方法

如果ES6模块直接导出两个函数(不在类,对象等中,只是直接导出问题中的函数)而一个直接调用另一个函数,那么该调用就不能被模拟

在这种情况下,funcB 不能funcA在代码当前编写的方式内进行模拟

有模拟替换模块出口funcB,但funcA不调用模块输出funcB,它只是调用funcB直接。


惩戒funcBfuncA需要funcA调用模块导出funcB

这可以通过以下两种方式之一完成:


移动funcB到自己的模块

funcB.js

export const funcB = () => {
  return 'original';
};

helper.js

import { funcB } from './funcB';

export const funcA = () => {
  return funcB();
};

helper.spec.js

import * as funcBModule from './funcB';
import { funcA } from './helper';

describe('helper', () => {

  test('test funcB', () => {
    expect(funcBModule.funcB()).toBe('original');  // Success!
  });

  test('test funcA', () => {
    const spy = jest.spyOn(funcBModule, 'funcB');
    spy.mockReturnValue('mocked');

    expect(funcA()).toBe('mocked');  // Success!

    spy.mockRestore();
  });
});

将模块导入自身

“ES6模块自动支持循环依赖”,因此它对import模块本身完全有效,因此模块中的函数可以调用模块中的其他函数的模块导出

helper.js

import * as helper from './helper';

export const funcA = () => {
  return helper.funcB();
};

export const funcB = () => {
  return 'original';
};

helper.spec.js

import * as helper from './helper';

describe('helper', () => {

  test('test funcB', () => {
    expect(helper.funcB()).toBe('original');  // Success!
  });

  test('test funcA', () => {
    const spy = jest.spyOn(helper, 'funcB');
    spy.mockReturnValue('mocked');

    expect(helper.funcA()).toBe('mocked');  // Success!

    spy.mockRestore();
  });
});

问题

I have one file called helper.js that consist of two functions

export const funcA = (key) => {
   return funcB(key)
};

export const funcB = (key,prop) => {
   return someObj;
};

I have my helper.spec.js to test the helper.js file functions.

import {funcA,funcB} from 'helper';

describe('helper', () => {
   test('testFuncB', () => {

   }
   test('testFuncA', () => {

   }
}

The test for funcB is pretty simple i just call it and expect someObj
The problem is to test funcA, in order to test it i want to mock the response of funcB.

I want testFuncB call the actual funcB and testFuncA call mocked funcB

How can i achieve funcB to be mocked and original in my two tests?

This is not a duplicate. It is a different case: they mock inner called functions only, if I remove the testFuncB then it will be the same but I must perform test on testFuncB too.

解决方法

If an ES6 module directly exports two functions (not within a class, object, etc., just directly exports the functions like in the question) and one directly calls the other, then that call cannot be mocked.

In this case, funcB cannot be mocked within funcA the way the code is currently written.

A mock replaces the module export for funcB, but funcA doesn't call the module export for funcB, it just calls funcB directly.


Mocking funcB within funcA requires that funcA call the module export for funcB.

That can be done in one of two ways:


Move funcB to its own module

funcB.js

export const funcB = () => {
  return 'original';
};

helper.js

import { funcB } from './funcB';

export const funcA = () => {
  return funcB();
};

helper.spec.js

import * as funcBModule from './funcB';
import { funcA } from './helper';

describe('helper', () => {

  test('test funcB', () => {
    expect(funcBModule.funcB()).toBe('original');  // Success!
  });

  test('test funcA', () => {
    const spy = jest.spyOn(funcBModule, 'funcB');
    spy.mockReturnValue('mocked');

    expect(funcA()).toBe('mocked');  // Success!

    spy.mockRestore();
  });
});

Import the module into itself

"ES6 modules support cyclic dependencies automatically" so it is perfectly valid to import a module into itself so that functions within the module can call the module export for other functions in the module:

helper.js

import * as helper from './helper';

export const funcA = () => {
  return helper.funcB();
};

export const funcB = () => {
  return 'original';
};

helper.spec.js

import * as helper from './helper';

describe('helper', () => {

  test('test funcB', () => {
    expect(helper.funcB()).toBe('original');  // Success!
  });

  test('test funcA', () => {
    const spy = jest.spyOn(helper, 'funcB');
    spy.mockReturnValue('mocked');

    expect(helper.funcA()).toBe('mocked');  // Success!

    spy.mockRestore();
  });
});
相似信息