Add more flexibility in testing errors in begin/complete phases (#13235)

* Add more flexibility in testing errors in begin/complete phases

* Update too
This commit is contained in:
Dan Abramov
2018-07-19 00:23:10 +01:00
committed by GitHub
parent e4e58343e4
commit ead08827d0
4 changed files with 17 additions and 41 deletions

View File

@@ -40,10 +40,7 @@ if (__DEV__) {
function createReactNoop(reconciler: Function, useMutation: boolean) {
let scheduledCallback = null;
let instanceCounter = 0;
let failInBeginPhase = false;
let failInCompletePhase = false;
function appendChildToContainerOrInstance(
parentInstance: Container | Instance,
@@ -167,9 +164,6 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
const sharedHostConfig = {
getRootHostContext() {
if (failInBeginPhase) {
throw new Error('Error in host config.');
}
return NO_CONTEXT;
},
@@ -182,7 +176,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
},
createInstance(type: string, props: Props): Instance {
if (failInCompletePhase) {
if (type === 'errorInCompletePhase') {
throw new Error('Error in host config.');
}
const inst = {
@@ -217,6 +211,9 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
oldProps: Props,
newProps: Props,
): null | {} {
if (type === 'errorInCompletePhase') {
throw new Error('Error in host config.');
}
if (oldProps === null) {
throw new Error('Should have old props');
}
@@ -227,6 +224,9 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
},
shouldSetTextContent(type: string, props: Props): boolean {
if (type === 'errorInBeginPhase') {
throw new Error('Error in host config.');
}
return (
typeof props.children === 'string' || typeof props.children === 'number'
);
@@ -695,24 +695,6 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
console.log(...bufferedLog);
},
simulateErrorInHostConfigDuringBeginPhase(fn: () => void) {
failInBeginPhase = true;
try {
fn();
} finally {
failInBeginPhase = false;
}
},
simulateErrorInHostConfigDuringCompletePhase(fn: () => void) {
failInCompletePhase = true;
try {
fn();
} finally {
failInCompletePhase = false;
}
},
flushWithoutCommitting(
expectedFlush: Array<mixed>,
rootID: string = DEFAULT_ROOT_ID,

View File

@@ -1212,10 +1212,8 @@ describe('ReactIncrementalErrorHandling', () => {
});
it('handles error thrown by host config while working on failed root', () => {
ReactNoop.simulateErrorInHostConfigDuringBeginPhase(() => {
ReactNoop.render(<span />);
expect(() => ReactNoop.flush()).toThrow('Error in host config.');
});
ReactNoop.render(<errorInBeginPhase />);
expect(() => ReactNoop.flush()).toThrow('Error in host config.');
});
it('handles error thrown by top-level callback', () => {

View File

@@ -21,10 +21,8 @@ describe('ReactIncrementalErrorReplay', () => {
});
it('should fail gracefully on error in the host environment', () => {
ReactNoop.simulateErrorInHostConfigDuringBeginPhase(() => {
ReactNoop.render(<span />);
expect(() => ReactNoop.flush()).toThrow('Error in host config.');
});
ReactNoop.render(<errorInBeginPhase />);
expect(() => ReactNoop.flush()).toThrow('Error in host config.');
});
it("should ignore error if it doesn't throw on retry", () => {

View File

@@ -992,14 +992,12 @@ describe('Profiler', () => {
// Simulate a renderer error during the "complete" phase.
// This mimics behavior like React Native's View/Text nesting validation.
ReactNoop.simulateErrorInHostConfigDuringCompletePhase(() => {
ReactNoop.render(
<React.unstable_Profiler id="profiler" onRender={jest.fn()}>
<span>hi</span>
</React.unstable_Profiler>,
);
expect(ReactNoop.flush).toThrow('Error in host config.');
});
ReactNoop.render(
<React.unstable_Profiler id="profiler" onRender={jest.fn()}>
<errorInCompletePhase>hi</errorInCompletePhase>
</React.unstable_Profiler>,
);
expect(ReactNoop.flush).toThrow('Error in host config.');
// So long as the profiler timer's fiber stack is reset correctly,
// Subsequent renders should not error.