How to use hasCaughtError method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

MainErrorBoundary.js

Source: MainErrorBoundary.js Github

copy
1import React from 'react';
2import DocumentTitle from 'react-document-title';
3
4import ImageBrokenPlate from './ImageBrokenPlate';
5import ImageSpinningPlate from './ImageSpinningPlate';
6
7class MainErrorBoundary extends React.Component {
8  constructor(props) {
9    super(props);
10    this.state = {hasCaughtError: false};
11  }
12
13  componentDidCatch(error) {
14    console.error(error.message);
15    this.setState({hasCaughtError: true});
16  }
17
18  render() {
19    const {title, className, isWaiting, hasError} = this.props;
20    const {hasCaughtError} = this.state;
21    return (
22      <DocumentTitle title={title}>
23        <main className={className}>
24          {isWaiting ? (
25            <ImageSpinningPlate />
26          ) : hasError || hasCaughtError ? (
27            <ImageBrokenPlate />
28          ) : (
29            this.props.children
30          )}
31        </main>
32      </DocumentTitle>
33    );
34  }
35}
36
37export default MainErrorBoundary;
38
Full Screen

ReactErrorUtils-test.internal.js

Source: ReactErrorUtils-test.internal.js Github

copy
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @emails react-core
8 */
9
10'use strict';
11
12let ReactErrorUtils;
13
14describe('ReactErrorUtils', () => {
15  beforeEach(() => {
16    // TODO: can we express this test with only public API?
17    ReactErrorUtils = require('shared/ReactErrorUtils');
18  });
19
20  it(`it should rethrow caught errors`, () => {
21    const err = new Error('foo');
22    const callback = function() {
23      throw err;
24    };
25    ReactErrorUtils.invokeGuardedCallbackAndCatchFirstError(
26      'foo',
27      callback,
28      null,
29    );
30    expect(ReactErrorUtils.hasCaughtError()).toBe(false);
31    expect(() => ReactErrorUtils.rethrowCaughtError()).toThrow(err);
32  });
33
34  it(`should call the callback the passed arguments`, () => {
35    const callback = jest.fn();
36    ReactErrorUtils.invokeGuardedCallback(
37      'foo',
38      callback,
39      null,
40      'arg1',
41      'arg2',
42    );
43    expect(callback).toBeCalledWith('arg1', 'arg2');
44  });
45
46  it(`should call the callback with the provided context`, () => {
47    const context = {didCall: false};
48    ReactErrorUtils.invokeGuardedCallback(
49      'foo',
50      function() {
51        this.didCall = true;
52      },
53      context,
54    );
55    expect(context.didCall).toBe(true);
56  });
57
58  it(`should catch errors`, () => {
59    const error = new Error();
60    const returnValue = ReactErrorUtils.invokeGuardedCallback(
61      'foo',
62      function() {
63        throw error;
64      },
65      null,
66      'arg1',
67      'arg2',
68    );
69    expect(returnValue).toBe(undefined);
70    expect(ReactErrorUtils.hasCaughtError()).toBe(true);
71    expect(ReactErrorUtils.clearCaughtError()).toBe(error);
72  });
73
74  it(`should return false from clearCaughtError if no error was thrown`, () => {
75    const callback = jest.fn();
76    ReactErrorUtils.invokeGuardedCallback('foo', callback, null);
77    expect(ReactErrorUtils.hasCaughtError()).toBe(false);
78    expect(ReactErrorUtils.clearCaughtError).toThrow('no error was captured');
79  });
80
81  it(`can nest with same debug name`, () => {
82    const err1 = new Error();
83    let err2;
84    const err3 = new Error();
85    let err4;
86    ReactErrorUtils.invokeGuardedCallback(
87      'foo',
88      function() {
89        ReactErrorUtils.invokeGuardedCallback(
90          'foo',
91          function() {
92            throw err1;
93          },
94          null,
95        );
96        err2 = ReactErrorUtils.clearCaughtError();
97        throw err3;
98      },
99      null,
100    );
101    err4 = ReactErrorUtils.clearCaughtError();
102
103    expect(err2).toBe(err1);
104    expect(err4).toBe(err3);
105  });
106
107  it(`handles nested errors`, () => {
108    const err1 = new Error();
109    let err2;
110    ReactErrorUtils.invokeGuardedCallback(
111      'foo',
112      function() {
113        ReactErrorUtils.invokeGuardedCallback(
114          'foo',
115          function() {
116            throw err1;
117          },
118          null,
119        );
120        err2 = ReactErrorUtils.clearCaughtError();
121      },
122      null,
123    );
124    // Returns null because inner error was already captured
125    expect(ReactErrorUtils.hasCaughtError()).toBe(false);
126
127    expect(err2).toBe(err1);
128  });
129
130  it('handles nested errors in separate renderers', () => {
131    const ReactErrorUtils1 = require('shared/ReactErrorUtils');
132    jest.resetModules();
133    const ReactErrorUtils2 = require('shared/ReactErrorUtils');
134    expect(ReactErrorUtils1).not.toEqual(ReactErrorUtils2);
135
136    let ops = [];
137
138    ReactErrorUtils1.invokeGuardedCallback(
139      null,
140      () => {
141        ReactErrorUtils2.invokeGuardedCallback(
142          null,
143          () => {
144            throw new Error('nested error');
145          },
146          null,
147        );
148        // ReactErrorUtils2 should catch the error
149        ops.push(ReactErrorUtils2.hasCaughtError());
150        ops.push(ReactErrorUtils2.clearCaughtError().message);
151      },
152      null,
153    );
154
155    // ReactErrorUtils1 should not catch the error
156    ops.push(ReactErrorUtils1.hasCaughtError());
157
158    expect(ops).toEqual([true, 'nested error', false]);
159  });
160
161  if (!__DEV__) {
162    // jsdom doesn't handle this properly, but Chrome and Firefox should. Test
163    // this with a fixture.
164    it('catches null values', () => {
165      ReactErrorUtils.invokeGuardedCallback(
166        null,
167        function() {
168          throw null; // eslint-disable-line no-throw-literal
169        },
170        null,
171      );
172      expect(ReactErrorUtils.hasCaughtError()).toBe(true);
173      expect(ReactErrorUtils.clearCaughtError()).toBe(null);
174    });
175  }
176
177  it(`can be shimmed`, () => {
178    const ops = [];
179    jest.resetModules();
180    jest.mock(
181      'shared/invokeGuardedCallbackImpl',
182      () =>
183        function invokeGuardedCallback(name, func, context, a) {
184          ops.push(a);
185          try {
186            func.call(context, a);
187          } catch (error) {
188            this.onError(error);
189          }
190        },
191    );
192    ReactErrorUtils = require('shared/ReactErrorUtils');
193
194    try {
195      const err = new Error('foo');
196      const callback = function() {
197        throw err;
198      };
199      ReactErrorUtils.invokeGuardedCallbackAndCatchFirstError(
200        'foo',
201        callback,
202        null,
203        'somearg',
204      );
205      expect(() => ReactErrorUtils.rethrowCaughtError()).toThrow(err);
206      expect(ops).toEqual(['somearg']);
207    } finally {
208      jest.unmock('shared/invokeGuardedCallbackImpl');
209    }
210  });
211});
212
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)