testing-library/dom-testing-library

global is not defined

JesusTheHun opened this issue · 6 comments

  • @testing-library/dom version: 7.31.2
  • Testing Framework and version: jest@27.0.4, @testing-library/react@11.2.7
  • DOM Environment: @testing-library/jest-dom@5.14.1

Running Chrome

What you did:

I tried to use rtl in a vitejs project. This builder uses esbuild, which is a lot more closed to the ECMAScript spec and therefore is more strict.

What happened:

Uncaught ReferenceError: global is not defined at node_modules/@testing-library/dom/node_modules/pretty-format/build/plugins/AsymmetricMatcher.js (AsymmetricMatcher.js:10)

Referencing this line of code : var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;

Problem description:

While rtl is supposed to be used by nodejs, the package is by default compiled by esbuild and send to the browser, leading to this error.

Suggested solution:

Despite being unrelated to RTL directly, it would be very convenient if you could simply add a check to make sure global exists before going further.

This should be fixed in our alpha. Could you try it with @testing-library/react@alpha and report back if the issue persists? For me information about breaking changes in the alpha check out #979

Using the alpha, the problems appears somewhere else :

Uncaught ReferenceError: global is not defined at node_modules/pretty-format/build/plugins/AsymmetricMatcher.js (AsymmetricMatcher.js:10) at __require2 (chunk-IHTDASF6.js?v=60a1262a:17) at node_modules/pretty-format/build/index.js (index.js:14) at __require2 (chunk-IHTDASF6.js?v=60a1262a:17) at dom.esm.js:3

dom.esm.js from node_modules/@testing-library/react/node_modules/@testing-library/dom/dist/@testing-library/dom.esm.js

@JesusTheHun Could you provide a repro? Preferably a cloneable repository.

I've created a simple vitejs project with a storybook and some tests, but the error doesn't show up. I will try to get a repro but it will probably take a bit of time.

I spent a lot of time on this and I still can't make it go into error.

Sounds like an issue with your test setup. Feel free to re-open once you can reproduce the issue.