Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Chai is a commonly used library for creating unit test suites. It is easily extended with plugins.
- import { assert } from 'chai';
- // Sinon is a library used for mocking or verifying function calls in JavaScript.
- import * as sinon from 'sinon';
- // Require firebase-admin so we can stub out some of its methods.
- import * as admin from 'firebase-admin';
- // Require and initialize firebase-functions-test. Since we are not passing in any parameters, it will
- // be initialized in an "offline mode", which means we have to stub out all the methods that interact
- // with Firebase services.
- import * as _test from 'firebase-functions-test';
- const firebaseTest = _test();
- describe('Cloud Functions', () => {
- let adminInitStub;
- // @ts-ignore
- before(() => {
- // [START stubAdminInit]
- // If index.js calls admin.initializeApp at the top of the file,
- // we need to stub it out before requiring index.js. This is because the
- // functions will be executed as a part of the require process.
- // Here we stub admin.initializeApp to be a dummy function that doesn't do anything.
- adminInitStub = sinon.stub(admin, 'initializeApp');
- // [END stubAdminInit]
- });
- // @ts-ignore
- after(() => {
- // Restore admin.initializeApp() to its original method.
- adminInitStub.restore();
- // Do other cleanup tasks.
- firebaseTest.cleanup();
- // Cleaning up env
- process.env.FUNCTION_NAME = undefined;
- });
- describe('signUp', () => {
- process.env.FUNCTION_NAME = 'signUp';
- it('it should write all the default data', async () => {
- const firebaseObject = {
- firestore: () => ({
- collection: () => ({
- doc: () => {}
- }),
- batch: () => ({
- set: () => {},
- commit: () => Promise.resolve('ok')
- })
- })
- };
- adminInitStub.callsFake(() => firebaseObject);
- const userSignup = require('../src/userSignup');
- const user = {
- id: 'testId'
- };
- const wrapped = firebaseTest.wrap(userSignup);
- assert.equal(await wrapped(user), 'ok');
- });
- it('it should NOT write all the default data', async () => {
- const firebaseObject = {
- firestore: () => ({
- collection: () => ({
- doc: () => {}
- }),
- batch: () => ({
- set: () => {},
- commit: () => Promise.reject('error')
- })
- })
- };
- adminInitStub.callsFake(() => firebaseObject);
- const userSignup = require('../src/userSignup');
- const user = {
- id: 'testId'
- };
- const wrapped = firebaseTest.wrap(userSignup);
- assert.equal(await wrapped(user), 'error');
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement