-
Notifications
You must be signed in to change notification settings - Fork 893
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
Upgrade to 9.2.0 gives error in jest unit tests #5687
Comments
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight. |
I got a similar error when using firebase in Sveltekit deployed on Vercel.
|
Note that you are loading the Nodejs bundle The root cause of the issue is this line, which we added to allow us to use |
Hi @Feiyang1 , well we actually have our tests for the front-end application (angular + angularFire + firebase) in the same location as our tests for the functions (firebase-admin). Probably that's indeed not a normal setup and this issue wouldn't occur if we would separate our tests? |
Your setup sounds fine to me as long as you are able to specify different configurations for your FE and BE tests respectively. Or are you saying you run both FE and BE tests as a single test run? Are you able to instruct the FE tests to use Is webpack (since you mentioned Angular) involved at all to bundle tests before running them in jest? if so, you can use the If you could provide a min repro, I can take a closer look and it will be much easier for me to find where the problem is. |
Try this 👍 https://github.com/tja4472/angularfire-jest-tests HTH |
Okay, thanks for the repro. It seems that external dependencies are not bundled for testing, and they are resolved using To do that, you need to define a custom resolver to tell Jest to resolve firebase packages using their browser bundles:
// my-module-resolve.js
module.exports = (request, options) => {
// Call the defaultResolver, so we leverage its cache, error handling, etc.
return options.defaultResolver(request, {
...options,
// Use packageFilter to process parsed `package.json` before the resolution (see https://www.npmjs.com/package/resolve#resolveid-opts-cb)
packageFilter: pkg => {
if(pkg.name.startsWith('@firebase')) {
return {
...pkg,
// Alter the value of `main` before resolving the package
main: pkg.esm5 || pkg.module,
};
}
return pkg;
},
});
};
module.exports = {
....
// set the value to the custom resolver created in step 1
resolver: '<rootDir>/my-module-resolve.js',
// browser bundles in firebase are ESM, transform them to CJS to work in Jest
transformIgnorePatterns: [
"<rootDir>/node_modules/(?!(@firebase.*)/)"
]
}; This should solve the You can also solve the problem by bundling tests with all external dependencies before running them in Jest, but that will probably slow down the tests significantly and beats the purpose of using Jest with Angular. I also want to point out that this could be an issue for any packages, not just Fireabase, that support both Nodejs and Browser, especially if they supply different bundles/implementations for them, so I think Jest's default strategy to use the @tja4472 There is another error I got from the repro, which the above workaround doesn't help - |
I've got the same issue, but since in tests we do not rely on the details of the implementation of the 3rd-party library (which in general considered bad test practice), we just mocked entire jest.mock("firebase/firestore", () => {
return {
getFirestore: jest.fn(),
};
}); |
Same issue as-well however I'm working with React and the solution above doesn't seem to work due to create-react-app's limitations. Thoughts? |
I know it is bad. But it fixed using |
After applying your proposed solution @Feiyang1 using the custom resolver, the original error
Does anyone else encounter this? |
// Edit The above error ( |
This this error too. Effectively preventing us from upgrading. |
This is a blocker for my team as well. We applied the same recommended module resolution fix, but received the "Cannot find module 'rxfire/auth' from..." error. Our project is using React. |
Okay I joined in on this and it has been driving me crazy as well, gals and guys I found a working solution. Jest cannot resolve scoped module names correctly sigh so instead you have to add extra rules for the modulenamemapper config to jest. I attempted to use the regex replacer to make a rule, but it did not work as is so I just wrote the rule for each module imported.
This is the final piece of config required for jest along with the original fix to make firebase/app imports work in jest, or at least prevent them from error-ing to allow you to mock the functions inside. P.S. For anyone using create-react-app you can make almost all of these changes without any issues in the package.json under jest: the only issue is that resolver is not currently an accepted key so it will have to be modified on their end or you will have to fork and add the capability of modifying it which is not a big change tbh but their release cycle is slow enough to make this a wait if we raise the issue and wait for a fix to that separate issue. |
@MatthewFallon, I had to add a few more:
and
Unfortunately I still get the error below and haven't figure out how to resolve it in the same manner: Cannot find module '../base/dist/emotion-styled-base.cjs.dev.js' from 'node_modules/@emotion/styled/dist/emotion-styled.cjs.dev.js' |
Can someone please try |
@Feiyang1 with |
@Feiyang1 Jest and cypress tests are working using [email protected] and the latest firebase-admin together in the same project. |
Yes great, I've just tested the released 9.5.0 and Jest is working. |
I think we need to wait until the final release of a new npm but this looks great, thanks. |
@richtera |
Wow even better! I missed that. Testing. Tested with the official release and it works!!!! :) |
I'm going to close the issue since the original issue has been addressed in 9.5.0. Feel free to open a new issue if you encounter any other problems. |
Hello, have you found a solution for devs using react |
I use React/CRA for my job. I have embraced #5687 (comment) above and put this function in jest.mock("firebase/firestore", () => {
return {
getFirestore: jest.fn(),
};
}); |
@mo-gbolahan As of right now, the issue that broke the loading of the jest tests is fixed by the firebase 9.6.0 release. If you update to the newest firebase version it will fix the issue entirely in cra, no changes needed. I would recommend following advice from @kevinashworth and mocking the firebase firestore methods, but the presence of firebase version 9 imports won't crash the jest test anymore if you update it. |
[REQUIRED] Describe your environment
[REQUIRED] Describe the problem
When upgrading to firebase 9.2.0 from 9.1.3 our jest unit tests start to fail with the following message:
We do use AngularFire (7.1.1) as well.
Running our Angular application itself seems to be ok. So it seems to be related with the upgrade + our jest configuration maybe?
Adding the full test log below:
PASS spec/tests/app/import-service/validators/country.validator.spec.ts
PASS spec/tests/app/patchers/organisation/1_0.patcher.spec.ts
PASS spec/tests/app/import-service/validators/firestore-doc.validator.spec.ts
PASS spec/tests/app/functions/country-service-backend.spec.ts
PASS spec/tests/app/scoring/calculate-partial-score.spec.ts
PASS spec/tests/app/import-service/validators/unique-column-value.validator.spec.ts
PASS spec/tests/app/version-patcher-service.spec.ts
PASS spec/tests/app/import-service/validators/column-exclusive-required.validator.spec.ts
PASS spec/tests/app/import-service/validators/array-match.validator.spec.ts
PASS spec/tests/app/import-service/validators/vat.validator.spec.ts
PASS spec/tests/app/import-service/validators/positive-integer.validator.spec.ts
PASS spec/tests/app/import-service/validators/inbetween-integer.validator.spec.ts
PASS spec/tests/app/import-service/validators/length.validator.spec.ts
PASS spec/tests/app/import-service/validators/firestore-key.validator.spec.ts
PASS spec/tests/app/import-service/validators/trimmed.validator.spec.ts
PASS spec/tests/app/import-service/validators/email.validator.spec.ts
PASS spec/tests/app/import-service/validators/split-and-apply-multiple-validators.spec.ts
PASS spec/tests/app/import-service/import.service.spec.ts
FAIL spec/tests/app/generic-competition-admin/exercises-import.spec.ts
● Test suite failed to run
FAIL spec/tests/app/helpers.service.spec.ts
● Test suite failed to run
FAIL spec/tests/app/trampoline/judges-trampoline-service.spec.ts
● Test suite failed to run
PASS spec/tests/app/functions/util-helpers.spec.ts
● Console
}
PASS spec/tests/app/scoring/acro-specific-ranking.service.spec.ts
● Console
}
PASS spec/tests/app/generic-competitions/generic-acro-ranking.service.spec.ts
● Console
}
FAIL spec/tests/app/scoring/team-ranking.service.spec.ts
● Test suite failed to run
Summary of all failing tests
FAIL spec/tests/app/generic-competition-admin/exercises-import.spec.ts
● Test suite failed to run
FAIL spec/tests/app/helpers.service.spec.ts
● Test suite failed to run
FAIL spec/tests/app/trampoline/judges-trampoline-service.spec.ts
● Test suite failed to run
FAIL spec/tests/app/scoring/team-ranking.service.spec.ts
● Test suite failed to run
The text was updated successfully, but these errors were encountered: