简体   繁体   中英

Is jest.mock() equivalent to sinon.stub()

In all examples testing of Firebase SDK for Cloud Functions Quickstart, Mocha/Chai and Sinon are used... Trying to use Jest instead , I wonder what is the correct equivalent of sinon.stub() ?

I tried the following (only for testing jest.mock() acceptance...

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');

but I get an error :

1 failing

1) Cloud Functions "before all" hook: TypeError: jest.mock is not a function

I wrong somewhere ... but I cannot get it thanks for feedback

SOLVED ...

got a clear understanding from 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM