firebase / firebase-functions-test

MIT License
232 stars 48 forks source link

Basic online test case doesn't work #45

Closed garrettg123 closed 4 years ago

garrettg123 commented 5 years ago

Version info

firebase-functions-test: 0.1.6

firebase-functions: 3.2.0

firebase-admin: 8.2.0

Test case

import path from 'path'

// Set up Firebase Functions Test
const settings = {
  databaseURL: 'https://X.firebaseio.com',
  storageBucket: 'X.appspot.com',
  projectId: 'X',
}
const serviceAccountKeyPath = path.resolve(
  __dirname,
  'firebase-adminsdk-service-account-key.json'
)
const firebaseFunctionsTest = require('firebase-functions-test')(
  settings,
  serviceAccountKeyPath
)

const testFunction = firebaseFunctions.https.onRequest(
  async (request, response) => {
    try {
      console.log('logged')

      await firebaseAdmin
        .firestore()
        .doc('tests/test')
        .set({ foo: 'bar' })

      console.log('never logged')

      return response.send()
  }
)

describe('testFunction()', () => {
  afterAll(() => {
    firebaseFunctionsTest.cleanup()
  })

  it('calls send', async () => {
    const request = {}
    const send = jest.fn()
    const response = {
      send,
    }

    try {
      await testFunction(request, response)
    } catch (err) {
      console.log(err)
    }

    expect(send).toBeCalled()
  })
})

Steps to reproduce

  1. Run the test case either with jest or firebase emulators:exec 'jest'

Expected behavior

Test should pass

Actual behavior

Test doesn't pass and no error is logged.

garrettg123 commented 5 years ago

Talk about support for a paid product :P

laurenzlong commented 4 years ago

Sorry for the late response! This is still an experimental package (that's why the version number starts with 0) But that said, waiting multiple months is still not right, so apologies for that.

Looking at your code, you have

    try {
      console.log('logged')

      await firebaseAdmin
        .firestore()
        .doc('tests/test')
        .set({ foo: 'bar' })

      console.log('never logged')
      return response.send()
  }

You have a try but not a catch so if there were any errors, they wouldn't be logged.