Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80668 views
1
/**
2
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
3
*
4
* This source code is licensed under the BSD-style license found in the
5
* LICENSE file in the root directory of this source tree. An additional grant
6
* of patent rights can be found in the PATENTS file in the same directory.
7
*/
8
'use strict';
9
10
jest.autoMockOff();
11
12
var path = require('path');
13
var q = require('q');
14
var utils = require('../../lib/utils');
15
16
describe('nodeHasteModuleLoader', function() {
17
var HasteModuleLoader;
18
var mockEnvironment;
19
var resourceMap;
20
21
var CONFIG = utils.normalizeConfig({
22
name: 'nodeHasteModuleLoader-tests',
23
rootDir: path.resolve(__dirname, 'test_root')
24
});
25
26
function buildLoader() {
27
if (!resourceMap) {
28
return HasteModuleLoader.loadResourceMap(CONFIG).then(function(map) {
29
resourceMap = map;
30
return buildLoader();
31
});
32
} else {
33
return q(new HasteModuleLoader(CONFIG, mockEnvironment, resourceMap));
34
}
35
}
36
37
beforeEach(function() {
38
HasteModuleLoader = require('../HasteModuleLoader');
39
40
mockEnvironment = {
41
global: {
42
console: {},
43
mockClearTimers: jest.genMockFn()
44
},
45
runSourceText: jest.genMockFn().mockImplementation(function(codeStr) {
46
/* jshint evil:true */
47
return (new Function('return ' + codeStr))();
48
})
49
};
50
});
51
52
describe('genMockFromModule', function() {
53
pit('does not cause side effects in the rest of the module system when ' +
54
'generating a mock', function() {
55
return buildLoader().then(function(loader) {
56
var testRequire = loader.requireModule.bind(loader, __filename);
57
58
var regularModule = testRequire('RegularModule');
59
var origModuleStateValue = regularModule.getModuleStateValue();
60
61
testRequire('jest-runtime').dontMock('RegularModule');
62
63
// Generate a mock for a module with side effects
64
testRequire('jest-runtime').genMockFromModule('ModuleWithSideEffects');
65
66
expect(regularModule.getModuleStateValue()).toBe(origModuleStateValue);
67
});
68
});
69
});
70
});
71
72