开玩笑,React测试 - 组件定义缺少显示名称
IM遇到错误:组件定义缺少显示名称在我的React jest Test 中。我发现了有关此问题的不同问题和答案,但是这些答案都不对我的测试有用。有人是否遇到相同的错误并知道如何解决? 这是我的代码:
const mockPdfCountriesTable = jest.fn();
jest.mock(
'@components/admin/PdfReport/PdfCountriesPage/PdfCountriesPageTable',
() => (props) => {
mockPdfCountriesTable(props);
return <mockPdfCountriesTable />;
}
);
describe('PdfReport', () => {
let wrapper;
let sortedCountries;
let attendeeCountries;
beforeEach(() => {
const scaleLinear = jest.fn();
const geoMercator = jest.fn();
const getSupportedLanguage = jest.fn();
scaleLinear();
geoMercator();
getSupportedLanguage();
attendeeCountries = {
US: {
value: 3324,
alpha3: 'USA',
},
RU: {
value: 90,
alpha3: 'RUS',
},
CN: {
value: 675,
alpha3: 'CHN',
},
};
sortedCountries = [
{
value: 3324,
alpha3: 'USA',
},
{
value: 675,
alpha3: 'CHN',
},
{
value: 90,
alpha3: 'RUS',
},
],
wrapper = mount(
<PdfCountriesPage attendeeCountries={attendeeCountries} />
);
});
it('Renders <PdfCountriesPageTable/> child component props', () => {
expect(mockPdfCountriesTable).toHaveBeenCalledWith(
expect.objectContaining({
sortedCountriesArray: sortedCountries,
})
);
});
});
谢谢!
Im getting the the error: Component definition is missing display name in my react jest test. I found different questions and answers about this but none of this answers were useful for my test. Did someone experience the same error and know how to fix it?
Here is my code:
const mockPdfCountriesTable = jest.fn();
jest.mock(
'@components/admin/PdfReport/PdfCountriesPage/PdfCountriesPageTable',
() => (props) => {
mockPdfCountriesTable(props);
return <mockPdfCountriesTable />;
}
);
describe('PdfReport', () => {
let wrapper;
let sortedCountries;
let attendeeCountries;
beforeEach(() => {
const scaleLinear = jest.fn();
const geoMercator = jest.fn();
const getSupportedLanguage = jest.fn();
scaleLinear();
geoMercator();
getSupportedLanguage();
attendeeCountries = {
US: {
value: 3324,
alpha3: 'USA',
},
RU: {
value: 90,
alpha3: 'RUS',
},
CN: {
value: 675,
alpha3: 'CHN',
},
};
sortedCountries = [
{
value: 3324,
alpha3: 'USA',
},
{
value: 675,
alpha3: 'CHN',
},
{
value: 90,
alpha3: 'RUS',
},
],
wrapper = mount(
<PdfCountriesPage attendeeCountries={attendeeCountries} />
);
});
it('Renders <PdfCountriesPageTable/> child component props', () => {
expect(mockPdfCountriesTable).toHaveBeenCalledWith(
expect.objectContaining({
sortedCountriesArray: sortedCountries,
})
);
});
});
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
禁用ESLINT规则绝不是这样的问题,对于此类问题,在您编写组件测试时,在整个项目过程中,一遍又一遍地出现。另外,您应该避免像示例一样避免进行测试道具,因为这是实现细节,如果可能的话,您应该使用RTL,但这是另一个故事。
无论如何,我有一个建议是:
这将使之成为现实,因此显示名称是可以模拟的,并且您不必禁用有用的ESLINT规则。
Disabling an eslint rule is never a good solution for an issue like this which will come up over and over throughout the course of a project as you write component tests. Also, you should avoid testing props as in your example since that is an implementation detail, you should use RTL if possible but that is another story.
Anyway, one suggestion I have is this:
This will make it so the display name is MockTable and you will not have to disable useful eslint rules.
DisplayName属性用于给出React DevTools扩展名的描述性名称,并且在进行测试时,不需要。因此,删除错误的最简单方法是标记此函数,以便ESLINT忽略缺少的显示名称。为此,您可以在
jest.fn()
line上方直接添加以下注释:// eslint-disable-next-line react/display-name
,因此您的测试将其更改为:
有关此方法还有其他方法,但是为了简单,我只会对其进行评论。如果您想要更多详细信息,请查看这篇文章,这就是我遇到此解决方案的方式:
https://bobbyhadz.com/blog/react-component-is-缺少display-name
hth
The displayName property is used to give a descriptive name for the React devtools extension and as you are running a test, this is not needed. Therefore the simplest way to remove the error is to mark this function so that eslint ignores the missing displayName. To do this you can add the following comment directly above the
jest.fn()
line:// eslint-disable-next-line react/display-name
So, for your test you would change it to:
There are other ways around this, but for simplicity in the test I would just comment it out. If you want more detailed info, check out this post, which is how I came across this solution:
https://bobbyhadz.com/blog/react-component-is-missing-display-name
HTH