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

ReferenceError: global is not defined #4

Closed
steve-taylor-medirecords opened this issue May 16, 2021 · 4 comments
Closed

ReferenceError: global is not defined #4

steve-taylor-medirecords opened this issue May 16, 2021 · 4 comments
Labels
question Further information is requested

Comments

@steve-taylor-medirecords

I created a fresh CRA project with TypeScript:

npx create-react-app mocha-vite-puppeteer-poc --template typescript

I added Counter.tsx, Counter.test.tsx and test.html exactly as documented in README.md, except with extensions (and references to them) changed from .js and .jsx to .ts and .tsx.

Running npx mocha-vite-puppeteer produces the following output:


  vite v2.3.2 dev server running at:

  > Local: http://localhost:3001/
  > Network: use `--host` to expose
[vite] connecting...
[vite] connected.

Download the React DevTools for a better development experience: https://reactjs.org/link/react-devtools
file:///Users/steve/POC/mocha-vite-puppeteer-poc/node_modules/mocha-vite-puppeteer/cli.js:67
    throw new Error(message);
          ^

Error: ReferenceError: global is not defined
    at node_modules/pretty-format/build/plugins/AsymmetricMatcher.js (http://localhost:3001/node_modules/.vite/@testing-library_react.js?v=4cf13379:20464:19)
    at __require (http://localhost:3001/node_modules/.vite/chunk-22DOMQDG.js?v=4cf13379:9:44)
    at node_modules/pretty-format/build/index.js (http://localhost:3001/node_modules/.vite/@testing-library_react.js?v=4cf13379:21184:53)
    at __require (http://localhost:3001/node_modules/.vite/chunk-22DOMQDG.js?v=4cf13379:9:44)
    at http://localhost:3001/node_modules/.vite/@testing-library_react.js?v=4cf13379:33334:39
    at file:///Users/steve/POC/mocha-vite-puppeteer-poc/node_modules/mocha-vite-puppeteer/cli.js:67:11
    at /Users/steve/POC/mocha-vite-puppeteer-poc/node_modules/puppeteer/lib/cjs/vendor/mitt/src/index.js:51:62
    at Array.map (<anonymous>)
    at Object.emit (/Users/steve/POC/mocha-vite-puppeteer-poc/node_modules/puppeteer/lib/cjs/vendor/mitt/src/index.js:51:43)
    at Page.emit (/Users/steve/POC/mocha-vite-puppeteer-poc/node_modules/puppeteer/lib/cjs/puppeteer/common/EventEmitter.js:72:22)
    at Page._handleException (/Users/steve/POC/mocha-vite-puppeteer-poc/node_modules/puppeteer/lib/cjs/puppeteer/common/Page.js:684:14)
    at /Users/steve/POC/mocha-vite-puppeteer-poc/node_modules/puppeteer/lib/cjs/puppeteer/common/Page.js:153:66
    at /Users/steve/POC/mocha-vite-puppeteer-poc/node_modules/puppeteer/lib/cjs/vendor/mitt/src/index.js:51:62
    at Array.map (<anonymous>)
    at Object.emit (/Users/steve/POC/mocha-vite-puppeteer-poc/node_modules/puppeteer/lib/cjs/vendor/mitt/src/index.js:51:43)
@larsthorup
Copy link
Owner

Hey @steve-taylor-medirecords thank you for the detailed report!
My guess is that something in @testing-library/react expects global being defined as if it was running in a Node.js environment. To work around this, you can add global = window in your test.html. See the file https://github.com/larsthorup/mocha-vite-puppeteer/blob/main/test/app/test.html (part of the end-to-end test here in this repo).
Please let me know if this solves the issue?

@larsthorup
Copy link
Owner

Looks like it could be the same issue as this one: testing-library/dom-testing-library#756

@larsthorup larsthorup added the question Further information is requested label May 23, 2021
@larsthorup
Copy link
Owner

Closing for lack of reply. Feel free to add additional comments!

@fatso83
Copy link

fatso83 commented Aug 26, 2021

This is even mentioned in the Vite docs. If your code expects global to be defined, then just add global = window in a small script tag before your module executes, just like @larsthorup suggests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants