英文:
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('contains'))
expect(screen.getByText('contains')).toBeVisible();
this print:
console.log
<div
class="sc-jOhDuK iDFeaX"
>
contains
</div>
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('@testing-library/jest-dom');
答案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 '@testing-library/jest-dom';
or
require('@testing-library/jest-dom');
For example:
import { render, cleanup, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论