Run test cases based on Mocha, using the assertion library Chai
1. Reference: In Refactoring: Improve the Design of Existing Code (2nd Edition), run test cases based on Mocha.
2. MOCHA allows you to use any of the assertion libraries you want. In the above example, we used Node.js’s built-in assertion module. Schedule to use assertion library Chai.
3. Reference:https://mochajs.org/#assertions. The assertion library Chai supports expect(), assert() and should style assertions. as shown in Figure 1
4. Execute the command: npm install chai , install chai. as shown in Figure 2
PS E:\wwwroot\refactoring> npm install chai
added 8 packages, and audited 86 packages in 11s
20 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
5. Edit the test file test/province.js
import {Province} from '../4/modules/province.js';
import chai from 'chai';
function sampleProvinceData() {
return {
name: "Asia",
producers: [{name: "Byzantium", cost: 10, production: 9}, {
name: "Attalia",
cost: 12,
production: 10
}, {name: "Sinope", cost: 10, production: 6},],
demand: 30,
price: 20
};
}
var expect = chai.expect;
describe('province', function() {
it('shortfall', function() {
const asia = new Province(sampleProvinceData());
expect(asia.shortfall).equal(5);
});
it('profit', function() {
const asia = new Province(sampleProvinceData());
expect(asia.profit).equal(230);
});
});
6. Run the test and pass the test. as shown in Figure 3
PS E:\wwwroot\refactoring> npm test
> test
> mocha
province
√ shortfall
√ profit
2 passing (13ms)


