|
11 | 11 |
|
12 | 12 | 'use strict';
|
13 | 13 |
|
14 |
| -const { |
15 |
| - __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, |
16 |
| -} = require('ReactDOM-fb'); |
| 14 | +function getTestUtils() { |
| 15 | + const ReactDOM = require('ReactDOM-fb'); |
| 16 | + return ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactTestUtils; |
| 17 | +} |
17 | 18 |
|
18 |
| -module.exports = __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactTestUtils; |
| 19 | +const TestUtils = {...getTestUtils()}; |
| 20 | + |
| 21 | +if (__DEV__) { |
| 22 | + // Lasciate ogni speranza, voi ch'entrate. |
| 23 | + // |
| 24 | + // Some www tests currently rely on require('ReactTestUtils') acting as a lazy |
| 25 | + // require for the whole ReactDOM implementation. However this is no longer |
| 26 | + // the case with flat bundles since the implementation doesn't get transformed |
| 27 | + // by www lazy requires. As a result, if test calls jest.resetModuleRegistry() |
| 28 | + // in beforeEach(), Enzyme's ReactDOM reference will be stale and won't be |
| 29 | + // able to share any global state (such as current owner) with the newly reset |
| 30 | + // React singleton that would be used in classes inside the test cases. |
| 31 | + // To work around it, I'm making any TestUtils method call proxy to the latest |
| 32 | + // ReactDOM implementation. There might be a better way to do it but my brain |
| 33 | + // is fried. If you have ideas, please change it to something more reasonable. |
| 34 | + // |
| 35 | + // https://fburl.com/jgn0nh70 |
| 36 | + Object.keys(TestUtils).forEach(key => { |
| 37 | + Object.defineProperty(TestUtils, key, { |
| 38 | + get() { |
| 39 | + return getTestUtils()[key]; |
| 40 | + }, |
| 41 | + }); |
| 42 | + }) |
| 43 | + Object.keys(TestUtils.Simulate).forEach(key => { |
| 44 | + Object.defineProperty(TestUtils.Simulate, key, { |
| 45 | + get() { |
| 46 | + return getTestUtils().Simulate[key]; |
| 47 | + }, |
| 48 | + }); |
| 49 | + }); |
| 50 | + Object.keys(TestUtils.SimulateNative).forEach(key => { |
| 51 | + Object.defineProperty(TestUtils.SimulateNative, key, { |
| 52 | + get() { |
| 53 | + return getTestUtils().SimulateNative[key]; |
| 54 | + }, |
| 55 | + }); |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +module.exports = TestUtils; |
0 commit comments