sentry-javascript
sentry-javascript copied to clipboard
@sentry/nextjs v8.0.0 and above breaks jest tests.
Is there an existing issue for this?
- [X] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
- [X] I have reviewed the documentation https://docs.sentry.io/
- [X] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases
How do you use Sentry?
Sentry Saas (sentry.io)
Which SDK are you using?
@sentry/browser
SDK Version
Any version greater or equal 8.0.0
Framework Version
jest 29.7.0
Link to Sentry event
No response
SDK Setup/Reproduction Example
Does not matter.
Steps to Reproduce
Using TypeScript and @swc/jest anything that imports from @sentry/nextjs throws Cannot find module '@sentry/nextjs' from ....
Just test with Jest.
Additional environment info:
- TypeScript 5.4.5
- Jest 29.7.0
- @swc/jest 0.2.36
- pnpm 9.4.0
Expected Result
It should find the module.
Actual Result
It throws! I was able to make it work by monkey patching the package.json.
{
"name": "@sentry/nextjs",
// ....
"exports": {
"./package.json": "./package.json",
".": {
// ...
"node": "./build/cjs/index.server.js",
"import": "./build/esm/index.server.js",
"require": "./build/cjs/index.server.js",
},
// ...
},
//...
}
Still haven't checked if something else blows up.
Hi, thanks for writing in. I think we'll only be able to effectively debug this with a minimal reproduction. Thanks!
I was running into the same issue and was able to get around it by creating a jest mock module ref
// src/__mocks__/@sentry/nextjs.ts
module.exports = {
captureException: jest.fn(),
captureMessage: jest.fn(),
setUser: jest.fn(),
};
You might need to add other mock functions depending on your usage ofc, but hopefully this is helpful to others
If possible a reproduction would be amazing to sustainably fix this.
We are facing a similar issue that was ultimately resolved with a monkey patch in jest.config.js
moduleNameMapper: {
'@sentry/nextjs': '<rootDir>/node_modules/@sentry/nextjs',
},
So far we've not been able to reproduce this issue. To everyone experiencing this issue: Please provide a minimal reproduction so that we can further investigate this. Thank you!
(Changed the scope of this issue to @sentry/nextjs as opposed to @sentry/browser as I believe this only affects NextJS SDKs so far)
@Lms24 @lforst I added a minimal reproduction here: https://github.com/prichey/sentry-8-jest-error-repro.
For us, the issue seems to be caused by adding the following lines to Jest config:
testEnvironment: 'jest-environment-jsdom',
testEnvironmentOptions: {
customExportConditions: [''],
},
My minimal reproduction matches the Next with-jest template, with the following changes:
npm i @sentry/nextjs- Modify
app/utils/add.tsto import and usecaptureException - Modifying Jest config to add the lines above
Aside: I had to do a bit of archaelogy to figure out the reason for the customExportConditions: [''], and it turns out this was due to some strange interactions between Jest and MSW (suggestion found in their docs here).
FWIW, the solution here seems to fix.
@sentry/nextjs needs export conditions to work. We ship multiple versions of the SDK for the different runtimes (browser/node/edge) and we use export conditions to determine which SDK should be loaded.
I don't think the above will concretely help you but maybe it helps someone understand why this is happening and maybe find a different workaround. I myself don't know how we would fix this near term - we're up for suggestions.
[!NOTE]
"@sentry/nextjs": "^8.32.0", "jest": "^29.7.0",
https://github.com/getsentry/sentry-javascript/issues/12683#issuecomment-2227525891
this works for me
I don't think this is solvable from within the SDK as it has more to do with consuming tooling and configuration. If anybody has a suggestion on what we could do to improve this, hit us up.