Open felipedeboni opened 5 months ago
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
app/utils/add.ts
to import and use captureException
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
Is there an existing issue for this?
How do you use Sentry?
Sentry Saas (sentry.io)
Which SDK are you using?
@sentry/nextjs
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
throwsCannot find module '@sentry/nextjs' from ...
.Just test with Jest.
Additional environment info:
Expected Result
It should find the module.
Actual Result
It throws! I was able to make it work by monkey patching the package.json.
Still haven't checked if something else blows up.