首页 > 解决方案 > 将 firebase-functions-test 与模拟器一起使用

问题描述

我正在尝试为我的 CI 创建一个自动化测试,将我的云功能与本地模拟器连接起来。我正在使用 firebase-functions-test 和 firebase-admin sdk。虽然我的测试在本地通过,但在使用测试 docker 容器时会失败。我创建了一个测试来说明。

const databaseName = 'foo';
const test = require('firebase-functions-test')({
  databaseURL: "http://localhost:9000?ns=" + databaseName
});
describe('test', () => {
  it('tests', async () => {
    const admin = require('firebase-admin')
    admin.initializeApp();
    return await admin.database().ref('bar').push('baz')
  })
})

上面的测试会反复发出警告:

@firebase/database: FIREBASE WARNING: {"code":"app/invalid-credential","message":"Credential implementation provided to initializeApp() via the \"credential\" property failed to fetch a valid Google OAuth2 access token with the following error: \"Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal metadata.google.internal:80. Error code: ENOTFOUND\"."}

我怀疑它与~/.config/gcloud/application_default_credentials.json,但我真的不明白为什么模拟器会检查 gcloud 凭据。

任何建议表示赞赏,在此先感谢您!

标签: firebase-realtime-databasegoogle-cloud-functionsfirebase-admin

解决方案


推荐阅读