如何在Test Cafe错误中获取完整的堆栈跟踪

5

我一直在使用Test Cafe编写内部测试框架,其中的操作(t.click)和断言(t.expect)不是直接写在规范中,而是在其他文件中定义和聚合。

一切都很顺利,直到一个测试没有失败:在这种情况下,Test Cafe报告器会在控制台中写入断言/操作失败和相关代码片段,但我没有找到理解从我的测试到失败断言的完整函数调用堆栈的方法。

如何确保在报告器中提供完整的堆栈跟踪,记录使我的测试失败的所有函数调用的堆栈跟踪?

我明白原因应该与async/await如何转译成生成器有关:错误的堆栈跟踪仅显示最后执行的await,而不是所有先前的调用。

<section> ... </section>
<section class="section--modifier">
  <h1> ... </h1>
  <div>
    ...
    <button class="section__button">
      <div class="button__label">
        <span class="label__text">Hello!</span> <-- Target of my test
      </div>
    </button>
    ...
  </div>
</section>
<section> ... </section>

// 
// My spec file
//
import { Selector } from 'testcafe';

import {
  verifyButtonColor
} from './button';

fixture`My Fixture`
  .page`....`;

test('Test my section', async (t) => {
  const MySection = Selector('.section--modifier');
  const MyButton1 = MySection.find('.section__button');

  const MySection2 = Selector('.section--modifier2');
  const MyButton2 = MySection2.find('.section__button');

  ....
  await verifyButtonColor(t, MyButton1, 'green'); // it will fail!
  ....
  ....
  ....
  await verifyButtonColor(t, MyButton2, 'green');
});

//
// Definition of assertion verifyButtonColor (button.js)
//
import { Selector } from 'testcafe';

import {
  verifyLabelColor
} from './label';

export async function verifyButtonColor(t, node, expectedColor) {
   const MyLabel = node.find('.button__label');
   await verifyLabelColor(t, MyLabel, expectedColor);
}

// 
// Definition of assertion verifyLabelColor (label.js)
//
export async function verifyLabelColor(t, node, expectedColor) {
   const MyText= node.find('.label__text');
   const color = await MyText.getStyleProperty('color');

   await t.expect(color).eql(expectedColor, `Color should be ${expectedColor}, found ${color}`); // <-- it will FAIL!
}

我在报告中看到的是,我的测试失败了,因为在“verifyLabelColor”中定义的断言失败了(颜色不是绿色 :(),

...
await t.expect(color).eql(expectedColor, `Color should be ${expectedColor}, found ${color}`);
...

但是在报告中,我没有证据表明失败是由于以下一系列调用堆栈所导致的。

- await verifyButtonColor(t, MyButton1, 'green');
- await verifyLabelColor(t, MyLabel, expectedColor);
- await t.expect(color).eql(expectedColor, `Color should be ${expectedColor}, found ${color}`);

有人遇到过类似的问题吗?

另一种方法是记录导致失败的选择器的“路径”,但是在查看Test Cafe文档时,我没有找到这样做的可能性:知道断言失败在以下路径元素上可以帮助理解出了什么问题。

.section--modifier .section__button .button__label .label__text
1个回答

1

1
显然,自定义报告器也无法从引擎中获取足够的信息来改进这些堆栈跟踪(请参见您链接的错误报告上的最后一条评论)。 - Ben

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接