vue.nextTick ложноположительный - PullRequest
0 голосов
/ 11 июня 2019

контекст:

const Constructor = Vue.extend(MyComponent);
function createComponent() {
    vm = new Constructor({
      props,
    }).$mount();

    return vm;
  }

Вопрос: На тестах я нахожу

vm.$nextTick().then(() => {
 expect(result).to.equal(expectedResult);
})

и

vm.$nextTick().then(() => {
 expect(result).to.not.equal(expectedResult);
})

оба проходят. Как избавиться от этой ситуации? Будет ли aync ждать когда-нибудь, чтобы убедиться, что правда только проходит?

1 Ответ

0 голосов
/ 11 июня 2019

Во-первых: то, что вы можете отрицать любое утверждение с помощью .not, не означает, что вы должны это делать. С большой властью приходит большая ответственность. Часто лучше утверждать, что был получен один ожидаемый результат, а не утверждать, что один из бесчисленных неожиданных результатов не был получен.

Равен утверждает, что цель строго (===) равна данному значению.

expect(2).to.equal(2); // Recommended
expect(2).to.not.equal(1); // Not recommended

Хорошо, теперь, в вашем случае, вы можете добавить .deep ранее в цепочке, чтобы использовать вместо него глубокое равенство:

// Target object deeply (but not strictly) equals `{a: 1}`
expect({a: 1}).to.deep.equal({a: 1});
expect({a: 1}).to.not.equal({a: 1});

// Target array deeply (but not strictly) equals `[1, 2]`
expect([1, 2]).to.deep.equal([1, 2]);
expect([1, 2]).to.not.equal([1, 2]);

Объяснение, почему это просто:

1 === 1 // These are primitives, they hold the same reference - they are strictly equal
1 == '1' // These are two different primitives, through type coercion they hold the same value - they are loosely equal
{ a: 1 } !== { a: 1 } // These are two different objects, they hold different references and so are not strictly equal - even though they hold the same values inside
{ a: 1 } != { a: 1 } // They have the same type, meaning loose equality performs the same check as strict equality - they are still not equal.

Chai "to.equal" использует алгоритм "deep-eql" => package .

var deepEql = require("deep-eql");
deepEql({ a: 1 }, { a: 1 }) === true // deepEql can determine that they share the same keys and those keys share the same values, therefore they are deeply equal!

Хотите узнать больше о методах чай? Их документы удивительны.

...