鉴于:Express应用,AVA,超级测试
什么时候:我测试生成的html作为响应,并且测试用例失败
然后:AVA在控制台中显示整个响应对象,这会减慢对问题的分析
测试示例:
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')
})
控制台中的输出示例
/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
最佳答案
Ava试图帮助调试失败的测试,因此Ava分别放入了控制台
response.res.text
反应
响应
它会在控制台中生成数百甚至数千行
所以解决方案非常简单-使用中间变量进行断言
代替
t.truthy(response.res.text.match(expectedBook), 'Book title is not found')
采用
const text = response.res.text
t.truthy(text.match(expectedBook), 'Book title is not found')
关于javascript - 测试失败时如何防止ava显示长对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50315036/