Skip to content
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

Failed to get mock metadata: node_modules/@sentry/react/cjs/index.js #5994

Closed
3 tasks done
EvgenyOrekhov opened this issue Oct 19, 2022 · 12 comments · Fixed by #6043
Closed
3 tasks done

Failed to get mock metadata: node_modules/@sentry/react/cjs/index.js #5994

EvgenyOrekhov opened this issue Oct 19, 2022 · 12 comments · Fixed by #6043
Labels
Package: react Issues related to the Sentry React SDK Type: Bug

Comments

@EvgenyOrekhov
Copy link

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which package are you using?

@sentry/react

SDK Version

7.16.0

Framework Version

React 18.2.0

Link to Sentry event

No response

Steps to Reproduce

  1. Write a Jest test with the following contents:
import { init } from '@sentry/react';
jest.mock('@sentry/react');

Expected Result

No error

Actual Result

Test fails with Failed to get mock metadata: node_modules/@sentry/react/cjs/index.js

@EvgenyOrekhov
Copy link
Author

The issue started happening after I upgraded @sentry/react from 7.15.0 to 7.16.0.

@EvgenyOrekhov
Copy link
Author

Node.js version: 18.11.0
npm version: 8.19.2

@AbhiPrasad
Copy link
Member

Thanks for writing in! I ran into this here when running yarn deduplicate - I wonder why 🤔. I had to fix this for some internal tests. Maybe the way we generated modules changed so we now require folks to use an explicit mocks file?

#5951

We did make some refactors around using global objects, perhaps that had some effect. #5948

Perhaps we have to change our entry points as was discussed in aws-amplify/amplify-js#4499?

@SimenB perhaps you have some context on what could be going on here?

@jackmccloy
Copy link

@AbhiPrasad I just wanted to report that I'm having the same issue. I haven't had a chance to dig into the links you shared and probably won't until the weekend, but this does seem to be an issue going from 7.15.0 to 7.16.0

@AbhiPrasad
Copy link
Member

The temp fix here for everyone is to actually add mocks for your method, similar to what is done here: https://github.com/getsentry/sentry-javascript/pull/5951/files#diff-9b6a20828c3c1c7a7e897abb6d7c8a88130fc3ac55dad3cbd8d3ac92aff19fadR9

Probably also cleaner because you can control exactly what is being mocked, but that’s probably a matter of opinion on coding style.

@lobsterkatie
Copy link
Member

lobsterkatie commented Oct 20, 2022

@EvgenyOrekhov @jackmccloy - Two questions:

Does either of you have automock: true set in your jest config? That seemed to come up in a number of mentions of this error message in my googling.
Would one of you mind manually editing node_modules/@sentry/react/cjs/index.js to replace all instances of const with var and then trying to run the tests again?

@lobsterkatie lobsterkatie added the Package: react Issues related to the Sentry React SDK label Oct 20, 2022
@EvgenyOrekhov
Copy link
Author

@lobsterkatie I don't have automock: true. Replacing all const with var in node_modules/@sentry/react/cjs/index.js didn't help, got the same error.

@kolorfilm
Copy link

I have the same issue. Suggestions don't work for me neither.

@scottmmjackson
Copy link

scottmmjackson commented Oct 21, 2022

I'm getting the same issue but for mocking @sentry/browser. no automock: true in jest config.

EDIT: Confirmed that jest unit tests work in @sentry/react: 7.14.2 but are broken in the latest release.

EDIT2: Confirmed working in 7.15.0 - the release that broke this is 7.16.0

@johnhunter
Copy link

Can confirm the same issue with:

I do not have automock: true in jest config.

This browser test fails:

import * as Sentry from '@sentry/browser';

jest.mock('@sentry/browser');

test('Sentry', () => {
  expect(Sentry).toBeDefined();
});

Failed to get mock metadata: <redacted>/node_modules/@sentry/browser/cjs/index.js

This node one fails:

const Sentry = require('@sentry/node');

jest.mock('@sentry/node');

test('Sentry', () => {
  expect(Sentry).toBeDefined();
});

Failed to get mock metadata: <redacted>/node_modules/@sentry/node/cjs/index.js

@AbhiPrasad
Copy link
Member

This seems to be a jest bug - will be fixed by jestjs/jest#13513

AbhiPrasad pushed a commit that referenced this issue Oct 27, 2022
…'` to CJS files (#6043)

As of version 2.69.0, setting `output.generatedCode` to `'es2015'` (as we do) causes Rollup to [add `[Symbol.toStringTag]: 'Module'` to generated CJS files](rollup/rollup#4378 (comment)). Though this is valid ES6, it causes Jest to be unable to mock our generated packages.

Though [a PR](jestjs/jest#13513) has been opened to fix this, the change almost certainly won't be backported, so anyone using Jest 29.2.2 or under will run into [this problem](#5994) if they try to mock `@sentry/xxx` 7.16. (The relevant change was introduced in #5951, when we (semi-)accidentally upgraded Rollup from 2.67.1 to 2.78.0, and was first included in version 7.16.) 

This PR prevents the new Rollup behavior, since it has no known benefit. (The [Rollup docs](https://rollupjs.org/guide/en/#outputgeneratedcode) say that presence of the `'Module'` toStringTag "is used for feature detection in certain libraries and frameworks," but not having it hasn't seemed to hurt us so far.)
@AbhiPrasad
Copy link
Member

This will be fixed in the SDK with the next release - we recognize that not everyone can bump jest versions, but do want to shout out @Tofandel for fixing this in jest directly!

Appreciate the patience everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: react Issues related to the Sentry React SDK Type: Bug
Projects
None yet
7 participants