firebase / firebase-admin-node

Firebase Admin Node.js SDK
https://firebase.google.com/docs/admin/setup
Apache License 2.0
1.64k stars 372 forks source link

'@firebase/database-compat/standalone' import error in jest tests #2091

Open igorgarbuz opened 1 year ago

igorgarbuz commented 1 year ago

Environment

Describe the problem After updating: firebase-functions v3 -> v4 firebase-admin v11.3 -> v11.5 firebase-functions-test v0.3.0 -> v3.0.0

Any jest test using any firebase library will fail with the following error:

  ● Test suite failed to run

    Cannot find module '@firebase/database-compat/standalone' from '../../node_modules/firebase-admin/lib/database/index.js'

    Require stack:
      /Users/igor/Dev/vogel_database/node_modules/firebase-admin/lib/database/index.js
      /Users/igor/Dev/vogel_database/node_modules/firebase-functions/lib/common/providers/database.js
      /Users/igor/Dev/vogel_database/node_modules/firebase-functions/lib/v1/providers/database.js
      /Users/igor/Dev/vogel_database/node_modules/firebase-functions/lib/v1/index.js
      /Users/igor/Dev/vogel_database/node_modules/firebase-functions-test/lib/v1.js
      /Users/igor/Dev/vogel_database/node_modules/firebase-functions-test/lib/main.js
      /Users/igor/Dev/vogel_database/node_modules/firebase-functions-test/lib/features.js
      /Users/igor/Dev/vogel_database/node_modules/firebase-functions-test/lib/index.js
     path/to/the/test/file.test.ts

Call stack in the example above may differ depending on the firebase library used, but will alway end at Users/igor/Dev/vogel_database/node_modules/firebase-admin/lib/database/index.js with the same error for importing '@firebase/database-compat/standalone'.

Steps to reproduce:

Install libraries with the provided versions. Run jest <any.file.test.ts> where test file uses at least one firebase library that has an internal reference to firebase-admin.

google-oss-bot commented 1 year ago

I found a few problems with this issue:

lahirumaramba commented 1 year ago

Hey @igorgarbuz , could you share a code sample to reproduce this? I am curious to see what your imports look like.

igorgarbuz commented 1 year ago

It's sufficient to have only one import referring to firebase-admin. For example import FFTest from 'firebase-functions-test'; is the only line in a file <filename>.test.ts. When the file is run npx jest <filename>.test.ts there is an error :

    Cannot find module '@firebase/database-compat/standalone' from '../../node_modules/firebase-admin/lib/database/index.js'

    Require stack:
      <path>/node_modules/firebase-admin/lib/database/index.js
      <path>/node_modules/firebase-functions/lib/common/providers/database.js
      <path>/node_modules/firebase-functions/lib/v1/providers/database.js
      <path>/node_modules/firebase-functions/lib/v1/index.js
      src/config.ts
      src/mocks/handlers.ts
      src/mocks/server.ts
      jest.setup.ts
igorgarbuz commented 1 year ago

Actually my previous example was not accurate. Even if there is an empty test file, there is still en error because there is an import import * as functions from 'firebase-functions'; in src/config.ts used by jest.setup.ts and run before the test. If there is not import of firebase-functions in src/config.ts, there is still an error from firebase-functions-test:

 FAIL  <filename>.test.ts
  ● Test suite failed to run

    Cannot find module '@firebase/database-compat/standalone' from '../../node_modules/firebase-admin/lib/database/index.js'

    Require stack:
      <path>/node_modules/firebase-admin/lib/database/index.js
      <path>/node_modules/firebase-functions/lib/common/providers/database.js
     <path>/node_modules/firebase-functions/lib/v1/providers/database.js
      <path>/node_modules/firebase-functions/lib/v1/index.js
      <path>/node_modules/firebase-functions-test/lib/v1.js
      <path>/node_modules/firebase-functions-test/lib/main.js
      <path>/node_modules/firebase-functions-test/lib/features.js
      <path>node_modules/firebase-functions-test/lib/index.js
      <filename>.test.ts

imports in <filename>test.ts look like

import path from 'path';
import { DocumentReference, DocumentSnapshot } from '@firebase/firestore-types';
import FFTest from 'firebase-functions-test';
import 'html-validate/jest';
import { v4 as uuidv4 } from 'uuid';
andresfelipe9619 commented 1 month ago

@igorgarbuz did u manage to solve this? this would be very helpful, thanks