Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

381
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda