简体   繁体   English

React 测试库 - “messageParent”只能在工作人员内部使用

[英]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:在您的终端中检查您的节点版本是什么:

  1. node -v if (version 12) upgrade it! node -v if (version 12) 升级吧!
  2. nvm install v14.8.0 (something like this or the leates one) nvm install v14.8.0 (类似this或leates one)
  3. nvm use v14.8.0 (or any other version you may wish to install that is above 12) nvm 使用 v14.8.0(或任何其他您可能希望安装的高于 12 的版本)

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.

相关问题 Jest messageParent 只能在 worker 内部使用 - Jest messageParent can only be used inside a worker React 测试库 - 组件未使用的 Mock Service Worker 响应 - React Testing Library - Mock Service Worker response not used by component RegEx 可以与 React 测试库一起使用来检查类名吗? - Can RegEx be used with React Testing Library to check classnames? 如何在反应测试库中模拟网络工作者进行测试? - How to mock web worker for testing in react testing library? Jest是仅用于对React Js代码进行单元测试还是可以用于UI测试React Js应用程序? - Whether Jest is used only for unit testing the React Js code or it can be used to UI Test the React Js application? 获取模拟服务工作者的请求体和反应测试库 - Getting request body of mock service worker and react testing library 无法在反应测试库中使用模拟服务工作者 - Unable use mock servise worker in react testing library React 测试库:测试中的更新未包含在 act(...) 中,并且无法对未安装的组件执行 React state 更新 - React testing library: An update inside a test was not wrapped in act(…) & Can't perform a React state update on an unmounted component 如何使用 React 测试库测试 React 门户内部的模式? - How can I test a modal inside of a React Portal using React Testing Library? 使用 React 测试库和 Mock Service Worker 测试 React 模态的最佳实践? - Best practices for testing a React modal with React Testing Library and Mock Service Worker?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM