`.toBeVisible` 不是一个函数

huangapple go评论59阅读模式
英文:

jest and react-test library (...).toBeVisible is not a function

问题

TypeError: expect(...).toBeVisible is not a function

debug(screen.getByText('contains'))
expect(screen.getByText('contains')).toBeVisible();

this print:

console.log
      <div
        class="sc-jOhDuK iDFeaX"
      >
        contains
      </div>
英文:

My unit test for react component throw error even if debug print the correct result.

TypeError: expect(...).toBeVisible is not a function

debug(screen.getByText(&#39;contains&#39;))
expect(screen.getByText(&#39;contains&#39;)).toBeVisible();

this print:

console.log
      &lt;div
        class=&quot;sc-jOhDuK iDFeaX&quot;
      &gt;
        contains
      &lt;/div&gt;

I can't figure out the reason why toBeVisible is not a function even if debug print the correct result.

答案1

得分: 2

你可能在你的package.json文件中缺少@testing-library/jest-dom:

npm i -D @testing-library/jest-dom

如果已经存在,那么你需要在你的jest设置中require它,以便使用toBeVisible:

require('testing-library/jest-dom');
英文:

You might be missing @testing-library/jest-dom in your package.json file:

npm i -D @testing-library/jest-dom

If it is present, then you need to require it in your jest setup to be able to use toBeVisible:

require(&#39;@testing-library/jest-dom&#39;);

答案2

得分: 2

你需要在你的单独测试文件中导入或引入。

    import '@testing-library/jest-dom';
或者

    require('@testing-library/jest-dom');

例如:

    import { render, cleanup, screen } from '@testing-library/react';
    import '@testing-library/jest-dom';
英文:

You will need to import or require in your individual test file.

import &#39;@testing-library/jest-dom&#39;;

or

require(&#39;@testing-library/jest-dom&#39;);

For example:

import { render, cleanup, screen } from &#39;@testing-library/react&#39;;
import &#39;@testing-library/jest-dom&#39;;

答案3

得分: 0

我使用 PNPM 并且使用 pnpm add -D @types/testing-library__jest-dom 进行安装对我有效。

英文:

I use PNPM and installing with pnpm add -D @types/testing-library__jest-dom worked for me.

huangapple
  • 本文由 发表于 2023年2月6日 17:22:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定