Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

375
Views
TypeError: firebase_admin_1.default.messaging is not a function

I am trying to write unit tests using jest and trying to mock firebase-admin. Here is my application code:

import logger from './logger';
import admin from 'firebase-admin';

class NotificationService {
  constructor() {
    admin.initializeApp({
      credential: admin.credential.cert({
        projectId: 'FCM_PROJECT_ID',
        privateKey: 'FCM_PRIVATE_KEY'
        clientEmail: 'FCM_CLIENT_EMAIL'
      }),
    });
  }

  public async send(message: TokenMessage): Promise<ApiResponse<string>> {
    try {
      await admin.messaging().send(message)
    } catch (e) {
      logger.error(e);
    }
  }
}

I am trying to mock the code like below:

jest.mock('firebase-admin');

But it is erroring out with:

firebase_admin_1.default.messaging is not a function

What is the solution?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Issue: not mocking the member variables and methods(messaging, send) of firebase-admin.

Fix:

jest.mock('firebase-admin', () => ({
  messaging: jest.fn().mockImplementation(() => {
    return {
      send: jest
        .fn()
        .mockReturnValue('message_id'),
    };
  }),
}));

Note: You would probably need to mock credential and initializeApp as well:

jest.mock('firebase-admin', () => ({
      credential: {
        cert: jest.fn(),
      },
      initializeApp: jest.fn(),
      messaging: jest.fn().mockImplementation(() => {
        return {
          send: jest
            .fn()
            .mockReturnValue('message_id'),
        };
      }),
    }));
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!