react / wstein / node_modules / jest-cli / src / HasteModuleLoader / __tests__ / HasteModuleLoader-NODE_PATH-test.js
80668 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.resolve(__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}3536function initHasteModuleLoader(nodePath) {37process.env.NODE_PATH = nodePath;38HasteModuleLoader = require('../HasteModuleLoader');39mockEnvironment = {40global: {41console: {},42mockClearTimers: jest.genMockFn()43},44runSourceText: jest.genMockFn().mockImplementation(function(codeStr) {45/* jshint evil:true */46return (new Function('return ' + codeStr))();47})48};49}5051pit('uses NODE_PATH to find modules', function() {52var nodePath = process.cwd() +53'/src/HasteModuleLoader/__tests__/NODE_PATH_dir';54initHasteModuleLoader(nodePath);55return buildLoader().then(function(loader) {56var exports = loader.requireModuleOrMock(null, 'RegularModuleInNodePath');57expect(exports).toBeDefined();58});59});6061pit('finds modules in NODE_PATH containing multiple paths', function() {62var cwd = process.cwd();63var nodePath = cwd + '/some/other/path' + path.delimiter + cwd +64'/src/HasteModuleLoader/__tests__/NODE_PATH_dir';65initHasteModuleLoader(nodePath);66return buildLoader().then(function(loader) {67var exports = loader.requireModuleOrMock(null, 'RegularModuleInNodePath');68expect(exports).toBeDefined();69});70});7172pit('doesnt find modules if NODE_PATH is relative', function() {73var nodePath = process.cwd().substr(path.sep.length) +74'src/HasteModuleLoader/__tests__/NODE_PATH_dir';75initHasteModuleLoader(nodePath);76return buildLoader().then(function(loader) {77try {78var exports = loader.requireModuleOrMock(null,79'RegularModuleInNodePath');80expect(exports).toBeUndefined();81} catch (e) {82expect(83(e.message.indexOf('Cannot find module'))).toBeGreaterThan(-1);84}85});86});8788});899091