reactjs - Expect(...).toBeInTheDocument 设置后不是函数

标签 reactjs jestjs react-testing-library formik ts-jest

我已经安装了'@testing-library/jest-dom',所以它应该在那里。还有以下导入,我的配置有什么问题吗?

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

      52 |     renderIt(onSubmit);
      53 |
    > 54 |     expect(screen.getByText(/alberta/i)).toBeInTheDocument();

我的测试:

import { render, screen } from '@testing-library/react';
import { Formik, Form } from 'formik';

import { Select } from '.';

const options = [
  'Alberta',
  'British Columbia',
  'Manitoba',
  'New Brunswick',
  'Newfoundland & Labrador',
  'Northwest Territories',
  'Nova Scotia',
  'Nunavut',
  'Ontario',
  'Prince Edward Island',
  'Quebec',
  'Saskatchewan',
  'Yukon',
];

const renderIt = (onSubmit: () => void) => render(
  <Formik
    initialValues={{ province: '' }}
    onSubmit={onSubmit}
  >
    <Form>
      <Select name="province" options={options} />
    </Form>
  </Formik>,
);

describe('Select component', () => {
  test('It renders', () => {
    const onSubmit = jest.fn();
    renderIt(onSubmit);

    expect(screen.getByText(/alberta/i)).toBeInTheDocument();
  });
});

我的src/setupTests.ts文件:

// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';
import '@testing-library/jest-dom/extend-expect';

package.json:

  "devDependencies": {
    "@testing-library/dom": "8.11.3",
    "@testing-library/jest-dom": "5.16.2",
    "@testing-library/react": "^13.0.0-alpha.6",
    "@testing-library/react-hooks": "7.0.2",
    "@testing-library/user-event": "13.5.0",
    "@types/jest": "27.4.1",
    "jest": "27.5.1",
    "ts-jest": "27.1.3",
    "typescript": "4.6.2"
  },

jest.config.js:

module.exports = {
  testEnvironment: 'jsdom',
  preset: 'ts-jest',
  moduleNameMapper: {
    '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
      '<rootDir>/__mocks__/fileMock.js',
    '\\.(css|less)$': 'identity-obj-proxy',
  },
};

最佳答案

您应该使用 setupFilesAfterEnv 在 Jest 配置中声明 setupTests.ts属性。

jest.config.js

module.exports = {
  testEnvironment: 'jsdom',
  setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],
  preset: 'ts-jest',
  moduleNameMapper: {
    '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
      '<rootDir>/__mocks__/fileMock.js',
    '\\.(css|less)$': 'identity-obj-proxy',
  },
};

关于reactjs - Expect(...).toBeInTheDocument 设置后不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71589882/

相关文章:

javascript - React.js 从 AWS 读取用户信息并设置变量状态

javascript - Jest 一次性模拟多个组件

typescript - 如何使用 jest 测试 Apollo Server 订阅?

reactjs - 测试 --coverage 抛出 "Nothing was returned from render."错误

reactjs - 语法错误 : Unexpected token, 预期 "</>"

javascript - 如何使用 firestore orderBy 但同时添加未定义的值

javascript - 当前值未反射(reflect)在 reactjs 选择组件中

javascript - Flow 的 $ElementType 的奇怪行为

javascript - 我们如何与 npm start 或 npm build 一起运行 jest 单元测试?

reactjs - 为什么在我使用 React 测试库的测试中没有执行 jest 模拟函数?