chai

Chai is a LIBRARY of BDD/TDD assertions for nodes and browsers that pair perfectly with any javascript testing framework.

What is a BDD/TDD

TDD: Test-Driven Development

Is a technique that uses automated unit testing to drive software design and enforce dependency decoupling. The result of this approach is a comprehensive set of unit tests that can be run at any time to provide feedback that the software is working properly.

BDD:Behavior-Driven Development

BDD combines the general techniques and principles of TDD with the ideas of domain-driven design (DDD). BDD is a design activity that allows you to gradually build blocks of functionality based on expected behavior.

BDD focuses on the languages and interactions used during software development.

Behavior-driven developers use their native language in conjunction with the language of domain-driven design to describe the purpose and benefits of their code.

Use the chai

Run $NPM install chai

Add chai where it is used

import chai from 'chai'
const expect= chai.expect
Copy the code

The function of the three statements is the same, according to their own habits (BDD emphasizes the use of natural language)

expect(foo).to.be.a('string'); // expect(foo).to.equal('bar'); // Expect foo = bar expect(foo).to.have. LengthOf (3); LengthOf (3); // Expect Foo to have lengthOf(3) expect(tea).to.have. Property (' change '). // Tea has attributes that can be changed and lengthOf(3)Copy the code

If the console does not report an error, the test passes