首页 > 解决方案 > Jest:编写所有需要首先断言某些步骤的测试的正确方法是什么?

问题描述

我已经开始开玩笑地编写一些 UI 测试,现在,我遇到了需要测试模态的情况。模态仅在单击按钮后打开。我的代码如下。我想知道我是否可以将getButtonand getModalwith 断言提取到另一个函数中,以便在每次测试之前运行。我想把它放在 beforeEach 中,但我不确定这是否是最佳做法。任何建议都是最受欢迎的。

it('should open Modal when click on button', async () => {
    const button = getButton();
    expect(button.isDisplayed()).to.be.true;

    const modal = getModal();
    expect(modal.isDisplayed()).to.be.true;
});

it('should contain text and close icon', async () => {

    const button = getButton();
    expect(button.isDisplayed()).to.be.true;

    const modal = getModal();
    expect(modal.isDisplayed()).to.be.true;

    /* * some other code....*/

});

标签: jestjs

解决方案


推荐阅读