From f9950346ad81e2f91156782e68e93eccfc44602d Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Sun, 1 Apr 2018 01:49:13 +0100 Subject: [PATCH] Add regression tests for error boundary replay bugs --- .../ReactIncrementalErrorReplay-test.js | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 packages/react-reconciler/src/__tests__/ReactIncrementalErrorReplay-test.js diff --git a/packages/react-reconciler/src/__tests__/ReactIncrementalErrorReplay-test.js b/packages/react-reconciler/src/__tests__/ReactIncrementalErrorReplay-test.js new file mode 100644 index 0000000000000..a14c6a9b3e393 --- /dev/null +++ b/packages/react-reconciler/src/__tests__/ReactIncrementalErrorReplay-test.js @@ -0,0 +1,59 @@ +/** + * Copyright (c) 2013-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @emails react-core + * @jest-environment node + */ + +'use strict'; + +let React; +let ReactNoop; + +describe('ReactIncrementalErrorReplay', () => { + beforeEach(() => { + jest.resetModules(); + React = require('react'); + ReactNoop = require('react-noop-renderer'); + }); + + function div(...children) { + children = children.map(c => (typeof c === 'string' ? {text: c} : c)); + return {type: 'div', children, prop: undefined}; + } + + function span(prop) { + return {type: 'span', children: [], prop}; + } + + it('should fail gracefully on error in the host environment', () => { + ReactNoop.simulateErrorInHostConfig(() => { + ReactNoop.render(); + expect(() => ReactNoop.flush()).toThrow('Error in host config.'); + }); + }); + + it('should fail gracefully on error that does not reproduce on replay', () => { + let didInit = false; + + function badLazyInit() { + const needsInit = !didInit; + didInit = true; + if (needsInit) { + throw new Error('Hi'); + } + } + + class App extends React.Component { + render() { + badLazyInit(); + return
; + } + } + ReactNoop.render(); + expect(() => ReactNoop.flush()).toThrow('Hi'); + }); +});