There is no problem not worth solving, and no technology not worth learning!

Run test cases based on Mocha in Refactoring: Improved Design of Existing Code (2nd Edition)

再次运行测试,测试通过

1. Search in Githab: mocha, select the first one: mochajs/mocha, a simple, flexible and interesting JavaScript test framework for node.js and browsers. as shown in Figure 1

在 Githab 中搜索:Mocha,选择第一个:mochajs/mocha,用于 node.js 和浏览器的简单、灵活、有趣的 javascript 测试框架
Figure 1

2. Click the link documentation to enterhttps://mochajs.org/

3. Reference installation:https://mochajs.org/#installation, as a project development dependency, use npm to install, enter the project directory: npm install –save-dev mocha. as shown in Figure 2

参考安装:https://mochajs.org/#installation ,作为项目的开发依赖项,使用 npm 安装,进入项目目录:npm install --save-dev mocha
Figure 2

PS E:\wwwroot\refactoring> npm install --save-dev mocha

added 77 packages, and audited 78 packages in 20s

20 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
PS E:\wwwroot\refactoring> ls


    目录: E:\wwwroot\refactoring


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2022/12/28     20:47                .idea
d-----       2022/12/26     20:52                1
d-----       2022/12/28     20:47                4
d-----       2022/12/28     20:50                node_modules
-a----         2022/7/4     21:42              7 .gitignore
-a----       2022/12/28     20:50          57330 package-lock.json
-a----       2022/12/28     20:50             54 package.json
-a----        2022/6/23     22:17             32 README.md



4. Reference:https://mochajs.org/#getting-started。运行:./node_modules/mocha/bin/mocha,报错:WindowsScript Host, script: E:\wwwroot\refactoring\node_modules\mocha\bin\mocha.js, error: invalid character. as shown in Figure 3

参考:https://mochajs.org/#getting-started。运行:./node_modules/mocha/bin/mocha,报错:Windows Script Host,脚本:E:\wwwroot\refactoring\node_modules\mocha\bin\mocha.js,错误:无效字符
Figure 3

5. Set the test script in package.json:


{
  "devDependencies": {
    "mocha": "^10.2.0"
  },
  "scripts": {
    "test": "mocha"
  }
}



6. Then run the test: npm test, run normally. as shown in Figure 4

然后运行测试:npm test,运行正常
Figure 4

PS E:\wwwroot\refactoring> npm test

> test
> mocha



  Array
    #indexOf()
      √ should return -1 when the value is not present


  1 passing (14ms)

PS E:\wwwroot\refactoring>


7. Delete the file: test/test.js, create a new test file: test/province.js


import {Province} from '../4/modules/province.js';

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

describe('province', function () {
    it('shortfall', function () {
        const asia = new Province(sampleProvinceData());
        assert.equal(asia.shortfall, 5);
    });
});


8. Run test error: (Node:5740) warning: to load an es module, set “type”: “module” in the package.json or use the .mjs extension.. as shown in Figure 5

运行测试报错:(node:5740) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
Figure 5

PS E:\wwwroot\refactoring> npm test

> test
> mocha

(node:19240) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)

E:\wwwroot\refactoring\test\province.js:1
import {Province} from '../4/modules/province.js';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1088:15)
    at Module._compile (node:internal/modules/cjs/loader:1123:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at ModuleWrap. (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:438:15)
    at async formattedImport (E:\wwwroot\refactoring\node_modules\mocha\lib\nodejs\esm-utils.js:9:14)
    at async exports.requireOrImport (E:\wwwroot\refactoring\node_modules\mocha\lib\nodejs\esm-utils.js:42:28)
    at async exports.loadFilesAsync (E:\wwwroot\refactoring\node_modules\mocha\lib\nodejs\esm-utils.js:100:20)
    at async singleRun (E:\wwwroot\refactoring\node_modules\mocha\lib\cli\run-helpers.js:125:3)
    at async exports.handler (E:\wwwroot\refactoring\node_modules\mocha\lib\cli\run.js:370:5)
PS E:\wwwroot\refactoring>



9. Edit package.json, add “type”: “module”


{
  "devDependencies": {
    "mocha": "^10.2.0"
  },
  "scripts": {
    "test": "mocha"
  },
  "type": "module"
}



10. Run the test again, and report an error: ReferenceError: Assert is not defined. as shown in Figure 6

再次运行测试,报错:ReferenceError: assert is not defined
Figure 6

PS E:\wwwroot\refactoring> npm test

> test
> mocha



  province
    1) shortfall


  0 passing (10ms)
  1 failing

  1) province
       shortfall:
     ReferenceError: assert is not defined
      at Context. (file:///E:/wwwroot/refactoring/test/province.js:19:9)
      at process.processImmediate (node:internal/timers:471:21)





11. Edit the test file: test/province.js, add var assert = require(assert);


import {Province} from '../4/modules/province.js';
var assert = require('assert');

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

describe('province', function () {
    it('shortfall', function () {
        const asia = new Province(sampleProvinceData());
        assert.equal(asia.shortfall, 5);
    });
});


12. Run the test again and report an error: ReferenceError: Require is not defined within the scope of the ES module, you can use import instead. as shown in Figure 7

再次运行测试,报错:ReferenceError: require 未在 ES 模块范围内定义,你可以改用导入
Figure 7

PS E:\wwwroot\refactoring> npm test

> test
> mocha


ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'E:\wwwroot\refactoring\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///E:/wwwroot/refactoring/test/province.js:2:14
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:438:15)
    at async formattedImport (E:\wwwroot\refactoring\node_modules\mocha\lib\nodejs\esm-utils.js:9:14)
    at async exports.requireOrImport (E:\wwwroot\refactoring\node_modules\mocha\lib\nodejs\esm-utils.js:42:28)
    at async exports.loadFilesAsync (E:\wwwroot\refactoring\node_modules\mocha\lib\nodejs\esm-utils.js:100:20)
    at async singleRun (E:\wwwroot\refactoring\node_modules\mocha\lib\cli\run-helpers.js:125:3)
    at async exports.handler (E:\wwwroot\refactoring\node_modules\mocha\lib\cli\run.js:370:5)


13. Reference:https://mochajs.org/#nodejs-native-esm-support. Mocha supports writing tests as ES modules, not just using CommonJS. Edit the test file: test/province.js, modified to import assert fromassert;


import {Province} from '../4/modules/province.js';
import assert from 'assert';

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

describe('province', function () {
    it('shortfall', function () {
        const asia = new Province(sampleProvinceData());
        assert.equal(asia.shortfall, 5);
    });
});


14. Run the test again and the test passes. as shown in Figure 8

再次运行测试,测试通过
8

PS E:\wwwroot\refactoring> npm test

> test
> mocha



  province
    √ shortfall


  1 passing (33ms)


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

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.