firebase-admin-node: [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of Object

Environment:

  • Operating System version: macOS 10.15.3
  • Firebase SDK version: firebase-admin 8.0.0 - 8.9.2
  • Firebase Product: Firestore
  • Node.js version: 12.16.0
  • NPM version: 6.13.4

Describe the problem

I noticed this issue when trying to upgrade my admin-sdk from 7.2.0 to something that does not use the legacy metadata endpoints.

Trying to use the admin sdk to access firestore in a jest test causes this error – TypeError: The “path” argument must be of type string. Received an instance of ObjectTypeError [ERR_INVALID_ARG_TYPE]: The “path” argument must be of type string. Received an instance of Object

I do not notice this on my cloud functions deployment or any non jest scripts I’ve tried.

The stack trace is as follows:

TypeError: The "path" argument must be of type string. Received an instance of ObjectTypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of Object

  11 | describe('testing', () => {
  12 |   it ('does the thing', async () => {  
> 13 |     const doc = await adminDb.doc('movers/gea8cKBpFunx5VdE0lRw').get();
     |                 ^
  14 |     expect(doc.exists).toBe(true);
  15 |     console.log(doc.data());
  16 |   });

  at GrpcClient.loadProto (node_modules/google-gax/src/grpc.ts:166:23)
  at new FirestoreClient (node_modules/@google-cloud/firestore/build/src/v1/firestore_client.js:113:32)
  at ClientPool.clientFactory (node_modules/@google-cloud/firestore/build/src/index.js:329:26)
  at ClientPool.acquire (node_modules/@google-cloud/firestore/build/src/pool.js:87:35)
  at ClientPool.run (node_modules/@google-cloud/firestore/build/src/pool.js:164:29)
  at node_modules/@google-cloud/firestore/build/src/index.js:957:30
  at Firestore._retry (node_modules/@google-cloud/firestore/build/src/index.js:822:38)
  at Object.<anonymous> (test.js:13:17)

Relevant Code:

This template replicates the problem for me:

require('jest');
jest.setTimeout(20000);

const admin = require('firebase-admin');
admin.initializeApp({
  credential: admin.credential.cert(require("./<keyfile>.json")),
  databaseURL: "https://<project>.firebaseio.com",
});
const adminDb = admin.firestore();

describe('testing', () => {
  it ('does the thing', async () => {  
    const doc = await adminDb.doc('<collection>/<id>').get();
    expect(doc.exists).toBe(true);
    console.log(doc.data());
  });
});

My package dependencies are just:

"dependencies": {
  "firebase-admin": "^8.9.2",
  "jest": "^25.1.0"
}

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 17
  • Comments: 19 (7 by maintainers)

Most upvoted comments

Adding a jest.config.js with the following code will solve the issue

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

Hi @ankittater, thank you so much for that.

My tests run almost perfectly now. There’s a small issue with jest complaining about open handles that didn’t exist, and then hanging for an extra minute or two, but all the test work as intended, and I can tell if a regression happened.

I’d also like to point out for anyone else, the key line for me was this:

testEnvironment: 'node'

hi @ankittater where to create this file.

add a file jest.config.js into root of the project and add the following lines into that file

module.exports = { testEnvironment: ‘node’ };

Alternatively, simply use jest --env=node

@antoinerousseau the issues with FirebaseAdmin+Webpack are well-known, and that situation is unlikely to change anytime soon. You will have to continue using your workaround.

The issue reported here is more fundamental though, as it can be easily reproduced without any bundler or other framework.