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)
Need long-term technical maintenance or remote troubleshooting?
I am a PHP / Go backend engineer with 15+ years of experience, focused on existing system maintenance, bug fixing, performance optimization, server troubleshooting, WordPress maintenance, and small feature iterations.
If your project is facing any of the following issues, we can start with a small troubleshooting task first:
- ✅ PHP / Laravel / Yii2 legacy systems without active maintenance
- ✅ Go / Gin backend APIs that need troubleshooting or optimization
- ✅ Slow, broken, or unstable WordPress websites
- ✅ Nginx / MySQL / Redis / Linux server issues
- ✅ CDN / Cloudflare / DNS / HTTPS configuration problems
- ✅ Long-term remote technical support or part-time maintenance
More details: About Me & Collaboration
WeChat: 13980074657
Email: shuijingwanwq@gmail.com
Telegram: @shuijingwan
GitHub: https://github.com/shuijingwan

Leave a Reply