1. Reference: In Refactoring: Improving the Design of Existing Code (2nd Edition), test cases are run using Mocha.
2. Mocha allows you to use any assertion library you want. In the example above, we used Node.js’s built-in assert module. I plan to switch to using the Chai assertion library.
3. Reference: https://mochajs.org/#assertions . The Chai assertion library supports expect(), assert(), and should style assertions. See Figure 1.

4. Run the command: npm install chai to install Chai. See Figure 2.

Plaintext
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
JavaScript
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 tests; the tests pass. See Figure 3.

Plaintext
PS E:\wwwroot\refactoring> npm test
> test
> mocha
province
√ shortfall
√ profit
2 passing (13ms)
