I try to add test for my code use three according to Testing-with-NPM
Use typescript mocha. it works great with following code
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();
})
})
but when i uncomment following code and use ConvexHull
const ConvexHull = require('three/examples/jsm/math/ConvexHull');
let cc = new ConvexHull();
I got a error Error: Cannot find module 'D:\xxx\node_modules\three\examples\jsm\math\ConvexHull'
Use typescript load it with import does not work either.
import * as THREE from 'three';
import { ConvexHull } from 'three/examples/jsm/math/ConvexHull';
And I can't use any class under node_modules\three\examples\jsm. All of them give a Error: Cannot find module error.
Want to use classes provided under node_modules\three\examples\jsm folder
part of 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"
}
Don't know how to solve it. any ideas would be appreciated, thanks.
CodePudding user response:
The extension for the file you want to import may be needed.
Node likely struggles to solve the path without it for various reasons.
EDIT:
I tried their code on my PC and added a test case against a "convexHull" object. I have also used the .mjs extension instead of .js to avoid modifying the package.json. All test cases are okay.
Here is the code:
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
I am adding the package.json, so you can compare the dependency versions:
package.json
{
"scripts": {
"test": "mocha --reporter list"
},
"devDependencies": {
"mocha": "^10.2.0",
"three": "^0.148.0"
}
}
TYPESCRIPT
The "Three" module contains a type declaration file "index.d.ts". However, it does not expose the class ConvexHull as it's not part of Three core codes.
Because of this, copy the example folder in your root directory (outside the node_modules, at least).
Create a tsconfig.json within your test directory so it does not affect your main tsconfig: