react / wstein / node_modules / jest-cli / src / HasteModuleLoader / __tests__ / HasteModuleLoader-requireModule-test.js
80679 views/**1* Copyright (c) 2014, Facebook, Inc. All rights reserved.2*3* This source code is licensed under the BSD-style license found in the4* LICENSE file in the root directory of this source tree. An additional grant5* of patent rights can be found in the PATENTS file in the same directory.6*/7'use strict';89jest.autoMockOff();1011var path = require('path');12var q = require('q');13var utils = require('../../lib/utils');1415describe('HasteModuleLoader', function() {16var HasteModuleLoader;17var mockEnvironment;18var resourceMap;1920var CONFIG = utils.normalizeConfig({21name: 'HasteModuleLoader-tests',22rootDir: path.join(__dirname, 'test_root')23});2425function buildLoader() {26if (!resourceMap) {27return HasteModuleLoader.loadResourceMap(CONFIG).then(function(map) {28resourceMap = map;29return buildLoader();30});31} else {32return q(new HasteModuleLoader(CONFIG, mockEnvironment, resourceMap));33}34}3536beforeEach(function() {37HasteModuleLoader = require('../HasteModuleLoader');3839mockEnvironment = {40global: {41console: {},42mockClearTimers: jest.genMockFn(),43JSON: JSON44},45runSourceText: jest.genMockFn().mockImplementation(function(codeStr) {46/* jshint evil:true */47return (new Function('return ' + codeStr))();48})49};50});5152describe('requireModule', function() {53pit('finds @providesModule modules', function() {54return buildLoader().then(function(loader) {55var exports = loader.requireModule(null, 'RegularModule');56expect(exports.isRealModule).toBe(true);57});58});5960pit('throws on non-existant @providesModule modules', function() {61return buildLoader().then(function(loader) {62expect(function() {63loader.requireModule(null, 'DoesntExist');64}).toThrow('Cannot find module \'DoesntExist\' from \'.\'');65});66});6768pit('finds relative-path modules without file extension', function() {69return buildLoader().then(function(loader) {70var exports = loader.requireModule(71__filename,72'./test_root/RegularModule'73);74expect(exports.isRealModule).toBe(true);75});76});7778pit('finds relative-path modules with file extension', function() {79return buildLoader().then(function(loader) {80var exports = loader.requireModule(81__filename,82'./test_root/RegularModule.js'83);84expect(exports.isRealModule).toBe(true);85});86});8788pit('throws on non-existant relative-path modules', function() {89return buildLoader().then(function(loader) {90expect(function() {91loader.requireModule(__filename, './DoesntExist');92}).toThrow(93'Cannot find module \'./DoesntExist\' from \'' + __filename + '\''94);95});96});9798pit('finds node core built-in modules', function() {99return buildLoader().then(function(loader) {100expect(function() {101loader.requireModule(null, 'fs');102}).not.toThrow();103});104});105106pit('finds and loads JSON files without file extension', function() {107return buildLoader().then(function(loader) {108var exports = loader.requireModule(__filename, './test_root/JSONFile');109expect(exports.isJSONModule).toBe(true);110});111});112113pit('finds and loads JSON files with file extension', function() {114return buildLoader().then(function(loader) {115var exports = loader.requireModule(116__filename,117'./test_root/JSONFile.json'118);119expect(exports.isJSONModule).toBe(true);120});121});122123pit('requires a JSON file twice successfully', function() {124return buildLoader().then(function(loader) {125var exports1 = loader.requireModule(126__filename,127'./test_root/JSONFile.json'128);129var exports2 = loader.requireModule(130__filename,131'./test_root/JSONFile.json'132);133expect(exports1.isJSONModule).toBe(true);134expect(exports2.isJSONModule).toBe(true);135expect(exports1).toBe(exports2);136});137});138139describe('features I want to remove, but must exist for now', function() {140/**141* I'd like to kill this and make all tests use something more explicit142* when they want a manual mock, like:143*144* require.mock('MyManualMock');145* var ManuallyMocked = require('ManuallyMocked');146*147* --or--148*149* var ManuallyMocked = require.manualMock('ManuallyMocked');150*151* For now, however, this is built-in and many tests rely on it, so we152* must support it until we can do some cleanup.153*/154pit('provides manual mock when real module doesnt exist', function() {155return buildLoader().then(function(loader) {156var exports = loader.requireModule(157__filename,158'ExclusivelyManualMock'159);160expect(exports.isExclusivelyManualMockModule).toBe(true);161});162});163164/**165* requireModule() should *always* return the real module. Mocks should166* only be returned by requireMock().167*168* See the 'overrides real modules with manual mock when one exists' test169* for more info on why I want to kill this feature.170*/171pit('doesnt override real modules with manual mocks when explicitly ' +172'marked with .dontMock()', function() {173return buildLoader().then(function(loader) {174loader.requireModule(__filename, 'jest-runtime')175.dontMock('ManuallyMocked');176177var exports = loader.requireModule(__filename, 'ManuallyMocked');178expect(exports.isManualMockModule).toBe(false);179});180});181182/**183* This test is only in this section because it seems sketchy to be able184* to load up a module without pulling it from the registry. I need to do185* more investigation to understand the reasoning behind this before I186* declare it unnecessary and condemn it.187*/188pit('doesnt read from the module registry when bypassModuleRegistry is ' +189'set', function() {190return buildLoader().then(function(loader) {191var registryExports = loader.requireModule(192__filename,193'RegularModule'194);195registryExports.setModuleStateValue('registry');196197var bypassedExports = loader.requireModule(198__filename,199'RegularModule',200true201);202expect(bypassedExports.getModuleStateValue()).not.toBe('registry');203});204});205206pit('doesnt write to the module registry when bypassModuleRegistry is ' +207'set', function() {208return buildLoader().then(function(loader) {209var registryExports = loader.requireModule(210__filename,211'RegularModule'212);213registryExports.setModuleStateValue('registry');214215var bypassedExports = loader.requireModule(216__filename,217'RegularModule',218true219);220bypassedExports.setModuleStateValue('bypassed');221222expect(registryExports.getModuleStateValue()).toBe('registry');223});224});225});226});227});228229230