简体   繁体   中英

Understanding Chai's assertions (expect-style)

What's the difference here?

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

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

It does not make any difference if I use the first way or the second way. At least it seems so to me.

What's the purpose of the ".to.be."?

Is it just for having something which is more similar to an actual sentence? Or does it something functional?

From the doc

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

Look this test case:

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 }]);
  });
});

Both of them work, but with these language Chains, we can get:

  • Better readability
  • Human friendly
  • Good semantics

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM