因为enzyme没有维护并且不支持react 18+,所以我正在尝试将1750多个现有的单元测试迁移到react-testing-library + global-jsdom来运行,这样我们的应用程序就可以继续运行最新版本的react。我们所有的测试都是使用mocha、chai、enzyme编写的,我希望尽可能简单地迁移。换句话说,我绝不会在一个全新的框架如jest中重写1750多个测试。
我正在尝试按照使用react-testing-library来对react组件进行单元测试的示例。如果我在使用React.createElement时使用简单的元素,比如'div'或'input',它就能正常工作,但是当我使用material UI组件时,就会出现错误:
类型错误:无法读取 null 的属性(读取“已注册”) 在 C:\Users\user\Documents\project\node_modules@emotion\styled\base\dist\emotion-styled-base.cjs.dev.js:143:53
上述错误发生在
组件中: at C:\Users\user\Documents\project\node_modules\@emotion\react\dist\emotion-element-b63ca7c6.cjs.dev.js:43:23 at Box (C:\Users\user\Documents\project\node_modules\@mui\system\createBox.js:29:41) at DummyComponent (C:\Users\user\Documents\project\act-app\src\component\page\dummyComponent.js:2:346)考虑为您的树添加错误边界以自定义错误处理行为。访问https://reactjs.org/link/error-boundaries了解更多关于错误边界的信息。 堆栈跟踪不准确,但它在我尝试执行
h(Box, {},...)(创建mui元素)时失败。
这是我正在尝试渲染的dummyComponent.js:
const { Box } = require('@mui/material');
const React = require('react');
const h = React.createElement;
const DummyComponent = (props) => {
const { children } = props;
const [showChild, setShowChild] = React.useState(false);
return (
h(Box, {},
h('label', { htmlFor: 'toggle' }, 'Show/Hide'),
h('input', { id: 'toggle', type: 'checkbox', onChange: (e) => setShowChild(e.target.checked), checked: showChild }),
showChild && children)
);
};
module.exports = DummyComponent;
这是mocha单元测试:
const React = require('react');
const { render, fireEvent } = require('@testing-library/react');
const h = React.createElement;
const DummyComponent = require('./dummyComponent');
describe('pageCenter', function () {
before(function () {
this.jsdom = require('global-jsdom')();
});
after(function () {
this.jsdom();
});
it('should render', function () {
const w = render(h(DummyComponent, {}, 'Hi!'));
w.queryAllByText('Hi!').should.have.length(0);
fireEvent.click(w.getByLabelText('Show/Hide'));
w.queryAllByText('Hi!').should.have.length(1);
});
});
感觉好像我缺少一些上下文来允许MUI组件渲染,但我似乎无法弄清楚是什么,或者这是否实际上是问题。关于这个特定错误的谷歌搜索结果不多。有什么想法吗?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号