在 重构:改善既有代码的设计 (第2版) 中,基于 Mocha 运行测试用例
1、在 Githab 中搜索:Mocha,选择第一个:mochajs/mocha,用于 node.js 和浏览器的简单、灵活、有趣的 javascript 测试框架。如图1
2、点击链接 Documentation ,进入 https://mochajs.org/
3、参考安装:https://mochajs.org/#installation ,作为项目的开发依赖项,使用 npm 安装,进入项目目录:npm install –save-dev mocha。如图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、参考:https://mochajs.org/#getting-started。运行:./node_modules/mocha/bin/mocha,报错:Windows Script Host,脚本:E:\wwwroot\refactoring\node_modules\mocha\bin\mocha.js,错误:无效字符。如图3
5、在 package.json 中设置测试脚本:
{ "devDependencies": { "mocha": "^10.2.0" }, "scripts": { "test": "mocha" } }
6、然后运行测试:npm test,运行正常。如图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、删除文件:test/test.js,新建测试文件: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、运行测试报错:(node:5740) Warning: To load an ES module, set “type”: “module” in the package.json or use the .mjs extension.。如图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.<anonymous> (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、编辑 package.json,添加 “type”: “module”
{ "devDependencies": { "mocha": "^10.2.0" }, "scripts": { "test": "mocha" }, "type": "module" }
10、再次运行测试,报错:ReferenceError: assert is not defined。如图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.<anonymous> (file:///E:/wwwroot/refactoring/test/province.js:19:9) at process.processImmediate (node:internal/timers:471:21)
11、编辑测试文件:test/province.js,添加 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、再次运行测试,报错:ReferenceError: require 未在 ES 模块范围内定义,你可以改用导入。如图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、参考:https://mochajs.org/#nodejs-native-esm-support 。Mocha 支持将测试编写为 ES 模块,而不仅仅是使用 CommonJS。编辑测试文件:test/province.js,修改为 import assert from ‘assert’;
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、再次运行测试,测试通过。如图8
PS E:\wwwroot\refactoring> npm test > test > mocha province √ shortfall 1 passing (33ms)
近期评论