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

482
Visualizações
Jest testing fails with error code ERR_UNHANDLED_REJECTION

Why do I keep receiving this error code ERR_UNHANDLED_REJECTION ?

Test case:

beforeEach(() => {
  jest.resetModules()
})

test('A class should be already initialized at first call', async () => {
  jest.doMock('@google-cloud/firestore', () => class {
    collection () {}
  })

  const Statistics = require('./index')

  expect(Statistics.initialized).toBe(true)
})

And the class module:

const Firestore = require('@google-cloud/firestore')
const Database = new Firestore()

class Statistics {
  constructor () {
    this.initialized = false

    // Default field values
    this.fields = {
      products: {
        count: 0,
        published: 0,
        updatedAt: new Date()
      }
    }

    this.init()
  }

  async init () {
    const fetched = []

    await Database.collection('statistics').get().then(snapshot => {
      // Check if snapshot is completely empty
      if (snapshot.docs.length === 0) {
        // Create new document
        Object.keys(this.fields).forEach(key => {
          Database.collection('statistics').doc(key).set(this.fields[key])
          fetched[key] = this.fields[key]
        })
      } else {
        snapshot.forEach(doc => {
          fetched[doc.id] = doc.data()
        })
      }
    })

    // Setting fetched data to the fields (synchronous)
    // ...

    this.initialized = true
  }
}

module.exports = new Statistics()

Error comes from the line where I'm retrieving the data from Firestore: await Database.collection('statistics').get()...

about 4 years ago · Juan Pablo Isaza
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