getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
7.9k stars 1.56k forks source link

@sentry/nextjs v8.0.0 and above breaks jest tests. #12683

Open felipedeboni opened 3 months ago

felipedeboni commented 3 months ago

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 throws Cannot 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.

{
  "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.

lforst commented 3 months ago

Hi, thanks for writing in. I think we'll only be able to effectively debug this with a minimal reproduction. Thanks!

jarojasm95 commented 3 months ago

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

lforst commented 3 months ago

If possible a reproduction would be amazing to sustainably fix this.

KieraDOG commented 2 months ago

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',
  },
Lms24 commented 2 months ago

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)

prichey commented 2 days ago

@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:

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.

lforst commented 1 day ago

@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.