我试着根据Testing-with-NPM为我的代码添加三个测试
使用typescript +摩卡。使用以下代码
可以很好地工作const THREE = require('three');
// const ConvexHull = require('three/examples/jsm/math/ConvexHull');
const assert = require('assert');
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);
// let cc = new ConvexHull();
})
})
但是当我取消注释后面的代码并使用ConvexHull
时const ConvexHull = require('three/examples/jsm/math/ConvexHull');
let cc = new ConvexHull();
我得到一个错误错误:无法找到模块"D:xxxnode_modulesthreeexamplesjsmmathConvexHull">
Use typescript load it with import也不起作用
import * as THREE from 'three';
import { ConvexHull } from 'three/examples/jsm/math/ConvexHull';
我不能在node_modulesthreeexamplesjsm下使用任何类。它们都给出一个错误:Cannot find module Error .
想要使用node_modulesthreeexamplesjsm文件夹下提供的类
package.json的一部分
"scripts": {
"test": "mocha -r ts-node/register test/*.ts --reporter list"
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/three": "^0.148.0",
"mocha": "^10.2.0",
"three": "^0.148.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
}
不知道如何解决它。如有任何意见,我将不胜感激。
可能需要导入的文件的扩展名。
由于各种原因,Node可能会在没有它的情况下努力解决路径。
编辑:
我在我的PC上尝试了他们的代码,并添加了一个针对">convexHull的测试用例"对象。我还使用了.mjs扩展名而不是.js以避免修改package.json。所有的测试用例都可以。
代码如下:
JAVASCRIPT单元-测试- 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);
})
})
运行测试:
npm test
我正在添加包。
package.json
{
"scripts": {
"test": "mocha --reporter list"
},
"devDependencies": {
"mocha": "^10.2.0",
"three": "^0.148.0"
}
}
打印稿
"Three"模块包含一个类型声明文件"index. d.s "。然而,它不公开类ConvexHull,因为它不是三个核心代码的一部分。
因此,将示例文件夹复制到根目录中(至少在node_modules之外)。
创建一个tsconfig。在测试目录下的Json,这样它就不会影响你的主tsconfig:
/tsconfig📝测试。json↴
{
"compilerOptions": {
"allowJs": true,
"checkJs": false // <- Optional
}
}
创建.mocharc。让Mocha知道如何运行测试
📝.mocharc。json↴
{
"extensions": ["ts"],
"require": [
"test/register-test.js"
],
"spec": [
"test/**/*.spec.ts"
]
}
创建.mocharc引用的文件:
📝register.js ctrl
require("ts-node").register({
project: "test/tsconfig.json"
});
包。Json比较版本
📝包。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);
})
})
我有一个这样的结构:
📁 root
│
└───📁 examples
│ └───📁 jsm
│ └───📁 math
│ 📝 ConvexHull.js
└───📁 test
│ │─ 📝 .mocharc.json
│ │─ 📝 .register-test.js
│ │─ 📝 .tsconfig.json
│ └───📁 unit
│ 📝 unit-test.ts
│
│─ 📝 package.json
运行测试
npm test
所有的测试用例都将通过。
注意:我可能会更新这篇文章