Cypress.io + 打字稿。测试开始时的断言调用

Cypress.io + TypeScript. Assertion call in beginning of test

我是 Cypress.io 和 TypeScript 的新手。所以我不明白这里的一些东西。

我的代码:

//Test
describe('TEST description', function () {
it('newJobCreation', function () {
    //Some code 1
    var numberBefore = cy.get('#idOfItem')
    var _numberBefore = +numberBefore
    //Some code 2

    var numberAfter = cy.get('#idOfItem')
    var _numberAfter = +numberAfter
    //Assertion
    expect(_numberBefore-1).equals(_numberAfter) //Same result if I use: assert.equal(_numberBefore-1, _numberAfter)
   }) 
})

假设 _numberBefore 在 //一些 code2 被更改并成为 _numberAfter 之后。我想断言这个数字减少了 1。

我在 Cypress.io 中进行 运行 测试后,收到错误消息:

expected NaN to equal NaN

失败了。

问题:

为什么我的断言在执行完所有代码后没有调用?为什么在测试开始时调用它?

Cypress 一次异步地对所有命令进行排队。这意味着

let elem = cy.get("#elem");
// attempt to do something with returned element...

将不起作用。 cy.get() 只是告诉赛普拉斯将 get() 命令添加到最终成为 运行 的命令列表中。它不会立即 运行 命令。

.then() 提供了一个很好的选择——当命令是 运行 时,你可以用它来排队一些 Javascript 成为 运行,像这样:

cy.get("#elem1").then(elem1 => {
    // elem1 is the underlying DOM object.

    // You can put regular javascript code here:
    console.log("This will happen when the queued .then() command is run");

    // You can also put more Cypress commands here, like so:
    cy.get("#elem2").should(elem2 => {
        expect(elem1.someProperty).to.equal(elem2.someProperty);
    });
});

请注意,.should(() => {}) 的行为类似于 .then(),但如果包含的任何断言失败,它将重试。

有关赛普拉斯中异步命令队列的一般概念的详细信息,请参阅here for more info on comparing values of two elements to each other, and see this doc page