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)
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"
}
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
@dart200 Sorry to hear about your troubles. Have you seen this on a clean install (with package-lock.json and node_modules removed)?
If that doesn't help, you might have to upgrade jest to help with this problem. See https://github.com/timarney/react-app-rewired/issues/170
@schmidt-sebastian I think, I have the same problem.
My environment:
My package dependencies:
"dependencies": {
"firebase-admin": "8.9.0",
"jest": "^24.9.0",
"jest-circus": "^24.9.0",
"jest-junit": "10.0.0"
}
I try to access firestore using firebase admin SDK in a jest test and get this error -- TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object
console.error internal/process/next_tick.js:68
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object
at validateString (internal/validators.js:125:11)
at Object.basename (path.js:1289:5)
at GrpcClient.loadProto (/Users/5tarlxrd/mytest/node_modules/google-gax/src/grpc.ts:166:23)
at new FirestoreClient (/Users/5tarlxrd/mytest/node_modules/@google-cloud/firestore/build/src/v1/firestore_client.js:113:32)
at ClientPool.Firestore._clientPool.pool_1.ClientPool [as clientFactory] (/Users/5tarlxrd/mytest/node_modules/@google-cloud/firestore/build/src/index.js:329:26)
at ClientPool.acquire (/Users/5tarlxrd/mytest/node_modules/@google-cloud/firestore/build/src/pool.js:87:35)
at ClientPool.run (/Users/5tarlxrd/mytest/node_modules/@google-cloud/firestore/build/src/pool.js:164:29)
at Firestore.request (/Users/5tarlxrd/mytest/node_modules/@google-cloud/firestore/build/src/index.js:924:33)
at WriteBatch.commit_ (/Users/5tarlxrd/mytest/node_modules/@google-cloud/firestore/build/src/write-batch.js:450:14)
at process._tickCallback (internal/process/next_tick.js:68:7)
I tried a clean install (with package-lock.json and node_modules removed), but it didn't help for me.
This worked for me: https://stackoverflow.com/a/56862810/1052033
Update: it's also here:
https://github.com/netlify/netlify-lambda/issues/112#issuecomment-493037644
https://github.com/netlify/netlify-lambda#webpack-configuration
We have followed up with Facebook internally. We believe the root cause is https://github.com/facebook/jest/issues/2549
Unfortunately, there is not much that we can do from our end at this point.
I don't think it's only on Jest side, imho the Firebase team should look into it too.
I'm not having the issue in the context of tests, but in the context of lambda functions.
@antoinerousseau can you share your code, package.json, etc? Can you repro the issue locally?
I can reproduce both locally and on Netlify.
The relevant parts of my package.json:
{
"dependencies": {
"@babel/preset-typescript": "^7.8.3",
"@types/aws-lambda": "^8.10.44",
"@types/node": "^13.7.4",
"@types/react": "^16.9.22",
"@types/react-dom": "^16.9.0",
"dotenv": "^8.2.0",
"firebase": "^7.9.1",
"firebase-admin": "^8.9.2",
"http-proxy-middleware": "^1.0.0",
"netlify-lambda": "^1.6.3",
"react": "^16.12.0",
"react-app-rewired": "^2.1.5",
"react-dom": "^16.12.0",
"react-scripts": "^3.4.0",
"typescript": "^3.8.2",
},
"devDependencies": {
"@hot-loader/react-dom": "^16.11.0",
"react-app-rewire-hot-loader": "^2.0.1",
"react-hot-loader": "^4.12.19"
},
"scripts": {
"start": "react-app-rewired start",
"lambda": "netlify-lambda serve src/functions",
"build": "react-app-rewired build && netlify-lambda build src/functions",
},
}
This allows me to write my Netlify/Lambda functions in TypeScript in src/functions, which works great except for that Firebase error.
Using the workaround I posted fixes it both locally and on Netlify, i.e. adding webpack-node-externals, adding a webpack.functions.js and changing my scripts like this:
"lambda": "netlify-lambda serve src/functions --config webpack.functions.js",
"build": "react-app-rewired build && netlify-lambda build src/functions --config webpack.functions.js",
My functions only import dotenv and firebase-admin.
Using Node v10.
Note that I have a src/functions/.babelrc to make my TS transpile to JS, as instructed here.
@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.
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.
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'
};
Hi @ankittater
Works for me perfectly now. Thank you so very much!
@ankittater thanks, this solved the issue for me as well !
@dart200 if you want to remove the unclosed handle warning from Jest, you need to close the connection to Firebase:
// Assuming you created Firebase this way:
fire.initializeApp(config);
// Destroy the object that way:
await fire.app().delete()
@schmidt-sebastian is there any actionable steps regarding this issue on our side? Or is it more of an issue with Jest?
We haven't been able to fully verify, but as far as I know, this type of issue only occurs in combination with Jest.
Alternatively, simply use jest --env=node
Closing this as we have no other updates at this point. Feel free to ping us to re-open.
Most helpful comment
Adding a jest.config.js with the following code will solve the issue