|
| 1 | +/** |
| 2 | + * Copyright (c) 2014-present, 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 | + * @emails oncall+jsinfra |
| 9 | + */ |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +jest.disableAutomock(); |
| 13 | +jest.mock('../../environments/JSDOMEnvironment'); |
| 14 | + |
| 15 | +const path = require('path'); |
| 16 | +const normalizeConfig = require('../../config/normalize'); |
| 17 | + |
| 18 | +describe('HasteModuleLoader', () => { |
| 19 | + let HasteModuleLoader; |
| 20 | + let HasteResolver; |
| 21 | + let JSDOMEnvironment; |
| 22 | + |
| 23 | + const rootDir = path.join(__dirname, 'test_root'); |
| 24 | + const rootPath = path.join(rootDir, 'root.js'); |
| 25 | + const config = normalizeConfig({ |
| 26 | + cacheDirectory: global.CACHE_DIRECTORY, |
| 27 | + name: 'HasteModuleLoader-mock-tests', |
| 28 | + rootDir, |
| 29 | + }); |
| 30 | + |
| 31 | + function buildLoader() { |
| 32 | + const environment = new JSDOMEnvironment(config); |
| 33 | + const resolver = new HasteResolver(config, {resetCache: false}); |
| 34 | + return resolver.getHasteMap().then( |
| 35 | + response => resolver.end().then(() => |
| 36 | + new HasteModuleLoader(config, environment, response) |
| 37 | + ) |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + HasteModuleLoader = require('../HasteModuleLoader'); |
| 43 | + HasteResolver = require('../../resolvers/HasteResolver'); |
| 44 | + JSDOMEnvironment = require('../../environments/JSDOMEnvironment'); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('jest.mock', () => { |
| 48 | + pit('uses uses explicitly set mocks instead of automocking', () => { |
| 49 | + return buildLoader().then(loader => { |
| 50 | + const mockReference = {isMock: true}; |
| 51 | + const root = loader.requireModule(rootPath, './root.js'); |
| 52 | + // Erase module registry because root.js requires most other modules. |
| 53 | + root.jest.resetModuleRegistry(); |
| 54 | + |
| 55 | + root.jest.mock('RegularModule', () => mockReference); |
| 56 | + root.jest.mock('ManuallyMocked', () => mockReference); |
| 57 | + |
| 58 | + expect( |
| 59 | + loader.requireModuleOrMock(rootPath, 'RegularModule') |
| 60 | + ).toEqual(mockReference); |
| 61 | + |
| 62 | + expect( |
| 63 | + loader.requireModuleOrMock(rootPath, 'RegularModule') |
| 64 | + ).toEqual(mockReference); |
| 65 | + }); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('jest.setMock', () => { |
| 70 | + pit('uses uses explicitly set mocks instead of automocking', () => { |
| 71 | + return buildLoader().then(loader => { |
| 72 | + const mockReference = {isMock: true}; |
| 73 | + const root = loader.requireModule(rootPath, './root.js'); |
| 74 | + // Erase module registry because root.js requires most other modules. |
| 75 | + root.jest.resetModuleRegistry(); |
| 76 | + |
| 77 | + root.jest.setMock('RegularModule', mockReference); |
| 78 | + root.jest.setMock('ManuallyMocked', mockReference); |
| 79 | + |
| 80 | + expect( |
| 81 | + loader.requireModuleOrMock(rootPath, 'RegularModule') |
| 82 | + ).toBe(mockReference); |
| 83 | + |
| 84 | + expect( |
| 85 | + loader.requireModuleOrMock(rootPath, 'RegularModule') |
| 86 | + ).toBe(mockReference); |
| 87 | + }); |
| 88 | + }); |
| 89 | + }); |
| 90 | +}); |
0 commit comments