jsdom-global icon indicating copy to clipboard operation
jsdom-global copied to clipboard

Differences in scope/context when calling via CLI vs. include

Open dvbsknd opened this issue 3 years ago • 0 comments

I hope I'm not missing something obvious but have spent quite a bit of time trying to solve this before creating an issue.

I am using mocha as my test runner for historical reasons and attempting to test React components using @testing-library/react. I've come across what seems to be a scope/context issue between the command-line --require directive vs. including directly in the test file.

My npm test script is as follows:

"test:client": "npx mocha -r jsdom-global/register -r @babel/register -recursive 'client/**/test.js'",

I've pasted the full script in case there's something else that can be spotted. When I run the script above on a test that does not explicitly import jsdom-global I get an output like this:

image

Note that screen.debug() outputs an empty <body /> element.

If I remove the -r jsdom-global/register option and explicitly add import 'jsdom-global/register' in my test file, I get:

Screen Shot 2020-11-05 at 8 35 21 am ... Screen Shot 2020-11-05 at 8 34 55 am

As you can see, in the second example including jsdom-global/register via an explicit import in my test file appears to make the DOM available to screen, but requiring it via mocha on the command line does not.

Both methods appear to make an identical document global available:

image image

dvbsknd avatar Nov 04 '20 21:11 dvbsknd