firebase-admin-node: Jest testing failing with new import entry points
Enviornment
- Operating System version: Mac OS 11.6
- Firebase SDK version: 10.0.0
- Firebase Product: auth, firestore, (probably all)
- Node.js version: 16.0.0
- NPM version: 8.0.0
Unable to use new imports with Jest for testing
Unable to use new imports with Jest for testing. When using Jest on a very, very simple project we get one error. On a more complex project, ESM errors start arriving.
I’m pretty sure all this is around Babel and how it is not grabbing the correct setup around how this lib exposed the experimental ESM syntax.
###- Problem One
Jest fails on a very simple project.
Error
FAIL src/example.test.ts
● Test suite failed to run
Cannot find module 'firebase-admin/app' from 'src/example.ts'
Require stack:
src/example.ts
src/example.test.ts
> 1 | import { initializeApp, applicationDefault, App } from 'firebase-admin/app';
| ^
2 | import {
3 | getFirestore,
4 | Firestore,
at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:322:11)
at Object.<anonymous> (src/example.ts:1:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 2.489 s
Steps to reproduce:
Created a new project. Steps VERY basic:
mkdir fail-example
cd fail-example
npm init -y
npx tsc --init
npm i -D jest ts-jest @types/jest
npm i firebase-admin
I’ve created a function:
import { initializeApp, applicationDefault, App } from 'firebase-admin/app';
export const getFirebaseApp = (): App => {
const app = initializeApp({
credential: applicationDefault(),
});
return app;
};
And the Jest test:
import { getFirebaseApp } from './example';
describe('Testing firebase-admin imports', () => {
it('Should import a firebase instance without a problem', () => {
const result = getFirebaseApp();
expect(result).toBeDefined();
});
});
###- Problem Two
I have a MUCH more complex project using nrwl/nx
for mono-repo support.
My tsconfig files and overall config are more complex.
This starts to fail around ES Modules
error
FAIL server-database libs/server/database/src/lib/firebase.spec.ts
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/Users/brian/Development/yet-another-project/graphql-core/node_modules/firebase-admin/lib/auth/index.d.ts:22
import { App } from '../app/index';
^^^^^^
SyntaxError: Cannot use import statement outside a module
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 11
- Comments: 18 (3 by maintainers)
Just worked around this myself using a trick from the linked jest issue:
yarn add jest-node-exports-resolver -D
coupled withresolver: 'jest-node-exports-resolver',
injest.config.js
, a happy surprise it was so easy, thanks to https://github.com/k-g-a/jest-node-exports-resolverI found a much simpler solution, just
moduleNameMapper
the issue. In my case since I was only using the app and auth submodules, so I made these changes:jest.config.ts
@mikehardy Didn’t work for me with firebase/auth. Maybe it’s somehow different from firebase-admin? I’m getting:
Perhaps this will still be fixed in jest 28
I’ll add to that with v10 the old import style will work.
That works as expected.
For the heck of it, I also went through all the JEST steps to set up ES module support. When I did that, the error vanished. HOWEVER, every import returned
undefined
.That I mean, literally every function imported from
firebase-admin
was undefined when usingnode --experimental-vm-modules
and setting upts-jest
using ESM as well.@tettoffensive, @Deliaz, Did you find any solution for this case? I’m having the exact same issue.
@hiranya911 Thank you for finding this. It does seem that Jest 28 will include proper resolving.
A solution that covers more edge cases is here: https://github.com/k-g-a/jest-node-exports-resolver
I used the information provided from these links to create this: https://gist.github.com/Brian-McBride/ab81588f1278aa7708ca3a36c01d8d49
So far, I have
firebase-admin
working in Nx monorepo.