Files
react/packages/react-native-renderer/src/__tests__/ReactNativeError-test.internal.js
Sebastian Markbåge 98d410f500 Build Component Stacks from Native Stack Frames (#18561)
* Implement component stack extraction hack

* Normalize errors in tests

This drops the requirement to include owner to pass the test.

* Special case tests

* Add destructuring to force toObject which throws before the side-effects

This ensures that we don't double call yieldValue or advanceTime in tests.

Ideally we could use empty destructuring but ES lint doesn't like it.

* Cache the result in DEV

In DEV it's somewhat likely that we'll see many logs that add component
stacks. This could be slow so we cache the results of previous components.

* Fixture

* Add Reflect to lint

* Log if out of range.

* Fix special case when the function call throws in V8

In V8 we need to ignore the first line. Normally we would never get there
because the stacks would differ before that, but the stacks are the same if
we end up throwing at the same place as the control.
2020-04-10 13:32:12 -07:00

90 lines
2.3 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* 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 ReactNative;
let createReactNativeComponentClass;
let computeComponentStackForErrorReporting;
function normalizeCodeLocInfo(str) {
return (
str &&
str.replace(/\n +(?:at|in) ([\S]+)[^\n]*/g, function(m, name) {
return '\n in ' + name + ' (at **)';
})
);
}
describe('ReactNativeError', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactNative = require('react-native-renderer');
createReactNativeComponentClass = require('react-native/Libraries/ReactPrivate/ReactNativePrivateInterface')
.ReactNativeViewConfigRegistry.register;
computeComponentStackForErrorReporting =
ReactNative.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
.computeComponentStackForErrorReporting;
});
it('should throw error if null component registration getter is used', () => {
expect(() => {
try {
createReactNativeComponentClass('View', null);
} catch (e) {
throw new Error(e.toString());
}
}).toThrow(
'View config getter callback for component `View` must be a function (received `null`)',
);
});
it('should be able to extract a component stack from a native view', () => {
const View = createReactNativeComponentClass('View', () => ({
validAttributes: {foo: true},
uiViewClassName: 'View',
}));
const ref = React.createRef();
function FunctionComponent(props) {
return props.children;
}
class ClassComponent extends React.Component {
render() {
return (
<FunctionComponent>
<View foo="test" ref={ref} />
</FunctionComponent>
);
}
}
ReactNative.render(<ClassComponent />, 1);
const reactTag = ReactNative.findNodeHandle(ref.current);
const componentStack = normalizeCodeLocInfo(
computeComponentStackForErrorReporting(reactTag),
);
expect(componentStack).toBe(
'\n' +
' in View (at **)\n' +
' in FunctionComponent (at **)\n' +
' in ClassComponent (at **)',
);
});
});