react / wstein / node_modules / jest-cli / src / HasteModuleLoader / __tests__ / HasteModuleLoader-requireMock-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}3536beforeEach(function() {37HasteModuleLoader = require('../HasteModuleLoader');3839mockEnvironment = {40global: {41console: {},42mockClearTimers: jest.genMockFn()43},44runSourceText: jest.genMockFn().mockImplementation(function(codeStr) {45/* jshint evil:true */46return (new Function('return ' + codeStr))();47})48};49});5051describe('requireMock', function() {52pit('uses manual mocks before attempting to automock', function() {53return buildLoader().then(function(loader) {54var exports = loader.requireMock(null, 'ManuallyMocked');55expect(exports.isManualMockModule).toBe(true);56});57});5859pit('stores and re-uses manual mock exports', function() {60return buildLoader().then(function(loader) {61var exports = loader.requireMock(null, 'ManuallyMocked');62exports.setModuleStateValue('test value');63exports = loader.requireMock(null, 'ManuallyMocked');64expect(exports.getModuleStateValue()).toBe('test value');65});66});6768pit('automocks @providesModule modules without a manual mock', function() {69return buildLoader().then(function(loader) {70var exports = loader.requireMock(null, 'RegularModule');71expect(exports.getModuleStateValue._isMockFunction).toBe(true);72});73});7475pit('automocks relative-path modules without a file extension', function() {76return buildLoader().then(function(loader) {77var exports = loader.requireMock(78__filename,79'./test_root/RegularModule'80);81expect(exports.getModuleStateValue._isMockFunction).toBe(true);82});83});8485pit('automocks relative-path modules with a file extension', function() {86return buildLoader().then(function(loader) {87var exports = loader.requireMock(88__filename,89'./test_root/RegularModule.js'90);91expect(exports.getModuleStateValue._isMockFunction).toBe(true);92});93});9495pit('just falls back when loading a native module', function() {96return buildLoader().then(function(loader) {97var error;98// Okay so this is a really WAT way to test this, but we99// are going to require an empty .node file which should100// throw an error letting us know that the file is too101// short. If it does not (it gives another error) then we102// are not correctly falling back to 'native' require.103try {104loader.requireMock(105__filename,106'./test_root/NativeModule.node'107);108} catch (e) {109error = e;110} finally {111expect(error.message).toContain('NativeModule.node: file too short');112}113});114});115116pit('stores and re-uses automocked @providesModule exports', function() {117return buildLoader().then(function(loader) {118var exports = loader.requireMock(null, 'RegularModule');119exports.externalMutation = 'test value';120exports = loader.requireMock(null, 'RegularModule');121expect(exports.externalMutation).toBe('test value');122});123});124125pit('stores and re-uses automocked relative-path modules', function() {126return buildLoader().then(function(loader) {127var exports = loader.requireMock(128__filename,129'./test_root/RegularModule'130);131exports.externalMutation = 'test value';132exports = loader.requireMock(133__filename,134'./test_root/RegularModule'135);136expect(exports.externalMutation).toBe('test value');137});138});139140pit('multiple node core modules returns correct module', function() {141return buildLoader().then(function(loader) {142loader.requireMock(null, 'fs');143expect(loader.requireMock(null, 'events').EventEmitter).toBeDefined();144});145});146147pit('throws on non-existant @providesModule modules', function() {148return buildLoader().then(function(loader) {149expect(function() {150loader.requireMock(null, 'DoesntExist');151}).toThrow();152});153});154155pit('uses the closest manual mock when duplicates exist', function() {156return buildLoader().then(function(loader) {157var exports1 = loader.requireMock(158__dirname,159path.resolve(__dirname, './test_root/subdir1/MyModule')160);161expect(exports1.modulePath).toEqual(162'subdir1/__mocks__/MyModule.js'163);164165var exports2 = loader.requireMock(166__dirname,167path.resolve(__dirname, './test_root/subdir2/MyModule')168);169expect(exports2.modulePath).toEqual(170'subdir2/__mocks__/MyModule.js'171);172});173});174});175});176177178