-
Notifications
You must be signed in to change notification settings - Fork 371
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
Jest testing failing with new import entry points #1465
Comments
I'll add to that with v10 the old import style will work. import * as firebase from 'firebase-admin';
export const getFirebaseApp = (): firebase.app.App => {
const app = firebase.initializeApp({
credential: firebase.credential.applicationDefault(),
});
return app;
}; 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 That I mean, literally every function imported from |
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 Problem 2
This is saying that you're using |
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)
|
@hiranya911 Thank you for finding this. A solution that covers more edge cases is here: I used the information provided from these links to create this: So far, I have |
Closing this since there's no concrete action items for this repo. |
Just worked around this myself using a trick from the linked jest issue:
|
Thanks @mikehardy ! |
@mikehardy Didn't work for me with firebase/auth. Maybe it's somehow different from firebase-admin? I'm getting: Jest encountered an unexpected token
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from '@firebase/auth';
^^^^^^
SyntaxError: Unexpected token 'export'
import { getAuth } from 'firebase/auth'; Perhaps this will still be fixed in jest 28 |
Firebase/auth and firebase admin are definitely different SDKs, that much I know. Thus is still working for me for admin SDK at least |
Awesome, thanks for adding this, this is the best solution so far. |
In case anyone else is having this issue, I tried @mikehardy's fix, but found that this messed with resolution of Ultimately, I was able to get past this by upgrading to the latest version of At time of writing, I'm now on |
I found a much simpler solution, just jest.config.ts moduleNameMapper: {
"^firebase-admin/app$":
"<rootDir>/node_modules/firebase-admin/lib/app/index.js",
"^firebase-admin/auth$":
"<rootDir>/node_modules/firebase-admin/lib/auth/index.js",
}, |
Did you a solution? |
I ended up adopting a solution from here: microsoft/accessibility-insights-web#5421 (comment) which is adding a custom resolver: // Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Taken from
// https://github.com/microsoft/accessibility-insights-web/pull/5421#issuecomment-1109168149
module.exports = (path, options) => {
const firebaseRegex = /^@?firebase/;
// Call the defaultResolver, so we leverage its cache, error handling, etc.
return options.defaultResolver(path, {
...options,
// Use packageFilter to process parsed `package.json` before the resolution (see https://www.npmjs.com/package/resolve#resolveid-opts-cb)
packageFilter: (pkg) => {
if (firebaseRegex.test(pkg.name)) {
delete pkg.exports;
delete pkg.module;
delete pkg.browser;
}
return pkg;
},
});
}; |
Hi @tettoffensive, |
Updating to Jest 28 helps :) |
Having this same issue in a monorepo with Stuck on importing a @font-face now :(
|
@tettoffensive, @Deliaz, Did you find any solution for this case? I'm having the exact same issue. |
Enviornment
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
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:
And the Jest test:
###- 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
The text was updated successfully, but these errors were encountered: