admin管理员组

文章数量:1398787

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

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

Share Improve this question edited Dec 4, 2018 at 21:07 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Dec 4, 2018 at 8:47 user762579user762579
Add a ment  | 

1 Answer 1

Reset to default 3

SOLVED ...

got a clear understanding from mocking-es-and-monjs-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()

本文标签: javascriptIs jestmock() equivalent to sinonstub()Stack Overflow