@@ -18,9 +18,7 @@ const os = require('os');
1818const path = require ( 'path' ) ;
1919const resolve = require ( 'resolve' ) ;
2020const transform = require ( '../lib/transform' ) ;
21- const utils = require ( '../lib/utils' ) ;
2221
23- const COVERAGE_STORAGE_VAR_NAME = '____JEST_COVERAGE_DATA____' ;
2422const NODE_PATH =
2523 ( process . env . NODE_PATH ? process . env . NODE_PATH . split ( path . delimiter ) : null ) ;
2624const IS_PATH_BASED_MODULE_NAME = / ^ (?: \. \. ? \/ | \/ ) / ;
@@ -168,52 +166,58 @@ class Loader {
168166 * objects.
169167 */
170168 _execModule ( moduleObj ) {
171- const modulePath = moduleObj . __filename ;
172- let moduleContent = transform ( modulePath , this . _config ) ;
169+ // If the environment was disposed, prevent this module from
170+ // being executed.
171+ if ( ! this . _environment . global ) {
172+ return ;
173+ }
174+
175+ const filename = moduleObj . __filename ;
176+ let moduleContent = transform ( filename , this . _config ) ;
177+ let collectorStore ;
173178
174- // Every module, if loaded for jest, should have a parent
175- // so they don't think they are run standalone
179+ // Every module receives a mock parent so they don't assume they are run
180+ // standalone.
176181 moduleObj . parent = mockParentModule ;
177- moduleObj . require = this . constructBoundRequire ( modulePath ) ;
178-
179- const moduleLocalBindings = {
180- module : moduleObj ,
181- exports : moduleObj . exports ,
182- require : moduleObj . require ,
183- __dirname : path . dirname ( modulePath ) ,
184- __filename : modulePath ,
185- global : this . _environment . global ,
186- jest : this . _createRuntimeFor ( modulePath ) ,
187- } ;
182+ moduleObj . require = this . constructBoundRequire ( filename ) ;
188183
189184 const onlyCollectFrom = this . _config . collectCoverageOnlyFrom ;
190185 const shouldCollectCoverage =
191- this . _config . collectCoverage === true && ! onlyCollectFrom
192- || ( onlyCollectFrom && onlyCollectFrom [ modulePath ] === true ) ;
186+ ( this . _config . collectCoverage === true && ! onlyCollectFrom ) ||
187+ ( onlyCollectFrom && onlyCollectFrom [ filename ] === true ) ;
193188
194189 if ( shouldCollectCoverage ) {
195- if ( ! hasOwnProperty . call ( this . _coverageCollectors , modulePath ) ) {
196- this . _coverageCollectors [ modulePath ] =
197- new this . _CoverageCollector ( moduleContent , modulePath ) ;
190+ if ( ! hasOwnProperty . call ( this . _coverageCollectors , filename ) ) {
191+ this . _coverageCollectors [ filename ] =
192+ new this . _CoverageCollector ( moduleContent , filename ) ;
198193 }
199- const collector = this . _coverageCollectors [ modulePath ] ;
200- moduleLocalBindings [ COVERAGE_STORAGE_VAR_NAME ] =
201- collector . getCoverageDataStore ( ) ;
194+ const collector = this . _coverageCollectors [ filename ] ;
195+ collectorStore = collector . getCoverageDataStore ( ) ;
202196 moduleContent =
203- collector . getInstrumentedSource ( COVERAGE_STORAGE_VAR_NAME ) ;
197+ collector . getInstrumentedSource ( '____JEST_COVERAGE_DATA____' ) ;
204198 }
205199
206200 const lastExecutingModulePath = this . _currentlyExecutingModulePath ;
207- this . _currentlyExecutingModulePath = modulePath ;
208-
201+ this . _currentlyExecutingModulePath = filename ;
209202 const origCurrExecutingManualMock = this . _isCurrentlyExecutingManualMock ;
210- this . _isCurrentlyExecutingManualMock = modulePath ;
211-
212- utils . runContentWithLocalBindings (
213- this . _environment ,
214- moduleContent ,
215- modulePath ,
216- moduleLocalBindings
203+ this . _isCurrentlyExecutingManualMock = filename ;
204+
205+ // Use this name for the module wrapper for consistency with node.
206+ const evalResultVariable = 'Object.<anonymous>' ;
207+ const wrapper = 'this["' + evalResultVariable + '"] = function(module, exports, require, __dirname, __filename, global, jest, ____JEST_COVERAGE_DATA____) {' + moduleContent + '\n};' ;
208+ this . _environment . runSourceText ( wrapper , filename ) ;
209+ const wrapperFunc = this . _environment . global [ evalResultVariable ] ;
210+ delete this . _environment . global [ evalResultVariable ] ;
211+ wrapperFunc . call (
212+ moduleObj . exports , // module context
213+ moduleObj ,
214+ moduleObj . exports ,
215+ moduleObj . require ,
216+ path . dirname ( filename ) ,
217+ filename ,
218+ this . _environment . global ,
219+ this . _createRuntimeFor ( filename ) ,
220+ collectorStore
217221 ) ;
218222
219223 this . _isCurrentlyExecutingManualMock = origCurrExecutingManualMock ;
0 commit comments