Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

293
Visualizações
How to use nock in order to mock Firebase/Firestore in NodeJS?

I have a REST API which written in NodeJS. The REST API gets a file and information and then uploads the file to the drive and stores the information inside my Firebase/Firestore DB. I'm trying to write tests in order to test my REST API, so I need to mock Firestore. I already mock the google-drive using nock by tracking the HTTPS requests that are made by googleapis. Now I'm trying to do the same thing with Firestore.

I tried different methods like, for example, for authentication I tried (based on the docs):

nock('https://firestore.googleapis.com/$discovery/rest')
.persist()
.post('')
.reply(200, { 'access_token': 'abc', 'refresh_token': '123', 'expires_in': 10 })

But nock does not follow the requests. How do I use nock to track the following code? Does firebase package actually makes requests to the API?

const firebase = require("firebase");

// Required for side-effects
require("firebase/firestore");

// First code - authenatiocation
firebase.initializeApp(firebaseConfig);
db = firebase.firestore();

// ...
// Second code - Get data from DB
const snapshot = await db.collection(collection).orderBy("timestamp", "desc").get();

// ...
// Third code - upload new report
const response = await db.collection(collection).doc(document_id).set(data);

If it's not possible with nock, then it can be achieved?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It could be argued that stubbing the Firebase methods would make more sense than mocking any API calls the library might internally make. The latter assumes knowledge about the internal functioning of the library and doesn't make full use of the benefits of abstraction.

You could use sinon to stub Firebase methods. Something like,

db = firebase.firestore();

const stub = sinon.stub(db, 'collection')
  .returns({
    orderBy: sinon.stub().returns({
      get: sinon.stub().returns(yourDocObjectHere)
    }),
    doc: sinon.stub().returns({
      set: sinon.stub().returns(someObjectHere)
    })    
  })

and then assert like

sinon.assert.calledWith(db.collection.orderBy, 'timestamp', 'desc');
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda