I can't test firebase functions because of `TypeError: Cannot read properties of undefined (reading 'DataSnapshot')`

Viewed 40

I would like to run unit tests on firebase functions. I created functions/test/index.test.js with the intention of testing in offline mode, and the current file content is as follows:

// The content is only one line.
const test = require("firebase-functions-test")();

When I run jest with the above file, I get the following error:

$ jest
 FAIL  test/index.test.js
  ● Test suite failed to run

    TypeError: Cannot read properties of undefined (reading 'DataSnapshot')

    > 1 | const test = require("firebase-functions-test")();
        | ^
      2 |

      at Object.<anonymous> (node_modules/firebase-functions-test/lib/cloudevent/generate.js:26:45)
      at Object.<anonymous> (node_modules/firebase-functions-test/lib/v2.js:25:20)
      at Object.<anonymous> (node_modules/firebase-functions-test/lib/main.js:26:14)
      at Object.<anonymous> (node_modules/firebase-functions-test/lib/features.js:4:16)
      at Object.<anonymous>.module.exports (node_modules/firebase-functions-test/lib/index.js:30:20)
      at Object.<anonymous> (test/index.test.js:1:1)

Something does not seem to be set correctly before the test error. What should I do?

1 Answers

I ran npm ls firebase-functions and observed the following warning.

functions@ /path/to/project/functions
├─┬ firebase-functions-test@2.4.0
│ └── firebase-functions@3.20.0 deduped invalid: ">=3.23.0" from node_modules/firebase-functions-test
└── firebase-functions@3.20.0 invalid: ">=3.23.0" from node_modules/firebase-functions-test

npm ERR! code ELSPROBLEMS
npm ERR! invalid: firebase-functions@3.20.0 /path/to/project/functions/node_modules/firebase-functions

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/my_home/.npm/_logs/2022-09-18T09_24_27_208Z-debug-0.log

So I ran yarn add firebase-functions@3.23.0. Then, when I run the test, I no longer get the error that I got when I asked the question. Apparently, it was a version compatibility issue. Thank you.

Related