From 7dba33b2cfc67246881f6d57633a80e628ea05ec Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Fri, 24 Mar 2017 23:11:39 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=98=AD=F0=9F=98=AD=F0=9F=98=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shims/facebook-www/ReactTestUtils.js | 49 +++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/scripts/rollup/shims/facebook-www/ReactTestUtils.js b/scripts/rollup/shims/facebook-www/ReactTestUtils.js index dafb421d1d..c73b4703ce 100644 --- a/scripts/rollup/shims/facebook-www/ReactTestUtils.js +++ b/scripts/rollup/shims/facebook-www/ReactTestUtils.js @@ -11,8 +11,49 @@ 'use strict'; -const { - __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, -} = require('ReactDOM-fb'); +function getTestUtils() { + const ReactDOM = require('ReactDOM-fb'); + return ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactTestUtils; +} -module.exports = __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactTestUtils; +const TestUtils = {...getTestUtils()}; + +if (__DEV__) { + // Lasciate ogni speranza, voi ch'entrate. + // + // Some www tests currently rely on require('ReactTestUtils') acting as a lazy + // require for the whole ReactDOM implementation. However this is no longer + // the case with flat bundles since the implementation doesn't get transformed + // by www lazy requires. As a result, if test calls jest.resetModuleRegistry() + // in beforeEach(), Enzyme's ReactDOM reference will be stale and won't be + // able to share any global state (such as current owner) with the newly reset + // React singleton that would be used in classes inside the test cases. + // To work around it, I'm making any TestUtils method call proxy to the latest + // ReactDOM implementation. There might be a better way to do it but my brain + // is fried. If you have ideas, please change it to something more reasonable. + // + // https://fburl.com/jgn0nh70 + Object.keys(TestUtils).forEach(key => { + Object.defineProperty(TestUtils, key, { + get() { + return getTestUtils()[key]; + }, + }); + }) + Object.keys(TestUtils.Simulate).forEach(key => { + Object.defineProperty(TestUtils.Simulate, key, { + get() { + return getTestUtils().Simulate[key]; + }, + }); + }); + Object.keys(TestUtils.SimulateNative).forEach(key => { + Object.defineProperty(TestUtils.SimulateNative, key, { + get() { + return getTestUtils().SimulateNative[key]; + }, + }); + }); +} + +module.exports = TestUtils;