Skip to content

Commit 2fcae6d

Browse files
rickhanloniifacebook-github-bot
authored andcommitted
Re-enable integration tests (#46639)
Summary: Pull Request resolved: #46639 These tests were skipped when we were switching to component stacks, which also hid a bug later in the stack. Re-enable them. Changelog: [Internal] Reviewed By: javache Differential Revision: D63349616
1 parent 9ee5fff commit 2fcae6d

File tree

1 file changed

+66
-25
lines changed

1 file changed

+66
-25
lines changed

packages/react-native/Libraries/LogBox/__tests__/LogBox-integration-test.js

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ const LogBoxData = require('../Data/LogBoxData');
1818
const TestRenderer = require('react-test-renderer');
1919

2020
const installLogBox = () => {
21-
const LogBox = require('../LogBox');
21+
const LogBox = require('../LogBox').default;
2222

2323
LogBox.install();
2424
};
2525

2626
const uninstallLogBox = () => {
27-
const LogBox = require('../LogBox');
27+
const LogBox = require('../LogBox').default;
2828
LogBox.uninstall();
2929
};
3030

@@ -46,21 +46,20 @@ const cleanLog = logs => {
4646
});
4747
};
4848

49-
// TODO(T71117418): Re-enable skipped LogBox integration tests once React component
50-
// stack frames are the same internally and in open source.
51-
// eslint-disable-next-line jest/no-disabled-tests
52-
describe.skip('LogBox', () => {
49+
// TODO: we can remove all the symetric matchers once OSS lands component stack frames.
50+
// For now, the component stack parsing differs in ways we can't easily detect in this test.
51+
describe('LogBox', () => {
5352
const {error, warn} = console;
5453
const mockError = jest.fn();
5554
const mockWarn = jest.fn();
5655

5756
beforeEach(() => {
5857
jest.resetModules();
5958
jest.restoreAllMocks();
59+
jest.spyOn(console, 'error').mockImplementation(() => {});
6060

6161
mockError.mockClear();
6262
mockWarn.mockClear();
63-
6463
(console: any).error = mockError;
6564
(console: any).warn = mockWarn;
6665
});
@@ -79,7 +78,10 @@ describe.skip('LogBox', () => {
7978
// so we can assert on what React logs.
8079
jest.spyOn(console, 'error');
8180

82-
const output = TestRenderer.create(<DoesNotUseKey />);
81+
let output;
82+
TestRenderer.act(() => {
83+
output = TestRenderer.create(<DoesNotUseKey />);
84+
});
8385

8486
// The key error should always be the highest severity.
8587
// In LogBox, we expect these errors to:
@@ -88,16 +90,37 @@ describe.skip('LogBox', () => {
8890
// - Pass to console.error, with a "Warning" prefix so it does not pop a RedBox.
8991
expect(output).toBeDefined();
9092
expect(mockWarn).not.toBeCalled();
91-
expect(console.error.mock.calls[0].map(cleanPath)).toMatchSnapshot(
92-
'Log sent from React',
93-
);
94-
expect(cleanLog(spy.mock.calls[0])).toMatchSnapshot('Log added to LogBox');
95-
expect(mockError.mock.calls[0].map(cleanPath)).toMatchSnapshot(
96-
'Log passed to console error',
97-
);
93+
expect(console.error).toBeCalledTimes(1);
94+
expect(console.error.mock.calls[0].map(cleanPath)).toEqual([
95+
'Each child in a list should have a unique "key" prop.%s%s See https://react.dev/link/warning-keys for more information.%s',
96+
'\n\nCheck the render method of `DoesNotUseKey`.',
97+
'',
98+
expect.stringMatching('at DoesNotUseKey'),
99+
]);
100+
expect(spy).toHaveBeenCalledWith({
101+
level: 'warn',
102+
category: expect.stringContaining(
103+
'Warning: Each child in a list should have a unique',
104+
),
105+
componentStack: expect.anything(),
106+
componentStackType: 'stack',
107+
message: {
108+
content:
109+
'Warning: Each child in a list should have a unique "key" prop.\n\nCheck the render method of `DoesNotUseKey`. See https://react.dev/link/warning-keys for more information.',
110+
substitutions: [
111+
{length: 45, offset: 62},
112+
{length: 0, offset: 107},
113+
],
114+
},
115+
});
98116

99117
// The Warning: prefix is added due to a hack in LogBox to prevent double logging.
100-
expect(mockError.mock.calls[0][0].startsWith('Warning: ')).toBe(true);
118+
// We also interpolate the string before passing to the underlying console method.
119+
expect(mockError.mock.calls[0]).toEqual([
120+
expect.stringMatching(
121+
'Warning: Each child in a list should have a unique "key" prop.\n\nCheck the render method of `DoesNotUseKey`. See https://react.dev/link/warning-keys for more information.\n at ',
122+
),
123+
]);
101124
});
102125

103126
it('integrates with React and handles a fragment warning in LogBox', () => {
@@ -108,7 +131,10 @@ describe.skip('LogBox', () => {
108131
// so we can assert on what React logs.
109132
jest.spyOn(console, 'error');
110133

111-
const output = TestRenderer.create(<FragmentWithProp />);
134+
let output;
135+
TestRenderer.act(() => {
136+
output = TestRenderer.create(<FragmentWithProp />);
137+
});
112138

113139
// The fragment warning is not as severe. For this warning we don't want to
114140
// pop open a dialog, so we show a collapsed error UI.
@@ -118,15 +144,30 @@ describe.skip('LogBox', () => {
118144
// - Pass to console.error, with a "Warning" prefix so it does not pop a RedBox.
119145
expect(output).toBeDefined();
120146
expect(mockWarn).not.toBeCalled();
121-
expect(console.error.mock.calls[0].map(cleanPath)).toMatchSnapshot(
122-
'Log sent from React',
123-
);
124-
expect(cleanLog(spy.mock.calls[0])).toMatchSnapshot('Log added to LogBox');
125-
expect(mockError.mock.calls[0].map(cleanPath)).toMatchSnapshot(
126-
'Log passed to console error',
127-
);
147+
expect(console.error).toBeCalledTimes(1);
148+
expect(console.error.mock.calls[0].map(cleanPath)).toEqual([
149+
'Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.%s',
150+
'invalid',
151+
expect.stringMatching('at FragmentWithProp'),
152+
]);
153+
expect(spy).toHaveBeenCalledWith({
154+
level: 'warn',
155+
category: expect.stringContaining('Warning: Invalid prop'),
156+
componentStack: expect.anything(),
157+
componentStackType: 'legacy',
158+
message: {
159+
content:
160+
'Warning: Invalid prop `invalid` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.',
161+
substitutions: [{length: 7, offset: 23}],
162+
},
163+
});
128164

129165
// The Warning: prefix is added due to a hack in LogBox to prevent double logging.
130-
expect(mockError.mock.calls[0][0].startsWith('Warning: ')).toBe(true);
166+
// We also interpolate the string before passing to the underlying console method.
167+
expect(mockError.mock.calls[0]).toEqual([
168+
expect.stringMatching(
169+
'Warning: Invalid prop `invalid` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.\n at FragmentWithProp',
170+
),
171+
]);
131172
});
132173
});

0 commit comments

Comments
 (0)