Skip to content

Commit dc24f32

Browse files
committed
Add test case that demonstrates errors are logged even if they're caught
1 parent cb6fd8c commit dc24f32

File tree

1 file changed

+40
-0
lines changed
  • fixtures/dom/src/components/fixtures/error-handling

1 file changed

+40
-0
lines changed

fixtures/dom/src/components/fixtures/error-handling/index.js

+40
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const React = window.React;
2+
const ReactDOM = window.ReactDOM;
23

34
import FixtureSet from '../../FixtureSet';
45
import TestCase from '../../TestCase';
@@ -56,6 +57,34 @@ class Example extends React.Component {
5657
}
5758
}
5859

60+
class TriggerErrorAndCatch extends React.Component {
61+
container = document.createElement('div');
62+
63+
triggerErrorAndCatch = () => {
64+
// Use setImmediate so that the render is syncrhonous
65+
setImmediate(() => {
66+
try {
67+
ReactDOM.render(
68+
<BadRender
69+
throws={() => {
70+
throw new Error('Caught error');
71+
}}
72+
/>,
73+
this.container
74+
);
75+
} catch (e) {}
76+
});
77+
};
78+
79+
render() {
80+
return (
81+
<button onClick={this.triggerErrorAndCatch}>
82+
Trigger error and catch
83+
</button>
84+
);
85+
}
86+
}
87+
5988
export default class ErrorHandlingTestCases extends React.Component {
6089
render() {
6190
return (
@@ -119,6 +148,17 @@ export default class ErrorHandlingTestCases extends React.Component {
119148
}}
120149
/>
121150
</TestCase>
151+
<TestCase
152+
title="Errors are logged even if they're caught (development mode only)"
153+
description="">
154+
<TestCase.Steps>
155+
<li>Click the "Trigger render error and catch" button</li>
156+
</TestCase.Steps>
157+
<TestCase.ExpectedResult>
158+
Open the console. "Uncaught Error: Caught error" should have been logged by the browser.
159+
</TestCase.ExpectedResult>
160+
<TriggerErrorAndCatch />
161+
</TestCase>
122162
</FixtureSet>
123163
);
124164
}

0 commit comments

Comments
 (0)