Модульное тестирование не имеет никакого смысла - PullRequest
0 голосов
/ 27 марта 2020

Я делаю некоторые юнит-тесты, используя 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 |     );

И когда я изменяю код на ожидаемый нулевой, он получает значение в ответе.

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

Я понятия не имею, что происходит; никогда не видел этого раньше. ToT

1 Ответ

0 голосов
/ 27 марта 2020

В совсем недавней версии Jest describe.each и test.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();
  })
})
...