Eagerly evaluate inline requires in Jest (#7245)
* Eagerly evaluate inline requires in Jest I inlined some requires in #7188 to fix the build size regression. However this caused an issue with Jest due to it resetting module registry between tests. This is a temporary fix to #7240. It should be reverted as part of #7178. * Make the hack work in all environments
Dan Abramov committed
Jul 16, 2016 at 20:51 UTC
15ae5857f6a86ead4143dc100c61d27736cf29d9
6 files changed
+64
-17
grunt/tasks/browserify.js
+10
@@ -25,6 +25,16 @@ module.exports = function() {
25
entries: entries,
26
debug: config.debug, // sourcemaps
27
standalone: config.standalone, // global
28
+ insertGlobalVars: {
29
+ // We can remove this when we remove the few direct
30
+ // process.env.NODE_ENV checks against "test".
31
+ // The intention is to avoid embedding Browserify's `process` shim
32
+ // because we don't really need it.
33
+ // See https://github.com/facebook/react/pull/7245 for context.
34
+ process: function() {
35
+ return 'undefined';
36
+ },
37
+ },
38
};
39
40
var bundle = browserify(options);
src/isomorphic/classic/types/checkReactTypeSpec.js
+18
-1
@@ -17,6 +17,21 @@ var ReactPropTypesSecret = require('ReactPropTypesSecret');
17
var invariant = require('invariant');
18
var warning = require('warning');
19
20
+var ReactComponentTreeDevtool;
21
+
22
+if (
23
+ typeof process !== 'undefined' &&
24
+ process.env &&
25
+ process.env.NODE_ENV === 'test'
26
+) {
27
+ // Temporary hack.
28
+ // Inline requires don't work well with Jest:
29
+ // https://github.com/facebook/react/issues/7240
30
+ // Remove the inline requires when we don't need them anymore:
31
+ // https://github.com/facebook/react/pull/7178
32
+ ReactComponentTreeDevtool = require('ReactComponentTreeDevtool')
33
+}
34
+
35
var loggedTypeFailures = {};
36
37
/**
@@ -73,7 +88,9 @@ function checkReactTypeSpec(typeSpecs, values, location, componentName, element,
88
var componentStackInfo = '';
89
90
if (__DEV__) {
76
- var ReactComponentTreeDevtool = require('ReactComponentTreeDevtool');
91
+ if (!ReactComponentTreeDevtool) {
92
+ ReactComponentTreeDevtool = require('ReactComponentTreeDevtool');
93
+ }
94
if (debugID !== null) {
95
componentStackInfo = ReactComponentTreeDevtool.getStackAddendumByID(debugID);
96
} else if (element !== null) {
src/renderers/shared/stack/reconciler/ReactChildReconciler.js
+18
-1
@@ -19,11 +19,28 @@ var shouldUpdateReactComponent = require('shouldUpdateReactComponent');
19
var traverseAllChildren = require('traverseAllChildren');
20
var warning = require('warning');
21
22
+var ReactComponentTreeDevtool;
23
+
24
+if (
25
+ typeof process !== 'undefined' &&
26
+ process.env &&
27
+ process.env.NODE_ENV === 'test'
28
+) {
29
+ // Temporary hack.
30
+ // Inline requires don't work well with Jest:
31
+ // https://github.com/facebook/react/issues/7240
32
+ // Remove the inline requires when we don't need them anymore:
33
+ // https://github.com/facebook/react/pull/7178
34
+ ReactComponentTreeDevtool = require('ReactComponentTreeDevtool')
35
+}
36
+
37
function instantiateChild(childInstances, child, name, selfDebugID) {
38
// We found a component instance.
39
var keyUnique = (childInstances[name] === undefined);
40
if (__DEV__) {
26
- var ReactComponentTreeDevtool = require('ReactComponentTreeDevtool');
41
+ if (!ReactComponentTreeDevtool) {
42
+ ReactComponentTreeDevtool = require('ReactComponentTreeDevtool');
43
+ }
44
warning(
45
keyUnique,
46
'flattenChildren(...): Encountered two children with the same key, ' +
src/renderers/shared/stack/reconciler/__tests__/ReactMultiChildReconcile-test.js
-6
@@ -278,12 +278,6 @@ function testPropsSequence(sequence) {
278
describe('ReactMultiChildReconcile', function() {
279
beforeEach(function() {
280
jest.resetModuleRegistry();
281
-
282
- React = require('React');
283
- ReactDOM = require('ReactDOM');
284
- ReactDOMComponentTree = require('ReactDOMComponentTree');
285
- ReactInstanceMap = require('ReactInstanceMap');
286
- mapObject = require('mapObject');
281
});
282
283
it('should reset internal state if removed then readded', function() {
src/renderers/shared/stack/reconciler/__tests__/refs-test.js
-8
@@ -116,10 +116,6 @@ var expectClickLogsLengthToBe = function(instance, length) {
116
describe('reactiverefs', function() {
117
beforeEach(function() {
118
jest.resetModuleRegistry();
119
-
120
- React = require('React');
121
- ReactTestUtils = require('ReactTestUtils');
122
- reactComponentExpect = require('reactComponentExpect');
119
});
120
121
/**
@@ -163,10 +159,6 @@ describe('reactiverefs', function() {
159
describe('ref swapping', function() {
160
beforeEach(function() {
161
jest.resetModuleRegistry();
166
-
167
- React = require('React');
168
- ReactTestUtils = require('ReactTestUtils');
169
- reactComponentExpect = require('reactComponentExpect');
162
});
163
164
var RefHopsAround = React.createClass({
src/shared/utils/flattenChildren.js
+18
-1
@@ -16,6 +16,21 @@ var KeyEscapeUtils = require('KeyEscapeUtils');
16
var traverseAllChildren = require('traverseAllChildren');
17
var warning = require('warning');
18
19
+var ReactComponentTreeDevtool;
20
+
21
+if (
22
+ typeof process !== 'undefined' &&
23
+ process.env &&
24
+ process.env.NODE_ENV === 'test'
25
+) {
26
+ // Temporary hack.
27
+ // Inline requires don't work well with Jest:
28
+ // https://github.com/facebook/react/issues/7240
29
+ // Remove the inline requires when we don't need them anymore:
30
+ // https://github.com/facebook/react/pull/7178
31
+ ReactComponentTreeDevtool = require('ReactComponentTreeDevtool')
32
+}
33
+
34
/**
35
* @param {function} traverseContext Context passed through traversal.
36
* @param {?ReactComponent} child React child component.
@@ -33,7 +48,9 @@ function flattenSingleChildIntoContext(
48
const result = traverseContext;
49
const keyUnique = (result[name] === undefined);
50
if (__DEV__) {
36
- var ReactComponentTreeDevtool = require('ReactComponentTreeDevtool');
51
+ if (!ReactComponentTreeDevtool) {
52
+ ReactComponentTreeDevtool = require('ReactComponentTreeDevtool');
53
+ }
54
warning(
55
keyUnique,
56
'flattenChildren(...): Encountered two children with the same key, ' +