question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
technicallyfeasiblecommented, May 12, 2018

I was actually able to fix my particular problem by using a custom environment and passing the globals that I need through manually:

const JsDomEnvironment = require('jest-environment-jsdom');


class JestCustomEnvironment extends JsDomEnvironment {
  async setup() {
    await super.setup();
    global.document = this.global.document;
    global.window = this.global.window;
    global.jsdom = this.global.jsdom;
  }
}

module.exports = JestCustomEnvironment;

And pass it in the config:

“testEnvironment”: “./JestCustomEnvironment.js”,

0reactions
github-actions[bot]commented, Apr 28, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found