How to use the jest-util.installCommonGlobals function in jest-util

To help you get started, we’ve selected a few jest-util examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github thymikee / jest-preset-angular / testEnvironment.js View on Github external
constructor(config) {
    // lazy require
    const {JSDOM} = require('jsdom');
    
    this.document = new JSDOM('', {
      url: config.testURL,
      runScripts: 'dangerously'
    });
    const global = (this.global = this.document.window.document.defaultView);
    // Node's error-message stack size is limited at 10, but it's pretty useful
    // to see more than that when a test fails.
    global.Error.stackTraceLimit = 100;
    installCommonGlobals(global, config.globals);

    this.moduleMocker = new mock.ModuleMocker(global);
    this.fakeTimers = new FakeTimers(global, this.moduleMocker, config);
  }
github sourcegraph / sourcegraph / shared / dev / jest-environment.js View on Github external
constructor(config, options = {}) {
    this.dom = new JSDOM('', {
      pretendToBeVisual: true,
      runScripts: 'dangerously',
      url: config.testURL,
      virtualConsole: new VirtualConsole().sendTo(options.console || console),
      ...config.testEnvironmentOptions,
    })
    const global = (this.global = this.dom.window.document.defaultView)
    if (!global) {
      throw new Error('JSDOM did not return a Window object')
    }
    // Node's error-message stack size is limited at 10, but it's pretty useful
    // to see more than that when a test fails.
    this.global.Error.stackTraceLimit = 100
    installCommonGlobals(global, config.globals)
    // Report uncaught errors.
    let userErrorListenerCount = 0
    this.errorEventListener = event => {
      if (userErrorListenerCount === 0 && event.error) {
        process.emit('uncaughtException', event.error)
      }
    }
    global.addEventListener('error', this.errorEventListener)
    // However, don't report them as uncaught if the user listens to 'error' event.
    // In that case, we assume the might have custom error handling logic.
    /* eslint-disable @typescript-eslint/unbound-method */
    const originalAddListener = global.addEventListener
    const originalRemoveListener = global.removeEventListener
    global.addEventListener = function(...args) {
      if (args[0] === 'error') {
        userErrorListenerCount++
github unadlib / tees / packages / tees-environment / src / index.js View on Github external
this.context = vm.createContext();
      global = (this.global = vm.runInContext(
        'this',
        Object.assign(this.context, config.testEnvironmentOptions),
      ));
      global.global = global;
      global.clearInterval = clearInterval;
      global.clearTimeout = clearTimeout;
      global.setInterval = setInterval;
      global.setTimeout = setTimeout;
      if (typeof URL !== 'undefined' && typeof URLSearchParams !== 'undefined') {
        global.URL = URL;
        global.URLSearchParams = URLSearchParams;
      }
    }
    installCommonGlobals(global, this._config.globals);
    if (this._isJSDOM) {
      this.errorEventListener = event => {
        if (userErrorListenerCount === 0 && event.error) {
          process.emit('uncaughtException', event.error);
        }
      };
      global.addEventListener('error', this.errorEventListener);
      const originalAddListener = global.addEventListener;
      const originalRemoveListener = global.removeEventListener;
      let userErrorListenerCount = 0;
      global.addEventListener = function (name) {
        if (name === 'error') {
          userErrorListenerCount++;
        }
        return originalAddListener.apply(this, arguments);
      };
github ringcentral / ringcentral-js-widgets / packages / ringcentral-e2e-environment / src / index.js View on Github external
this.context = vm.createContext();
      global = (this.global = vm.runInContext(
        'this',
        Object.assign(this.context, config.testEnvironmentOptions),
      ));
      global.global = global;
      global.clearInterval = clearInterval;
      global.clearTimeout = clearTimeout;
      global.setInterval = setInterval;
      global.setTimeout = setTimeout;
      if (typeof URL !== 'undefined' && typeof URLSearchParams !== 'undefined') {
        global.URL = URL;
        global.URLSearchParams = URLSearchParams;
      }
    }
    installCommonGlobals(global, this._config.globals);
    if (this._isJSDOM) {
      this.errorEventListener = event => {
        if (userErrorListenerCount === 0 && event.error) {
          process.emit('uncaughtException', event.error);
        }
      };
      global.addEventListener('error', this.errorEventListener);
      const originalAddListener = global.addEventListener;
      const originalRemoveListener = global.removeEventListener;
      let userErrorListenerCount = 0;
      global.addEventListener = function (name) {
        if (name === 'error') {
          userErrorListenerCount++;
        }
        return originalAddListener.apply(this, arguments);
      };
github facebookarchive / atom-ide-ui / modules / nuclide-jest / AtomJestEnvironment.js View on Github external
constructor(config) {
    const global = (this.global = window);
    installCommonGlobals(global, config.globals);
    this.moduleMocker = new mock.ModuleMocker(global);
    this.fakeTimers = new FakeTimers({
      config,
      global,
      moduleMocker: this.moduleMocker,
    });
  }
github d4rkr00t / jest-electron-runner / packages / jest-environment-electron / index.js View on Github external
constructor(config) {
    const global = (this.global = window);
    installCommonGlobals(global, config.globals);
    this.moduleMocker = new mock.ModuleMocker(global);
    this.fakeTimers = new FakeTimers(global, this.moduleMocker, config);
  }
github facebook-atom / jest-electron-runner / packages / electron / src / Environment.js View on Github external
constructor(config: ProjectConfig) {
    this.global = global;
    this.moduleMocker = new mock.ModuleMocker(global);
    this.fakeTimers = {
      useFakeTimers() {
        throw new Error('fakeTimers are not supproted in electron environment');
      },
      clearAllTimers() {},
    };
    installCommonGlobals(global, config.globals);
  }