可能需要您要导入的文件的扩展名.
由于各种原因, node 可能很难在没有它的情况下解决路径.
EDIT:个
我在我的PC上测试了他们的代码,并添加了一个针对"100"对象的测试用例.我还使用了.mjs扩展名而不是.js扩展名,以避免修改Package.json.
所有测试用例都没问题.
以下是代码:
JAVASCRIPT个
unit-test-2.specs.mjs个
import * as THREE from 'three';
import assert from "assert";
import {ConvexHull} from 'three/examples/jsm/math/ConvexHull.js';
describe('The THREE object', function ()
{
it('should have a defined BasicShadowMap constant', function ()
{
assert.notEqual('undefined', THREE.BasicShadowMap);
}),
it('should be able to construct a Vector3 with default of x=0', function ()
{
const vec3 = new THREE.Vector3();
assert.equal(0, vec3.x);
})
it("should have the convexHull tolerance set to -1", function ()
{
let cc = new ConvexHull();
assert.equal(cc.tolerance, -1);
})
})
To run the test:个
npm test
我正在添加Package.json,这样您就可以比较依赖项版本:
package.json个
{
"scripts": {
"test": "mocha --reporter list"
},
"devDependencies": {
"mocha": "^10.2.0",
"three": "^0.148.0"
}
}
TYPESCRIPT个
"Three"模块包含一个类型声明文件"index.d.ts".但是,它不公开类ConvexHull,因为它不是三个核心代码的一部分.
因此,请将示例文件夹复制到您的根目录中(至少在NODE_MODULES之外).
在您的测试目录中创建一个tsconfig.json,这样它就不会影响您的主tsconfig:
???? test/tsconfig.json ↴个
{
"compilerOptions": {
"allowJs": true,
"checkJs": false // <- Optional
}
}
创建.mocharc.json以使Mocha知道如何运行测试
???? .mocharc.json ↴个
{
"extensions": ["ts"],
"require": [
"test/register-test.js"
],
"spec": [
"test/**/*.spec.ts"
]
}
创建.mocharc引用的文件:
???? register.js ↴个
require("ts-node").register({
project: "test/tsconfig.json"
});
用于比较版本的Package.json
???? package.json ↴个
{
"scripts": {
"test": "mocha --config test/.mocharc.json"
},
"devDependencies": {
"@types/mocha": "^7.0.2",
"@types/node": "^18.11.18",
"mocha": "^10.2.0",
"ts-node": "^8.9.0",
"typescript": "^3.8.3"
},
"dependencies": {
"@types/three": "^0.148.0",
"three": "^0.148.0"
},
"type": "commonjs"
}
更新测试:
???? test/unit/unit-test.js ↴个
import { strict as assert } from 'node:assert';
import * as THREE from 'three';
import {ConvexHull} from '../../examples/jsm/math/ConvexHull';
describe('The THREE object', function ()
{
it('should have a defined BasicShadowMap constant', function ()
{
console.log(THREE.BasicShadowMap);
assert.notEqual(undefined, THREE.BasicShadowMap);
})
it('should be able to construct a Vector3 with default of x=0', function ()
{
const vec3 = new THREE.Vector3();
assert.equal(0, vec3.x);
})
it("should have the convexHull tolerance set to -1", function ()
{
let cc = new ConvexHull();
assert.equal(cc.tolerance, -1);
})
})
我有一个这样的 struct :
???? root
│
└───???? examples
│ └───???? jsm
│ └───???? math
│ ???? ConvexHull.js
└───???? test
│ │─ ???? .mocharc.json
│ │─ ???? .register-test.js
│ │─ ???? .tsconfig.json
│ └───???? unit
│ ???? unit-test.ts
│
│─ ???? package.json
运行测试
npm test
所有测试用例都将通过.
注:我可能会更新这篇文章