Repository navigation
Closed
Labels
Description
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-adminI'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
Reacted by Cristian David Ippolito Carrillo, joonhok-fittube, MG, andreas, Carsten Meyer, Bence Nemeth, Arif Ikhsanudin, fresh, Yana An, (Rew) Andrew McLagan and 1 more
Activity
Brian-McBride commentedon Oct 15, 2021
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-adminwas undefined when usingnode --experimental-vm-modulesand setting upts-jestusing ESM as well.hiranya911 commentedon Oct 15, 2021
Problem 1
Probably has to do something with how jest resolves modules. The same exact code works fine with Mocha. Do you know anything about how jest module resolution works, or how we can configure it? As far as our library is concerned, we declare both
exportsand thetypeVersionsin ourpackage.jsonfile as required by NPM. It seems Jest just ignores all that, and only looks at themainmodule, which is still set to the oldfirebase-adminentry point.Problem 2
This is saying that you're using
importin a file that is not recognized as a module. You will have to setup your project accordingly. See Node.js docs: https://nodejs.org/api/packages.html#packages_modules_packageshiranya911 commentedon Oct 15, 2021
I believe this is related: jestjs/jest#9771
Basically their resolver doesn't support multiple module entry points of dependencies. But it looks like they have plans to address it. In the meantime you will have to use the old namespaced API, or implement one of the workarounds mentioned in the above issue.
Update: FWIW, I tried the workaround discussed in the following comment, and it worked for me: jestjs/jest#9771 (comment)
Brian-McBride commentedon Oct 15, 2021
@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-adminworking in Nx monorepo.hiranya911 commentedon Oct 21, 2021
Closing this since there's no concrete action items for this repo.
mikehardy commentedon Oct 21, 2021
Just worked around this myself using a trick from the linked jest issue:
yarn add jest-node-exports-resolver -Dcoupled 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-resolveryairopro commentedon Dec 2, 2021
Thanks @mikehardy !
tettoffensive commentedon Apr 15, 2022
@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
mikehardy commentedon Apr 15, 2022
Firebase/auth and firebase admin are definitely different SDKs, that much I know. Thus is still working for me for admin SDK at least
why-not-try-calmer commentedon May 5, 2022
Awesome, thanks for adding this, this is the best solution so far.
chiubaka commentedon Jun 4, 2022
In case anyone else is having this issue, I tried @mikehardy's fix, but found that this messed with resolution of
ts-jestfor some reason. Might have something to do with my monorepo setup.Ultimately, I was able to get past this by upgrading to the latest version of
jestandts-jest. Seems like newer version ofjesthave updates to the resolver (or dependency resolver libraries) that fix this issue.At time of writing, I'm now on
jestv28.1.0 andts-jestv28.0.4. When I was having issues, I was onjestv26.6.3 andts-jestv26.4.3.TheCeloReis commentedon Jun 10, 2022
I found a much simpler solution, just
moduleNameMapperthe issue. In my case since I was only using the app and auth submodules, so I made these changes:jest.config.ts
iankitverma commentedon Jun 16, 2022
Did you a solution?
unshame commentedon Jul 1, 2022
I ended up adopting a solution from here: microsoft/accessibility-insights-web#5421 (comment) which is adding a custom resolver:
Deliaz commentedon Jul 17, 2022
Hi @tettoffensive,
I am having the same issue and none of the advice listed above helped so far.
May I know if you happened to find a solution?
IchordeDionysos commentedon Jul 29, 2022
Updating to Jest 28 helps :)
faces-of-eth commentedon Aug 1, 2022
Having this same issue in a monorepo with
Next.js 12.2.3,typescript 4.7.4,jest 28.1.3, andfirebase 9.9.1. Tried all above things, and I get to a point where it clears the issues with firebase using the custom resolvers posted above.Stuck on importing a @font-face now :(
pasindu-pr commentedon Feb 3, 2023
@tettoffensive, @Deliaz, Did you find any solution for this case? I'm having the exact same issue.
tran-simon commentedon Jul 4, 2025
I had a similar issue with firebase/auth and firebase/firestore
Adding this to my jest.config.js fixed the issue: