Add more flexibility in testing errors in begin/complete phases (#13235)
* Add more flexibility in testing errors in begin/complete phases * Update too
Dan Abramov committed
Jul 19, 2018 at 00:23 UTC
ead08827d05c6d292cb5953355dccc19553a1215
4 files changed
+17
-41
packages/react-noop-renderer/src/createReactNoop.js
+7
-25
@@ -40,10 +40,7 @@ if (__DEV__) {
40
41
function createReactNoop(reconciler: Function, useMutation: boolean) {
42
let scheduledCallback = null;
43
-
43
let instanceCounter = 0;
45
- let failInBeginPhase = false;
46
- let failInCompletePhase = false;
44
45
function appendChildToContainerOrInstance(
46
parentInstance: Container | Instance,
@@ -167,9 +164,6 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
164
165
const sharedHostConfig = {
166
getRootHostContext() {
170
- if (failInBeginPhase) {
171
- throw new Error('Error in host config.');
172
- }
167
return NO_CONTEXT;
168
},
169
@@ -182,7 +176,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
176
},
177
178
createInstance(type: string, props: Props): Instance {
185
- if (failInCompletePhase) {
179
+ if (type === 'errorInCompletePhase') {
180
throw new Error('Error in host config.');
181
}
182
const inst = {
@@ -217,6 +211,9 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
211
oldProps: Props,
212
newProps: Props,
213
): null | {} {
214
+ if (type === 'errorInCompletePhase') {
215
+ throw new Error('Error in host config.');
216
+ }
217
if (oldProps === null) {
218
throw new Error('Should have old props');
219
}
@@ -227,6 +224,9 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
224
},
225
226
shouldSetTextContent(type: string, props: Props): boolean {
227
+ if (type === 'errorInBeginPhase') {
228
+ throw new Error('Error in host config.');
229
+ }
230
return (
231
typeof props.children === 'string' || typeof props.children === 'number'
232
);
@@ -695,24 +695,6 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
695
console.log(...bufferedLog);
696
},
697
698
- simulateErrorInHostConfigDuringBeginPhase(fn: () => void) {
699
- failInBeginPhase = true;
700
- try {
701
- fn();
702
- } finally {
703
- failInBeginPhase = false;
704
- }
705
- },
706
-
707
- simulateErrorInHostConfigDuringCompletePhase(fn: () => void) {
708
- failInCompletePhase = true;
709
- try {
710
- fn();
711
- } finally {
712
- failInCompletePhase = false;
713
- }
714
- },
715
-
698
flushWithoutCommitting(
699
expectedFlush: Array<mixed>,
700
rootID: string = DEFAULT_ROOT_ID,
packages/react-reconciler/src/__tests__/ReactIncrementalErrorHandling-test.internal.js
+2
-4
@@ -1212,10 +1212,8 @@ describe('ReactIncrementalErrorHandling', () => {
1212
});
1213
1214
it('handles error thrown by host config while working on failed root', () => {
1215
- ReactNoop.simulateErrorInHostConfigDuringBeginPhase(() => {
1216
- ReactNoop.render(<span />);
1217
- expect(() => ReactNoop.flush()).toThrow('Error in host config.');
1218
- });
1215
+ ReactNoop.render(<errorInBeginPhase />);
1216
+ expect(() => ReactNoop.flush()).toThrow('Error in host config.');
1217
});
1218
1219
it('handles error thrown by top-level callback', () => {
packages/react-reconciler/src/__tests__/ReactIncrementalErrorReplay-test.js
+2
-4
@@ -21,10 +21,8 @@ describe('ReactIncrementalErrorReplay', () => {
21
});
22
23
it('should fail gracefully on error in the host environment', () => {
24
- ReactNoop.simulateErrorInHostConfigDuringBeginPhase(() => {
25
- ReactNoop.render(<span />);
26
- expect(() => ReactNoop.flush()).toThrow('Error in host config.');
27
- });
24
+ ReactNoop.render(<errorInBeginPhase />);
25
+ expect(() => ReactNoop.flush()).toThrow('Error in host config.');
26
});
27
28
it("should ignore error if it doesn't throw on retry", () => {
packages/react/src/__tests__/ReactProfiler-test.internal.js
+6
-8
@@ -992,14 +992,12 @@ describe('Profiler', () => {
992
993
// Simulate a renderer error during the "complete" phase.
994
// This mimics behavior like React Native's View/Text nesting validation.
995
- ReactNoop.simulateErrorInHostConfigDuringCompletePhase(() => {
996
- ReactNoop.render(
997
- <React.unstable_Profiler id="profiler" onRender={jest.fn()}>
998
- <span>hi</span>
999
- </React.unstable_Profiler>,
1000
- );
1001
- expect(ReactNoop.flush).toThrow('Error in host config.');
1002
- });
995
+ ReactNoop.render(
996
+ <React.unstable_Profiler id="profiler" onRender={jest.fn()}>
997
+ <errorInCompletePhase>hi</errorInCompletePhase>
998
+ </React.unstable_Profiler>,
999
+ );
1000
+ expect(ReactNoop.flush).toThrow('Error in host config.');
1001
1002
// So long as the profiler timer's fiber stack is reset correctly,
1003
// Subsequent renders should not error.