[英]React Testing Library - "messageParent" can only be used inside a worker
I'm testinng a react component using the RTL, and everytime I ran a test, I get,我正在使用 RTL 测试一个反应组件,每次我运行测试时,我都会得到,
"messageParent" can only be used inside a worker
**Here's the code **这是代码
describe('Header', () => {
it('validates header component is rendered', () => {
const { getByTestId } = render(<Header patientName="Jake Bland" />);
expect(getByTestId('patient-name')).toHaveTextContent(/^Jake Bland$/);
expect(getByTestId('dateRange')).toBe(dateRange);
});
});
Any help on this will be greatly appreciated.对此的任何帮助将不胜感激。
I was having the same issue, and it was caused by calling toBe()
or toEqual()
on an HTMLElement.我遇到了同样的问题,它是由在 HTMLElement 上调用toBe()
或toEqual()
引起的。 So your most likely culprit is here, since getByTestId
returns an HTMLElement:所以你最有可能的罪魁祸首在这里,因为getByTestId
返回一个 HTMLElement:
expect(getByTestId('dateRange')).toBe(dateRange);
I fixed my issue by testing against the element's text content instead.我通过测试元素的文本内容来解决我的问题。
expect(getByTestId('dateRange')).toHaveTextContent('...');
Hi I had the same issue after spending some time googling around i found this: github issues forum嗨,我花了一些时间谷歌搜索后遇到了同样的问题,我发现了这个: github 问题论坛
it turned out that this issue is caused by node12 version i downgraded my one to version 10 and everything worked i have also upgraded to version 14 and that also works.事实证明,这个问题是由 node12 版本引起的,我将我的版本降级到版本 10,一切正常,我也升级到版本 14,这也有效。
check what is yours node version in ur terminal:在您的终端中检查您的节点版本是什么:
hopefully this helps希望这会有所帮助
I had the same bug but I was able to fix it by importing '@testing-library/jest-dom'
as follows in my test file:我有同样的错误,但我能够通过在我的测试文件中导入'@testing-library/jest-dom'
来修复它,如下所示:
import '@testing-library/jest-dom'
It turned out that the package above is a peerDependency for '@testing-library/react'.原来,上面的 package 是 '@testing-library/react' 的 peerDependency。 Thus the error.因此错误。
I ran into this error while testing to see if a <select>
had the right number of options, when using this:我在测试以查看<select>
是否有正确数量的选项时遇到了这个错误,当使用这个时:
expect(container.querySelectorAll('option')).toBe(2);
@testing-library/react must be trying to do something strange to get the length of the HTML collection, I have. @testing-library/react 必须尝试做一些奇怪的事情来获取 HTML 集合的长度,我有。 So I added .length
to the query:所以我在查询中添加了.length
:
expect(container.querySelectorAll('option').length).toBe(2);
All was well.一切都好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.