

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

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

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

<pre class="wp-block-syntaxhighlighter-code">
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>
</pre>
{
"devDependencies": {
"mocha": "^10.2.0"
},
"scripts": {
"test": "mocha"
},
"type": "module"
}

<pre class="wp-block-syntaxhighlighter-code">
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)
</pre>
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);
});
});

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

PS E:\wwwroot\refactoring> npm test
> test
> mocha
province
√ shortfall
1 passing (33ms)
需要长期技术维护或远程问题排查?
我是拥有 15+ 年经验的 PHP / Go 后端工程师,长期关注已有系统维护、Bug 修复、性能优化、服务器排查、WordPress 网站维护和小功能迭代。
如果你的项目遇到以下情况,可以先从一次小问题排查开始合作:
- ✅ PHP / Laravel / Yii2 老项目无人维护
- ✅ Go / Gin 后端接口需要排查或优化
- ✅ WordPress 网站访问慢、报错或插件冲突
- ✅ Nginx / MySQL / Redis / Linux 服务器异常
- ✅ CDN / Cloudflare / DNS / HTTPS 配置问题
- ✅ 需要长期远程技术支持或兼职维护
更多介绍请查看:关于我 & 合作
微信:13980074657
邮箱:shuijingwanwq@gmail.com
Telegram:@shuijingwan
GitHub:https://github.com/shuijingwan

发表回复