0

Given: Express app, AVA, supertest

When: I test generated html in response and the test case fails

Then: AVA displays the whole response object in the console which slows down analysis of the issue

Example of the test:

test('Positive case: book is found in /api/v1/books/1', async (t) => {
  t.plan(2)
  const response = await request(app).get('/api/v1/books/1')
  t.is(response.status, 200)
  const expectedBook = '<h3>Book 1</h3>'
  t.truthy(response.res.text.match(expectedBook), 'Book title is not found')
})

Example of the output in the console

/express-project/src/books/index.test.js:22

   21:   const text = response.res.text
   22:   t.truthy(response.res.text.match(expectedBook), 'Book t…
   23:   })

  Book title is not found

  Value is not truthy:

  null

  response.res.text.match(expectedBook)
  => null

  expectedBook
  => '<h3>Book 2</h3>'

  response.res.text
  => '<!DOCTYPE html><html><head><title>BOOK</title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>BOOK</h1>
<h3>Book 1</h3><h4></h4></body></html>'

  response.res
  => IncomingMessage {
    _consuming: false,
    _dumped: false,
    _events: {
      close: Function bound emit {},
      data: [
        Function {},
        Function {},
        Function bound emit {},
      ],
      end: [
        Function responseOnEnd {},
        Function {},
        Function bound emit {},
      ],
      error: [
        Function bound onceWrapper { … },
        Function bound emit {},
      ],
    },
    _eventsCount: 4,
    _maxListeners: undefined,
    _readableState: ReadableState [
      awaitDrain: 0,

    ..........  VERY LONG LIST WITH HUNDREDS OF LINES
    SO HAVE TO SCROLL UP AND UP AND UP BEFORE YOU GET TO THE POINT
Sergiy Seletskyy
  • 16,236
  • 7
  • 69
  • 80

1 Answers1

1

Ava is trying to be helpful in debugging the failed test so Ava puts in the console respectively

  • response.res.text
  • response.res
  • response

and it generates hundreds of even thousands of lines in the console

So the solution is pretty simple - use an intermediate variable for the assertion

Instead of

t.truthy(response.res.text.match(expectedBook), 'Book title is not found')

use

const text = response.res.text
t.truthy(text.match(expectedBook), 'Book title is not found')
Sergiy Seletskyy
  • 16,236
  • 7
  • 69
  • 80