Testing with NPM three mocha+typescript.

huangapple go评论69阅读模式
英文:

Testing with NPM three mocha+typescript

问题

我尝试为我的代码添加测试,根据Testing-with-NPM的指南。

使用TypeScript + Mocha。以下代码运行良好。

const THREE = require('three');
// const ConvexHull = require('three/examples/jsm/math/ConvexHull');

const assert = require('assert');

describe('THREE对象', function() {
  it('应该有一个定义的BasicShadowMap常量', function() {
    assert.notEqual('undefined', THREE.BasicShadowMap);
  }),

  it('应该能够构建一个具有默认x=0的Vector3', 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();

我得到了一个错误:Error: 无法找到模块'D:\xxx\node_modules\three\examples\jsm\math\ConvexHull'。

使用TypeScript导入它也不起作用。

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

我不能使用node_modules\three\examples\jsm文件夹下的任何类。它们都会报错:Error: 无法找到模块。

想要使用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"
  }

不知道如何解决它。任何想法将不胜感激,谢谢。

英文:

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.

答案1

得分: 1

以下是翻译好的部分:

"The extension for the file you want to import may be needed." -> "您要导入的文件可能需要文件扩展名。"

"Node likely struggles to solve the path without it for various reasons." -> "出于各种原因,Node 可能无法在没有文件扩展名的情况下解析路径。"

"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." -> "我在我的电脑上尝试了他们的代码,并添加了一个针对“convexHull”对象的测试用例。我还使用了**.mjs**扩展名,而不是*.js*,以避免修改package.json文件。"

"All test cases are okay." -> "所有测试用例都通过。"

"To run the test:" -> "运行测试:"

"I am adding the package.json, so you can compare the dependency versions:" -> "我正在添加package.json,这样您可以比较依赖版本:"

"Update the tests:" -> "更新测试:"

"I have a structure like that:" -> "我的项目结构如下:"

"Run the test" -> "运行测试"

"All test cases will pass." -> "所有测试用例都会通过。"

"Note: I might update this post" -> "注意:我可能会更新此帖子。"

英文:

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:

📝 test/tsconfig.json ↴

{
    "compilerOptions": {
        "allowJs": true,
        "checkJs": false  // <- Optional
    }
}

Create .mocharc.json to make Mocha aware of how to run the tests

📝 .mocharc.json ↴

{
    "extensions": ["ts"],
    "require": [
        "test/register-test.js"
    ],
    "spec": [
        "test/**/*.spec.ts"
    ]
}

Create the file the .mocharc is referring to:

📝 register.js ↴

require("ts-node").register({
    project: "test/tsconfig.json"
});

package.json to compare versions

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

Update the tests:

📝 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);
    })
})

I have a structure like that:

📁 root
│
└───📁 examples
│         └───📁 jsm	
│                    └───📁 math
│                               📝 ConvexHull.js
└───📁 test
│          │─ 📝 .mocharc.json
│          │─ 📝 .register-test.js
│          │─ 📝 .tsconfig.json
│          └───📁 unit
│                  📝 unit-test.ts
│
│─ 📝 package.json

Run the test

npm test

All test cases will pass.

Note: I might update this post

huangapple
  • 本文由 发表于 2023年1月6日 16:15:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75028473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定