jest.mock()等同于sinon.stub()

时间:2018-12-04 08:47:43

标签: javascript jestjs sinon

在所有用于云功能快速入门的Firebase SDK的测试示例中,都使用Mocha / Chai和Sinon ...尝试使用Jest代替,我想知道sinon.stub()的正确含义是什么?

我尝试了以下操作(仅用于测试jest.mock()接受...

const sinon = require('sinon');

const jest = require('jest');

describe('Cloud Functions', () => {

    before(() => {
      let myFunctions, adminInitStub;
      let adminInitMock;

      adminInitStub = sinon.stub(admin, 'initializeApp');
      adminInitMock = jest.mock(admin, 'initializeApp');

      myFunctions = require('../index');
      myFunctions = require('../index');

但是我得到一个错误:

1个失败

1)云功能        “之前”钩子:      TypeError:jest.mock不是函数

我在某个地方错了...但是我无法理解 感谢您的反馈

1 个答案:

答案 0 :(得分:1)

已解决...

mocking-es-and-commonjs-modules-with-jest-mock

有了清楚的了解
However, when using export with require we would see an error such as:

TypeError: ourCode is not a function

The CommonJS module does not understand the ES Module it is trying to require. This is easily fixed, by adding a .functionNameBeingExported to the require, which for a default export is default.

externalModule.js
const ourCode = () => 'result';
export default ourCode;

testSubject.js
const ourCode = require('./externalModule').default;
// use ourCode()