skip to content

Jest
使用 Jest Mock React 组件中使用的变量

isMobileScreen<ComponentToTest/> 中 使用的变量

import * as React from 'react'
import { render, screen, waitFor } from '@testing-library/react'
import '@testing-library/jest-dom/extend-expect'
import { ComponentToTest } from './index'

jest.mock('@packages/utils/matchers', () => {
  return {
    isMobileScreen: true
  }
})

describe('ComponentToTest | Mobile', () => {
  it('在 Mobile 端默认不展开', async () => {
    render(<ComponentToTest />)
    await waitFor(
      () => {
        expect(screen.getByTestId('test-id')).not.toHaveClass('test-classname')
      },
      {
        timeout: 5000
      }
    )
  })
})

React:React Testing Library(RTL)教程