I am trying to write a unit test for my three js utility functions. My Project uses CRA w TS, and mocha & chai w TS for unit testing.
GeometryUtils.spec.ts
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { Scene, Vector3 } from 'three';
import { extractBoundaryOf } from './GeometryUtils';
import fs from 'fs';
import * as THREE from 'three';
import { GLTF, GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
describe('GeometryUtils.extractBoundaryOf', function () {
it('should return a Vector3[][]', function () {
THREE.Cache.enabled = true;
const surfaceName = 'wall_0';
const surfaceNormal = new Vector3(0, 0, 1);
new GLTFLoader().parse(
fs.readFileSync('./src/services/threejs/test/room_camera_geometry.gltf').toString(),
'./src/services/threejs/test/',
function (gltf) {
const scene = gltf.scene as unknown as Scene;
console.log('GLTF ', gltf);
console.log('Scene ', gltf.scenes);
const boundary = extractBoundaryOf(surfaceName, surfaceNormal, scene);
expect(boundary).to.be.an('array');
},
function (error) {
console.error(error);
},
);
});
});
GeometryUtil.ts
import {
Box2,
BufferGeometry,
Group,
LineSegments,
Mesh,
Object3D,
PerspectiveCamera,
Raycaster,
LineBasicMaterial,
Scene,
Triangle,
Vector2,
Vector3,
Line,
} from 'three';
export function extractBoundaryOf(surfaceName: string, surfaceNormal: Vector3, inScene: Scene): Vector3[][] {
// just an example
let allVertices: Vector3[][] = [[new Vector3(0, 0, 1)]];
return allVertices;
}
Error I'm facing:
yarn run v1.22.17
$ mocha src/**/*.spec.ts
(node:34713) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
GeometryUtils.extractBoundaryOf
✔ should return a Vector3[][]
ReferenceError: Request is not defined
at FileLoader.load (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/build/three.module.js:39258:15)
at file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2640:11
at new Promise (<anonymous>)
at GLTFParser.loadBuffer (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2638:10)
at GLTFParser.getDependency (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2541:24)
at GLTFParser.loadBufferView (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2659:15)
at file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2535:40
at GLTFParser._invokeOne (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2468:19)
at GLTFParser.getDependency (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2533:24)
at GLTFParser.loadAccessor (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2694:34)
at GLTFParser.getDependency (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2529:24)
at assignAttributeAccessor (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:4206:17)
at addPrimitiveAttributes (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:4222:17)
at GLTFParser.loadGeometries (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:3334:24)
at GLTFParser.loadMesh (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:3377:24)
at file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2523:34
at GLTFParser._invokeOne (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2468:19)
at GLTFParser.getDependency (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2521:24)
at GLTFParser.createNodeMesh (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:3739:17)
at file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:3788:38
at GLTFParser._invokeOne (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2468:19)
at file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:3786:31
at GLTFParser.loadNode (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:3820:4)
at GLTFParser.getDependency (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2517:24)
at buildNodeHierarchy (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:3985:16)
at GLTFParser.loadScene (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:3933:18)
at GLTFParser.getDependency (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2513:24)
at file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2601:19
at Array.map (<anonymous>)
at GLTFParser.getDependencies (file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2599:37)
at file:///home/subhankar/workspace/trisetra/trisetra-decor/node_modules/three/examples/jsm/loaders/GLTFLoader.js:2314:12
at processTicksAndRejections (internal/process/task_queues.js:95:5)
1 passing (64ms)
Done in 14.62s.
package.json:
{
"type": "module",
"scripts": {
....
"test": "mocha src/**/*.spec.ts",
....,
}
}
tsconfig.json
{
"compilerOptions": {
"types": [ "node", "mocha", "chai", "lodash"],
"typeRoots": ["typings", "./node_modules/@types"],
"target": "esnext",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"useDefineForClassFields": true
},
"include": [
"src"
]
}
.mocharc.json
{
"node-option": [
"experimental-specifier-resolution=node",
"loader=ts-node/esm"
]
}
I wanna pass the GLTF object to my testing This is my directory structure:
I tried to compile using commonjs too. But that didn't work either.