简体   繁体   中英

TypeError in "path" when running Firebase Functions Test

I'm writing a test for a google cloud function which'll write some information to a firestore database. The test uses firebase-functions-test and jest. The function I'm writing works successfully when I deploy it but when I try to run the test I get:

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object

      at GrpcClient.loadProto (node_modules/google-gax/src/grpc.ts:182:23)
      at new FirestoreClient (node_modules/@google-cloud/firestore/build/src/v1/firestore_client.js:113:32)
      at ClientPool.Firestore._clientPool.pool_1.ClientPool [as clientFactory] (node_modules/@google-cloud/firestore/build/src/index.js:319:26)
      at ClientPool.acquire (node_modules/@google-cloud/firestore/build/src/pool.js:81:35)
      at ClientPool.run (node_modules/@google-cloud/firestore/build/src/pool.js:155:29)
      at Firestore.request (node_modules/@google-cloud/firestore/build/src/index.js:885:33)
      at WriteBatch.commit_ (node_modules/@google-cloud/firestore/build/src/write-batch.js:450:14)

My function:

const admin = require('firebase-admin');
const functions = require('firebase-functions');
const db = admin.firestore();

const saveCheckup = functions.pubsub.topic('save-test').onPublish((message) => {
  const {url, ...dataToSave} = message.attributes;

  let current = db.collection('current').doc(url);
  current.set(dataToSave, {merge: true})

  return true;
});

module.exports = saveCheckup;

My test:

import * as admin from 'firebase-admin';

const testEnv = require('firebase-functions-test')(
  {
    databaseURL: "https://my-project.firebaseio.com",
    projectId: 'my-project',
    storageBucket: 'my-project.appspot.com'
  }, "./my-project-firebase-adminsdk.json"
);


describe('saveCheckup', () => {
  let adminStub, saveCheckup;

  beforeAll(() => {
    adminStub = jest.spyOn(admin, "initializeApp");
    saveCheckup = require('../functions/save_checkup');
  });

  afterAll(() => {
    adminStub.mockRestore();
    testEnv.cleanup();
    admin.database().ref("current").remove();
  });

  it("should save the user", async () => {
    const wrapped = testEnv.wrap(saveCheckup);

    await wrapped({attributes: {
      date: "test date",
      url: "testurl",
      status: "200"
    }});

    const record = await admin.database().ref('/current/testurl').once('value');
    expect(record.val()).toHaveProperty("status", "200");
  })
});

Update: We were not able to solve this problem and ended up just writing offline tests for firestore instead.

The error output you posted shows that the error is within the Google Firebase node module files. It even shows the line and character location:

at new FirestoreClient (node_modules/.../v1/firestore_client.js:113:32)
//                                             Error location here ^

If you are trying to deploy locally please read this: https://firebase.google.com/docs/hosting/deploying and follow the directions according to your situation.

Add a "jest.config.js" into the root directory keep the following code into the file

module.exports = {
    testPathIgnorePatterns: ['lib/', 'node_modules/'],
    moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
    testEnvironment: 'node'
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM