jest-environment-node
Version:
60 lines (38 loc) • 1.34 kB
JavaScript
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/
;
const FakeTimers = require('jest-util').FakeTimers;
const installCommonGlobals = require('jest-util').installCommonGlobals;
const vm = require('vm');
class NodeEnvironment {
constructor(config) {
const global = this.global = {};
vm.createContext(this.global);
global.setTimeout = setTimeout;
global.clearTimeout = clearTimeout;
global.setInterval = setInterval;
global.clearInterval = clearInterval;
global.Promise = Promise;
global.JSON = JSON;
installCommonGlobals(global, config.globals);
this.fakeTimers = new FakeTimers(global);}
dispose() {
this.global = null;
this.fakeTimers = null;}
runSourceText(sourceText, filename) {
if (this.global) {
return vm.runInContext(sourceText, this.global, {
filename,
displayErrors: false });}
return null;}
runWithRealTimers(callback) {
if (this.global && this.fakeTimers) {
this.fakeTimers.runWithRealTimers(callback);}}}
module.exports = NodeEnvironment;