【问题标题】:Is jest.mock() equivalent to sinon.stub()jest.mock() 是否等同于 sinon.stub()
【发布时间】:2018-12-04 08:47:43
【问题描述】:

在 Firebase SDK for Cloud Functions Quickstart 的所有示例测试中,都使用了 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 不是函数

我在某个地方错了……但我无法理解 感谢反馈

【问题讨论】:

    标签: javascript jestjs sinon


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 2019-06-16
      • 1970-01-01
      • 2017-11-29
      • 2010-11-30
      • 2017-01-20
      • 2016-02-12
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多