| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and 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 | 'use strict'; |
| 10 | |
| 11 | let React; |
| 12 | let ReactDOMClient; |
| 13 | let act; |
| 14 | |
| 15 | describe('ReactError', () => { |
| 16 | let globalErrorMock; |
| 17 | |
| 18 | beforeEach(() => { |
| 19 | if (!__DEV__) { |
| 20 | // In production, our Jest environment overrides the global Error |
| 21 | // class in order to decode error messages automatically. However |
| 22 | // this is a single test where we actually *don't* want to decode |
| 23 | // them. So we assert that the OriginalError exists, and temporarily |
| 24 | // set the global Error object back to it. |
| 25 | globalErrorMock = global.Error; |
| 26 | global.Error = globalErrorMock.OriginalError; |
| 27 | expect(typeof global.Error).toBe('function'); |
| 28 | } |
| 29 | jest.resetModules(); |
| 30 | React = require('react'); |
| 31 | ReactDOMClient = require('react-dom/client'); |
| 32 | act = require('internal-test-utils').act; |
| 33 | }); |
| 34 | |
| 35 | afterEach(() => { |
| 36 | if (!__DEV__) { |
| 37 | global.Error = globalErrorMock; |
| 38 | } |
| 39 | }); |
| 40 | |
| 41 | // @gate build === "production" |
| 42 | // @gate !source |
| 43 | it('should error with minified error code', () => { |
| 44 | expect(() => { |
| 45 | ReactDOMClient.createRoot(null); |
| 46 | }).toThrow( |
| 47 | 'Minified React error #200; visit ' + |
| 48 | 'https://react.dev/errors/200' + |
| 49 | ' for the full message or use the non-minified dev environment' + |
| 50 | ' for full errors and additional helpful warnings.', |
| 51 | ); |
| 52 | }); |
| 53 | |
| 54 | // @gate build === "production" |
| 55 | // @gate !source |
| 56 | it('should serialize arguments', async () => { |
| 57 | function Oops() { |
| 58 | return {}; |
| 59 | } |
| 60 | Oops.displayName = '#wtf'; |
| 61 | |
| 62 | const container = document.createElement('div'); |
| 63 | const root = ReactDOMClient.createRoot(container); |
| 64 | await expect(async () => { |
| 65 | await act(async () => { |
| 66 | root.render(<Oops />); |
| 67 | }); |
| 68 | }).rejects.toThrow( |
| 69 | 'Minified React error #152; visit ' + |
| 70 | 'https://reactjs.org/docs/error-decoder.html?invariant=152&args[]=%23wtf' + |
| 71 | ' for the full message or use the non-minified dev environment' + |
| 72 | ' for full errors and additional helpful warnings.', |
| 73 | ); |
| 74 | }); |
| 75 | }); |