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:

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:

...

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:


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
mochaas 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--requiredirective vs. including directly in the test file.My
npm testscript is as follows: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
importjsdom-globalI get an output like this:Note that
screen.debug()outputs an empty<body />element.If I remove the
-r jsdom-global/registeroption and explicitly addimport 'jsdom-global/register'in my test file, I get:
...As you can see, in the second example including
jsdom-global/registervia an explicitimportin my test file appears to make the DOM available toscreen, but requiring it viamochaon the command line does not.Both methods appear to make an identical
documentglobal available: