簡體   English   中英

單元測試沒有任何意義

[英]Unit testing not making any sense

我使用 Jest for React 進行了一些單元測試。

出於某種原因,當我期望它不為空時,它在響應中收到了空值。

describe('Components: FlightSummary', () => {
  it('should render local time by default', () => {
    const { queryByText } = render();

    ['18:10', '18:15', '18:18', '18:23', '18:30', '18:35'].forEach(
      timeValue => {
        expect(queryByText(timeValue)).not.toBeNull();
      }
    );
expect(received).not.toBeNull()

Received: null

  62 |     ['18:10', '18:15', '18:18', '18:23', '18:30', '18:35'].forEach(
  63 |       timeValue => {
> 64 |         expect(queryByText(timeValue)).not.toBeNull();
     |                                            ^
  65 |       }
  66 |     );

當我將代碼更改為期望 null 時,它在響應中收到了一個值。

describe('Components: FlightSummary', () => {
  it('should render local time by default', () => {
    const { queryByText } = render();

    ['18:10', '18:15', '18:18', '18:23', '18:30', '18:35'].forEach(
      timeValue => {
        expect(queryByText(timeValue)).toBeNull();
      }
    );
expect(received).toBeNull()

Received: <div>18:10</div>

  62 |     ['18:10', '18:15', '18:18', '18:23', '18:30', '18:35'].forEach(
  63 |       timeValue => {
> 64 |         expect(queryByText(timeValue)).toBeNull();
     |                                        ^
  65 |       }
  66 |     );

我不知道是怎么回事; 從來沒有見過這個。 托特

在最新版本的 Jest 中添加了describe.eachtest.each ,這將幫助您了解究竟是什么失敗了。

你可以試試這個:

describe('Components: FlightSummary', () => {
  it.each(
    ['18:10', '18:15', '18:18', '18:23', '18:30', '18:35']
  )('should render local time (%s) by default', (timeValue) => {
    const { queryByText } = render();

    expect(queryByText(timeValue)).toBeNull();
  })
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM