理解 Chai 的断言(预期风格)

Understanding Chai's assertions (expect-style)

这里有什么区别?

it("should be 5", () => {
  expect(num).equal(5);
});

it("should be 5", () => {
  expect(num).to.be.equal(5);
});

我用第一种方式和第二种方式没有区别。至少在我看来是这样。

“.to.be.”的用途是什么?

难道只是为了有更接近真实句子的东西吗?或者它有什么功能吗?

来自doc

The following are provided as chainable getters to improve the readability of your assertions.

看这个测试用例:

import { expect } from 'chai';

const num = 5;

describe('62770625', () => {
  it('should be 5', () => {
    expect(num).equal(5);
  });

  it('should be 5', () => {
    expect(num).to.be.equal(5);
  });

  it('should have members', () => {
    expect([{ a: 1 }]).deep.members([{ a: 1 }]); 
    expect([{ a: 1 }]).to.have.deep.members([{ a: 1 }]);
  });
});

两者都可以,但是有了这些语言链,我们可以得到:

  • 更好的可读性
  • 人性化
  • 良好的语义