链接诺言时触发断言为时过早

时间:2019-05-22 13:32:02

标签: javascript cypress

在Cypress中运行测试并链接多个Promises(我用cypress-promise封装它们,这只是new Cypress.Promise的语法糖)时,我的断言被触发得太早了,这会带来错误的结果。

我希望执行顺序如下:

  1. checkAutoSuggest
  2. checkIfSelectedValueMatches
  3. clearFilter
  4. checkOffer已解决,checkOffer.then的正文已准备好执行

相反,它会触发:

  1. checkAutoSuggest
  2. checkOffer已解决(它不等待链接的Promises,并且cy.log('checkOffer is now resolved')出现在整个测试执行的中间,而不是最后)。
  3. checkIfSelectedValueMatches
  4. clearFilter

已解决的响应的值来自最后一个承诺。

it('Approvals', function () {
    cy.visit('/approvals');
    openFilters();

    const checkOffer = (async () => {
        return await promisify(
            checkAutoSuggest('#offer__name', 'test', '**/offers?limit=200&name=test')
                .then((selectedOffer) => checkIfSelectedValueMatches('#offer__name', selectedOffer))
                .then(() => clearFilter('#offer__name', 'test', 'Offer', '**offset=0&limit=10&sort=-created'))
        );
    })();

    checkOffer.then(response => {
        console.log(response) // returns value resolved from clearFilter promise (correct)
        cy.log('checkOffer is now resolved') // appears right after checkAutoSuggest - too early!
        expect(cy.state('requests').filter(request => request.alias === 'loadList').length).to.equal(4); // appears right after checkAutoSuggest - too early!
    });
});

0 个答案:

没有答案