Skip to content

Unable to create firestore() instance from firebase/testing initializeAdminApp #1630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dimroc opened this issue Mar 24, 2019 · 3 comments
Closed

Comments

@dimroc
Copy link

dimroc commented Mar 24, 2019

Describe your environment

  • Operating System version: OSX Mojave 10.14.3
  • Browser version: N/A
  • Firebase SDK version: 5.9.1 (testing is 0.7.7)
  • Firebase Product: testing and firestore

Describe the problem

Attempting to test cloud functions offline: https://firebase.google.com/docs/functions/unit-testing#initializing, but against the firestore local emulator. Narrowed down the issue to being firebase/testing being unable to invoke firestore()

TypeError: app.firestore is not a function

Steps to reproduce:

  1. npm install @firebase/testing --saveDev
  2. Create a test firebase.initializeAdminApp({projectId})
  3. Invoke app.firestore()

Should return an instance of firestore but throws.

Relevant Code:

https://stackblitz.com/fork/firebase-issue-sandbox

const firebase = require('@firebase/testing');

const createApp = () => {
  console.log('--- init admin app')
  const rval = firebase.initializeAdminApp({projectId: 'projectId'})
  console.log('--- FINISHED init admin app ', rval)
  return rval
}

describe("firebasetesting broken", () => {
  it("when invoking firestore()", () => {
    const app = createApp()
    const fs = app.firestore()
    expect(1).toBe(1)
  })
})

Callstack

dimroc@DimitrisMBP2018: functions (chores/convert-rtdb-to-firestore)$ jest src/jseasyrepro.test.js
(node:74293) DeprecationWarning: grpc.load: Use the @grpc/proto-loader module with grpc.loadPackageDefinition instead
 FAIL  src/jseasyrepro.test.js
  firebasetesting broken
    ✕ when invoking firestore() (10ms)

  ● firebasetesting broken › when invoking firestore()

    TypeError: app.firestore is not a function

      3 | const createApp = () => {
      4 |   console.log('--- init admin app')
    > 5 |   const rval = firebase.initializeAdminApp({projectId: 'projectId'})
        |                         ^
      6 |   console.log('--- FINISHED init admin app ', rval)
      7 |   return rval
      8 | }

      at initializeApp (node_modules/@firebase/testing/src/api/index.ts:133:9)
      at Object.initializeAdminApp (node_modules/@firebase/testing/src/api/index.ts:103:10)
      at createApp (src/jseasyrepro.test.js:5:25)
      at Object.it (src/jseasyrepro.test.js:12:17)

  console.log src/jseasyrepro.test.js:4
    --- init admin app

originally found in typescript

import * as firebase from "@firebase/testing"

// tried all sorts of import side effects but shouldn't be needed
// because we're server side and the code is invoked inside firebase/testing.
// const Firestore = require('@google-cloud/firestore')
// import `firebase/firestore`

const createApp = () => {
  console.log('--- init admin app')
  const rval = firebase.initializeAdminApp({projectId: 'projectId'})
  console.log('--- FINISHED init admin app ', rval)
  return rval
}

describe("firebasetesting broken", () => {
  it("when invoking firestore()", () => {
    const app = createApp()
    const fs = app.firestore()
    expect(1).toBe(1)
  })
})

tsconfig.json

{
  "compilerOptions": {
    "lib": ["dom", "es2017", "es2018.promise"],
    "module": "commonjs",
    "target": "es2017",
    "typeRoots": ["node_modules/@types", "@types"],
    "noImplicitReturns": true,
    "esModuleInterop": true,
    "outDir": "lib",
    "sourceMap": true
  },
  "compileOnSave": true,
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "tmp",
    "node_modules"
  ]
}

package.json

{
  "name": "sb-cloudfunctions",
  "scripts": { ...},
  "main": "lib/index.js",
  "engines": {
    "node": "8"
  },
  "dependencies": {
    "@google-cloud/firestore": "^1.2.0",
    "@google-cloud/storage": "^2.4.3",
    "firebase-admin": "~7.1.1",
    "firebase-functions": "^2.2.1",
    "fs-extra": "^7.0.1",
    "sharp": "^0.22.0",
    "unfurl.js": "^3.0.1"
  },
  "devDependencies": {
    "@firebase/testing": "^0.7.7",
    "@types/jest": "^24.0.0",
    "@types/react": "^16.8.8",
    "firebase-functions-test": "^0.1.6",
    "firebase-tools": "^6.5.0",
    "jest": "^24.0.0",
    "ts-jest": "^24.0.0",
    "tslint": "~5.14.0",
    "typescript": "^3.3.4000",
    "uuid": "^3.3.2"
  },
  "private": true,
  "jest": {
    "collectCoverage": true,
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "json"
    ],
    "transform": {
      "^.+\\.ts?$": "ts-jest",
      "^.+\\.tsx?$": "ts-jest"
    },
    "testMatch": [
      "**/*.(test|spec).(ts|tsx|js)"
    ],
    "testPathIgnorePatterns": [
      "/node_modules/",
      "/tmp"
    ],
    "coveragePathIgnorePatterns": [
      "/node_modules/"
    ],
    "coverageReporters": [
      "json",
      "lcov",
      "text",
      "text-summary"
    ]
  }
}
@hsubox76
Copy link
Contributor

I tried to reproduce this by creating a fresh npm project, adding 2 files:

  • Your top example file (with require), naming it index.test.js
  • Your package.json

I did a yarn install and then ran jest, and the test passed. I had it console.log "fs" right before the "expect" line and it logged what seems to be a complete Firestore object. Am I missing a step?

@nicokaag
Copy link

nicokaag commented May 15, 2019

Just wanted to leave a comment for anyone encountering this issue (As I just had).
The problem might be fixed by upgrading your node version, and re-installing the packages.

First I tried installing with npm, didn't get any error, but got this error.
Then when I tried running yarn install, I got some errors and info messages about my node version. As soon as I updated to the latest version of node 8, I could install dependencies with both npm and yarn, and the test would run successful.

@stamler
Copy link

stamler commented May 16, 2019

This issue appears consistently when firebase-admin is a dependency even if no user code is run. I opened #1786 with steps to reproduce.

@firebase firebase locked and limited conversation to collaborators Oct 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants