sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

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

Open felipedeboni opened this issue 1 year ago • 1 comments

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.

felipedeboni avatar Jun 27 '24 19:06 felipedeboni

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

lforst avatar Jun 28 '24 08:06 lforst

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

jarojasm95 avatar Jul 03 '24 18:07 jarojasm95

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

lforst avatar Jul 04 '24 07:07 lforst

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',
  },

KieraDOG avatar Jul 14 '24 23:07 KieraDOG

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 avatar Jul 15 '24 07:07 Lms24

@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.ts to import and use captureException
  • 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.

prichey avatar Sep 30 '24 17:09 prichey

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

lforst avatar Oct 01 '24 07:10 lforst

[!NOTE]

"@sentry/nextjs": "^8.32.0",
 "jest": "^29.7.0",

https://github.com/getsentry/sentry-javascript/issues/12683#issuecomment-2227525891

this works for me

parksu0219 avatar Oct 29 '24 12:10 parksu0219

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.

lforst avatar Mar 03 '25 15:03 lforst