Reputation:
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
Upvotes: 5
Views: 15206
Reputation:
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()
Upvotes: 3