Mick Sear
Mick Sear

Reputation: 1566

Unable to authenticate against Firestore emulator

I'm using Firebase admin with Node + Express to update Firestore documents from Appengine. I have some tests that I want to have using the Firestore emulator. Here's the error I'm getting:

Error in post /requests Error: {"servicePath":"localhost","port":8080,
"clientConfig":{},"fallback":true,"sslCreds":{"callCredentials":{}},
"projectId":"test-project","firebaseVersion":"9.4.1","libName":"gccl",
"libVersion":"4.7.1 fire/9.4.1","ssl":false,
"customHeaders":{"Authorization":"Bearer owner"},"scopes":[
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/datastore"
]}
You need to pass auth instance to use gRPC-fallback client in browser. Use OAuth2Client from google-auth-library.

Before each test I'm calling:

var serviceAccount = require(process.env.FIREBASE_SA_CREDENTIAL);
firebaseAdmin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    projectId: 'test-project'
});

And the test is using a class that simply accesses Firestore like this:

this.db = firebaseAdmin.firestore();
...

I've got the following npm dependencies:

"@google-cloud/firestore": "^4.7.1",
"firebase": "^8.0.2",
"firebase-admin": "^9.4.1"
"firebase-tools": "^8.16.2"

I'm starting the emulators and running tests with:

firebase emulators:exec 'jest --verbose=false'

I can't see what's incorrect in the config - As far as I can see, the emulator should accept all auth. The error message suggests it's using some frontend library rather than a backend library, but the dependencies all appear to be correct.

Upvotes: 1

Views: 332

Answers (1)

Mick Sear
Mick Sear

Reputation: 1566

OK, solved. There was an important line missing from jest config:

module.exports = {
  testEnvironment: 'node'
}

testEnvironment: 'node'. This was triggering some behaviour in the Firebase libs that made them think the environment was a browser.

Upvotes: 6

Related Questions