Files
react/packages/shared/__tests__/describeComponentFrame-test.js
Andrew Clark 952aa74f8e Upgrade tests to use react/jsx-runtime (#28252)
Instead of createElement.

We should have done this when we initially released jsx-runtime but
better late than never. The general principle is that our tests should
be written using the most up-to-date idioms that we recommend for users,
except when explicitly testing an edge case or legacy behavior, like for
backwards compatibility.

Most of the diff is related to tweaking test output and isn't very
interesting.

I did have to workaround an issue related to component stacks. The
component stack logic depends on shared state that lives in the React
module. The problem is that most of our tests reset the React module
state and re-require a fresh instance of React, React DOM, etc. However,
the JSX runtime is not re-required because it's injected by the compiler
as a static import. This means its copy of the shared state is no longer
the same as the one used by React, causing any warning logged by the JSX
runtime to not include a component stack. (This same issue also breaks
string refs, but since we're removing those soon I'm not so concerned
about that.) The solution I went with for now is to mock the JSX runtime
with a proxy that re-requires the module on every function invocation. I
don't love this but it will have to do for now. What we should really do
is migrate our tests away from manually resetting the module state and
use import syntax instead.
2024-02-05 23:07:41 -05:00

138 lines
3.9 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and 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
*/
'use strict';
let React;
let ReactDOMClient;
let act;
let jsxDEV;
describe('Component stack trace displaying', () => {
beforeEach(() => {
React = require('react');
ReactDOMClient = require('react-dom/client');
act = require('internal-test-utils').act;
jsxDEV = require('react/jsx-dev-runtime').jsxDEV;
});
// @gate !enableComponentStackLocations
// @gate __DEV__
it('should provide filenames in stack traces', async () => {
class Component extends React.Component {
render() {
return [<span>a</span>, <span>b</span>];
}
}
spyOnDev(console, 'error');
const container = document.createElement('div');
const fileNames = {
'': '',
'/': '',
'\\': '',
Foo: 'Foo',
'Bar/Foo': 'Foo',
'Bar\\Foo': 'Foo',
'Baz/Bar/Foo': 'Foo',
'Baz\\Bar\\Foo': 'Foo',
'Foo.js': 'Foo.js',
'Foo.jsx': 'Foo.jsx',
'/Foo.js': 'Foo.js',
'/Foo.jsx': 'Foo.jsx',
'\\Foo.js': 'Foo.js',
'\\Foo.jsx': 'Foo.jsx',
'Bar/Foo.js': 'Foo.js',
'Bar/Foo.jsx': 'Foo.jsx',
'Bar\\Foo.js': 'Foo.js',
'Bar\\Foo.jsx': 'Foo.jsx',
'/Bar/Foo.js': 'Foo.js',
'/Bar/Foo.jsx': 'Foo.jsx',
'\\Bar\\Foo.js': 'Foo.js',
'\\Bar\\Foo.jsx': 'Foo.jsx',
'Bar/Baz/Foo.js': 'Foo.js',
'Bar/Baz/Foo.jsx': 'Foo.jsx',
'Bar\\Baz\\Foo.js': 'Foo.js',
'Bar\\Baz\\Foo.jsx': 'Foo.jsx',
'/Bar/Baz/Foo.js': 'Foo.js',
'/Bar/Baz/Foo.jsx': 'Foo.jsx',
'\\Bar\\Baz\\Foo.js': 'Foo.js',
'\\Bar\\Baz\\Foo.jsx': 'Foo.jsx',
'C:\\funny long (path)/Foo.js': 'Foo.js',
'C:\\funny long (path)/Foo.jsx': 'Foo.jsx',
'index.js': 'index.js',
'index.jsx': 'index.jsx',
'/index.js': 'index.js',
'/index.jsx': 'index.jsx',
'\\index.js': 'index.js',
'\\index.jsx': 'index.jsx',
'Bar/index.js': 'Bar/index.js',
'Bar/index.jsx': 'Bar/index.jsx',
'Bar\\index.js': 'Bar/index.js',
'Bar\\index.jsx': 'Bar/index.jsx',
'/Bar/index.js': 'Bar/index.js',
'/Bar/index.jsx': 'Bar/index.jsx',
'\\Bar\\index.js': 'Bar/index.js',
'\\Bar\\index.jsx': 'Bar/index.jsx',
'Bar/Baz/index.js': 'Baz/index.js',
'Bar/Baz/index.jsx': 'Baz/index.jsx',
'Bar\\Baz\\index.js': 'Baz/index.js',
'Bar\\Baz\\index.jsx': 'Baz/index.jsx',
'/Bar/Baz/index.js': 'Baz/index.js',
'/Bar/Baz/index.jsx': 'Baz/index.jsx',
'\\Bar\\Baz\\index.js': 'Baz/index.js',
'\\Bar\\Baz\\index.jsx': 'Baz/index.jsx',
'C:\\funny long (path)/index.js': 'funny long (path)/index.js',
'C:\\funny long (path)/index.jsx': 'funny long (path)/index.jsx',
};
const root = ReactDOMClient.createRoot(container);
let i = 0;
for (const fileName in fileNames) {
Component.displayName = 'Component ' + i;
await act(() => {
root.render(
// Intentionally inlining a manual jsxDEV() instead of relying on the
// compiler so that we can pass a custom source location.
jsxDEV(
Component,
{},
undefined,
false,
{fileName, lineNumber: i},
this,
),
);
});
i++;
}
if (__DEV__) {
i = 0;
expect(console.error).toHaveBeenCalledTimes(
Object.keys(fileNames).length,
);
for (const fileName in fileNames) {
if (!fileNames.hasOwnProperty(fileName)) {
continue;
}
const args = console.error.mock.calls[i];
const stack = args[args.length - 1];
const expected = fileNames[fileName];
expect(stack).toContain(`at ${expected}:`);
i++;
}
}
});
});