简体   繁体   English

单元测试没有任何意义

[英]Unit testing not making any sense

I doing some unit tests using Jest for React.我使用 Jest for React 进行了一些单元测试。

For some reason, when I'm expecting it to not be null, it Received null in the response.出于某种原因,当我期望它不为空时,它在响应中收到了空值。

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 |     );

And when I change the code to expect null, it Received a value in the response.当我将代码更改为期望 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 |     );

I have no idea what's going on;我不知道是怎么回事; have never seen this before.从来没有见过这个。 ToT托特

In a quite recent version of Jest describe.each and test.each were added, which will help you with the visibility of what exactly have failed.在最新版本的 Jest 中添加了describe.eachtest.each ,这将帮助您了解究竟是什么失败了。

You can try this instead:你可以试试这个:

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