Skip to content

Commit

Permalink
Remove getJestRuntime from ModuleLoader; expose it for tests as `__…
Browse files Browse the repository at this point in the history
…getJestRuntimeForTest`.
  • Loading branch information
cpojer committed Nov 16, 2015
1 parent 5f82cc7 commit e1faf1b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 31 deletions.
41 changes: 21 additions & 20 deletions src/HasteModuleLoader/HasteModuleLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -793,10 +793,6 @@ class Loader {
}
}

getJestRuntime(dir) {
return this._createRuntimeFor(dir);
}

_createRuntimeFor(currPath) {
const runtime = {
addMatchers: matchers => {
Expand Down Expand Up @@ -849,23 +845,7 @@ class Loader {
},

resetModuleRegistry: () => {
var envGlobal = this._environment.global;
Object.keys(envGlobal).forEach(key => {
const globalMock = envGlobal[key];
if (
(typeof globalMock === 'object' && globalMock !== null) ||
typeof globalMock === 'function'
) {
globalMock._isMockFunction && globalMock.mockClear();
}
});

if (envGlobal.mockClearTimers) {
envGlobal.mockClearTimers();
}

this.resetModuleRegistry();

return runtime;
},

Expand All @@ -891,6 +871,27 @@ class Loader {
resetModuleRegistry() {
this._mockRegistry = {};
this._moduleRegistry = {};

if (this._environment && this._environment.global) {
var envGlobal = this._environment.global;
Object.keys(envGlobal).forEach(key => {
const globalMock = envGlobal[key];
if (
(typeof globalMock === 'object' && globalMock !== null) ||
typeof globalMock === 'function'
) {
globalMock._isMockFunction && globalMock.mockClear();
}
});

if (envGlobal.mockClearTimers) {
envGlobal.mockClearTimers();
}
}
}

__getJestRuntimeForTest(dir) {
return this._createRuntimeFor(dir);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ describe('nodeHasteModuleLoader', function() {
var regularModule = testRequire('RegularModule');
var origModuleStateValue = regularModule.getModuleStateValue();

loader.getJestRuntime().dontMock('RegularModule');
loader.__getJestRuntimeForTest().dontMock('RegularModule');

// Generate a mock for a module with side effects
loader.getJestRuntime().genMockFromModule(
loader.__getJestRuntimeForTest().genMockFromModule(
'ModuleWithSideEffects'
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ describe('HasteModuleLoader', function() {

pit('passes config data through to jest.envData', function() {
return buildLoader().then(function(loader) {
var envData = loader.getJestRuntime().getTestEnvData();
var envData = loader.__getJestRuntimeForTest().getTestEnvData();
expect(envData).toEqual(config.testEnvData);
});
});

pit('freezes jest.envData object', function() {
return buildLoader().then(function(loader) {
var envData = loader.getJestRuntime().getTestEnvData();
var envData = loader.__getJestRuntimeForTest().getTestEnvData();
expect(Object.isFrozen(envData)).toBe(true);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ describe('HasteModuleLoader', function() {
'marked with .dontMock()',
function() {
return buildLoader().then(function(loader) {
loader.getJestRuntime(__filename).dontMock('ManuallyMocked');
loader.__getJestRuntimeForTest(__filename)
.dontMock('ManuallyMocked');
var exports = loader.requireModule(__filename, 'ManuallyMocked');
expect(exports.isManualMockModule).toBe(false);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('HasteModuleLoader', function() {

pit('doesnt mock modules when explicitly dontMock()ed', function() {
return buildLoader().then(function(loader) {
loader.getJestRuntime().dontMock('RegularModule');
loader.__getJestRuntimeForTest().dontMock('RegularModule');
var exports = loader.requireModuleOrMock(null, 'RegularModule');
expect(exports.isRealModule).toBe(true);
});
Expand All @@ -70,7 +70,7 @@ describe('HasteModuleLoader', function() {
'denormalized module name',
function() {
return buildLoader().then(function(loader) {
loader.getJestRuntime(__filename)
loader.__getJestRuntimeForTest(__filename)
.dontMock('./test_root/RegularModule');
var exports = loader.requireModuleOrMock(__filename, 'RegularModule');
expect(exports.isRealModule).toBe(true);
Expand All @@ -80,7 +80,7 @@ describe('HasteModuleLoader', function() {

pit('doesnt mock modules when autoMockOff() has been called', function() {
return buildLoader().then(function(loader) {
loader.getJestRuntime().autoMockOff();
loader.__getJestRuntimeForTest().autoMockOff();
var exports = loader.requireModuleOrMock(null, 'RegularModule');
expect(exports.isRealModule).toBe(true);
});
Expand All @@ -98,7 +98,7 @@ describe('HasteModuleLoader', function() {
'available',
function() {
return buildLoader().then(function(loader) {
loader.getJestRuntime(__filename).autoMockOff();
loader.__getJestRuntimeForTest().autoMockOff();
var exports = loader.requireModuleOrMock(
__filename,
'ManuallyMocked'
Expand All @@ -110,7 +110,6 @@ describe('HasteModuleLoader', function() {

pit('resolves mapped module names and unmocks them by default', function() {
return buildLoader().then(function(loader) {
loader.getJestRuntime(__filename);
var exports =
loader.requireModuleOrMock(__filename, 'image!not-really-a-module');
expect(exports.isGlobalImageStub).toBe(true);
Expand Down
2 changes: 1 addition & 1 deletion src/jasmineTestRunner/jasmineTestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ function jasmineTestRunner(config, environment, moduleLoader, testPath) {
});

if (!config.persistModuleRegistryBetweenSpecs) {
moduleLoader.getJestRuntime().resetModuleRegistry();
moduleLoader.resetModuleRegistry();
}
});

Expand Down

0 comments on commit e1faf1b

Please sign in to comment.