firebase / firebase-functions-test

MIT License
232 stars 48 forks source link

Running unit tests ends with error message "The 'credential' property must be an object which implements the Credential interface" even the GOOGLE_APPLICATION_CREDENTIALS env var is set #92

Closed shuneihayakawa closed 3 years ago

shuneihayakawa commented 3 years ago

Version info

firebase-functions-test: 0.2.3 firebase-functions: 3.13.2 firebase-admin: 9.5.0

Test case

initializeApp({ credential: credential.applicationDefault(), });

export const makeUppercase = functionBuilder('asia-northeast1') .firestore.document('/messages/{documentId}') .onCreate(async (snapshot) => { const original = snapshot.data().original as string; const uppercase = original.toUpperCase(); await snapshot.ref.set({ uppercase }, { merge: true }); });

- unit test
  - this test checks whether the upper case message is added or not on firestore after cloud function is executed
```typescript
import test from 'firebase-functions-test';
import { firestore } from 'firebase-admin';
import { credential } from 'firebase-admin';

import { makeUppercase } from '../src/index';

const tester = test({
  credential: credential.applicationDefault(),
});

it('Tests the sample makeUppercase function', async (done) => {
  const db = firestore();

  const path = 'messages/data1';

  const original = 'original';
  const uppercase = original.toUpperCase();

  afterEach(async () => {
    tester.cleanup();
    await db.doc(path).delete();
  });

  const wrapped = tester.wrap(makeUppercase);
  const snapshot = tester.firestore.makeDocumentSnapshot(
    {
      original,
    },
    path
  );

  db.doc(path).onSnapshot(
    (snapshot) => {
      const data = snapshot.data();
      const result = data?.uppercase;
      if (result == undefined) {
        return;
      }
      expect(result).toEqual(uppercase);
    },
    (error) => {
      done(error);
    }
  );
  await wrapped(snapshot);
});

Steps to reproduce

Clone this repo and follow these steps

Actual behavior

louis030195 commented 3 years ago

Same issue, only happen when trying to use makeDocumentSnapshot yet

Yoshihide-Nishimoto commented 3 years ago

Same here

inlined commented 3 years ago

On one hand, I think I see a bug, but on the other, this code sample doesn't follow the SDK's instructions. If you're going to use a credentials file, you should pass that value as the second parameter to initialize your tests:

const tester = test({}, process.env.GOOGLE_APPLICATION_CREDENTIALS);