when testing an application which loads separate modules globals are not propagated and the modules run in a separate context
See original GitHub issue🐛 Bug Report
I have an application that loads extra code as Module to override the “require” mechanic and isolate the code. This works well when running in directly in node and globals are propagated to the modules (they share the same virtual context).
However, when running the code in Jest it starts to fail with “document is not defined” and also with issues when using instanceof since objects have been created in different contexts.
To Reproduce
- setup a global
- load another file as a new Module
- try to access the global in the module
- ->You will get an error MY_GLOBAL is not defined
Expected behavior
- The global exists and can be accessed
Link to repl or repo (highly encouraged)
https://github.com/technicallyfeasible/jest-module
Run npx envinfo --preset jest
Paste the results here:
System:
OS: Windows 10
CPU: x64 Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz
Binaries:
Yarn: 1.6.0 - ~AppData\Roaming\npm\yarn.CMD
npm: 6.0.0 - ~AppData\Roaming\npm\npm.CMD
Issue Analytics
- State:
- Created 5 years ago
- Comments:6
Top Results From Across the Web
Globals and Ambient Context - Rubberduck News
The basic idea is to pull our dependencies from global scope, encapsulate them in a class module, …and then making an instance of...
Read more >python - Visibility of global variables in imported modules
Globals in Python are global to a module, not across all modules. ... There are different ways to solve this, depending on your...
Read more >Testing - Spring
The Spring TestContext Framework provides consistent loading of Spring ApplicationContext instances and WebApplicationContext instances as well ...
Read more >importlib — The implementation of import — Python 3.11.1 ...
This function should be called if any modules are created/installed while your program is running to guarantee all finders will notice the new...
Read more >Component testing scenarios - Angular
Here's another test that changes the component's title property before calling fixture.detectChanges() . content_copy it('should display a different test ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I was actually able to fix my particular problem by using a custom environment and passing the globals that I need through manually:
And pass it in the config:
“testEnvironment”: “./JestCustomEnvironment.js”,
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.