constructor()

in packages/jest-environment-node/src/index.ts [33:131]


  constructor(config: JestEnvironmentConfig, _context: EnvironmentContext) {
    const {projectConfig} = config;
    this.context = createContext();
    const global = (this.global = runInContext(
      'this',
      Object.assign(this.context, projectConfig.testEnvironmentOptions),
    ));
    global.global = global;
    global.clearInterval = clearInterval;
    global.clearTimeout = clearTimeout;
    global.setInterval = setInterval;
    global.setTimeout = setTimeout;
    global.Buffer = Buffer;
    global.setImmediate = setImmediate;
    global.clearImmediate = clearImmediate;
    global.ArrayBuffer = ArrayBuffer;
    // TextEncoder (global or via 'util') references a Uint8Array constructor
    // different than the global one used by users in tests. This makes sure the
    // same constructor is referenced by both.
    global.Uint8Array = Uint8Array;

    // URL and URLSearchParams are global in Node >= 10
    global.URL = URL;
    global.URLSearchParams = URLSearchParams;

    // TextDecoder and TextDecoder are global in Node >= 11
    global.TextEncoder = TextEncoder;
    global.TextDecoder = TextDecoder;

    // queueMicrotask is global in Node >= 11
    global.queueMicrotask = queueMicrotask;

    // AbortController is global in Node >= 15
    if (typeof AbortController !== 'undefined') {
      global.AbortController = AbortController;
    }
    // AbortSignal is global in Node >= 15
    if (typeof AbortSignal !== 'undefined') {
      global.AbortSignal = AbortSignal;
    }
    // Event is global in Node >= 15.4
    if (typeof Event !== 'undefined') {
      global.Event = Event;
    }
    // EventTarget is global in Node >= 15.4
    if (typeof EventTarget !== 'undefined') {
      global.EventTarget = EventTarget;
    }
    // MessageChannel is global in Node >= 15
    if (typeof MessageChannel !== 'undefined') {
      global.MessageChannel = MessageChannel;
    }
    // MessageEvent is global in Node >= 15
    if (typeof MessageEvent !== 'undefined') {
      global.MessageEvent = MessageEvent;
    }
    // performance is global in Node >= 16
    if (typeof performance !== 'undefined') {
      global.performance = performance;
    }
    // atob and btoa are global in Node >= 16
    if (typeof atob !== 'undefined' && typeof btoa !== 'undefined') {
      global.atob = atob;
      global.btoa = btoa;
    }
    installCommonGlobals(global, projectConfig.globals);

    this.moduleMocker = new ModuleMocker(global);

    const timerIdToRef = (id: number) => ({
      id,
      ref() {
        return this;
      },
      unref() {
        return this;
      },
    });

    const timerRefToId = (timer: Timer): number | undefined =>
      (timer && timer.id) || undefined;

    const timerConfig = {
      idToRef: timerIdToRef,
      refToId: timerRefToId,
    };

    this.fakeTimers = new LegacyFakeTimers({
      config: projectConfig,
      global,
      moduleMocker: this.moduleMocker,
      timerConfig,
    });

    this.fakeTimersModern = new ModernFakeTimers({
      config: projectConfig,
      global,
    });
  }