我试着为我的代码添加测试,根据Testing-with-NPM使用3

使用TypeScrip+摩卡.它可以很好地与以下代码配合使用

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:\xxx\node_modules\three\examples\jsm\math\ConvexHull’

使用TypeScrip与导入一起加载也不起作用.

import * as THREE from 'three';
import { ConvexHull } from 'three/examples/jsm/math/ConvexHull';

并且我不能使用node_MODULES\Three\Examples\jsm下的任何类. 它们都给出一个错误:找不到模块错误.

我想使用NODE_MODULES\Three\Examples\jsm文件夹下提供的类

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"
  }

不知道该怎么解决.任何 idea 都将不胜感激,谢谢.

推荐答案

可能需要您要导入的文件的扩展名.

由于各种原因, 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

所有测试用例都将通过.

注:我可能会更新这篇文章

Node.js相关问答推荐

Node.js promise 循环中的所有多个API调用

如何将我的Redis客户端配置为在禁用群集模式的情况下使用读取副本?

如何在Mongoose中调用动态Collection ?

JEST模拟由http服务器控制器导入的ES模块

无法生成仅具有生产依赖项的 node 类型脚本项目

将代码转换为 ES6 Discord.js 的问题

使用`useLocalStorage`和`useDebounce`时如何解决Next.js中的react-hydration-error

更新文档数组中的文档 Mongoose

JAVASCRIPT:foreach 循环后的空数组

即使部署成功,也不会触发 Firebase 函数来填充 Firestore 集合.为什么?

Node.js 大文件上传到 MongoDB 阻塞了事件循环和工作池

如何使动态私有IP地址静态?

使用 $in 查询时,如何获取 mongoDB 中每个唯一 ID 的 n 个文档?

无法更新MongoDB中的文档:";伯森场';writeConcern.w';是错误的类型';数组'&引用;

如何在 mongoDB 中用值 0 填充缺失的文档?

容器之间的 Docker HTTP 请求

如何在 node 中找到引用站点 URL?

User.findOrCreate 函数是做什么的,什么时候在Passport 中调用它?

npm install packagename --save-dev 不更新 package.json

Mongoose - 验证邮箱语法