我正在开发基于网络的 3D 游戏。我的项目完全使用 TypeScript 设置。
我使用 Angular 设置了所有路由,同时我使用 Three.js 作为我的 3D 引擎。
最后,我有 Mocha + Chai 组合来使用名为 typescript-require 的特定插件测试我的 TypeScript .
我使用 Systemjs 作为我的 JSPM 包的导入器。
所以要做好准备:
(出于示例目的,文件被精简)
Systemjs/JSPM 文件:jspmconfig.js
System.config({
...
paths: {
...
"github:*": "jspm_packages/github/*"
...
},
map: {
...
"three.js": "github:mrdoob/three.js@master"
...
}
});
节点文件:package.json
...
"scripts": {
"test": "mocha --compilers ts:typescript-require ./source/**/*.spec.ts"
},
...
Controller 文件:QuestDetailCtrl.ts
:
/// <reference path="../../../../../typings/tsd.d.ts" />
// Controller | Dashboard
export default class QuestDetailCtrl {
...
// Constructor
constructor() { ... }
renderer: THREE.WebGLRenderer = new THREE.WebGLRenderer(); // <-- This line uses a THREE instance.
}
注意 在我的应用程序之前的某个地方,我已经导入了 Three.js,因此它在我的 Controller 中可用(这也可以正确编译为 ES5 和在浏览器中工作)。
Mocha 测试文件 QuestDetailCtrl.spec.ts
/// <reference path="../../../../../typings/tsd.d.ts" />
// Imports
import { assert, expect } from 'chai';
import QuestDetailCtrl from './QuestDetailCtrl';
// Describe
describe('QuestDetailCtrl', function() {
// Shoulds
it('should exist', () => {
expect(QuestDetailCtrl).to.exist; // <-- WORKS
});
it('should initialize', () => {
let instance = new QuestDetailCtrl(); // <-- DOES NOT WORK
});
});
在最后一个文件 QuestDetailCtrl.spec.ts
中,我初始化了我的 Controller 的一个新实例(TypeScript 类)。
因为 Mocha 不了解 Three.js,所以这个测试在初始化我的 Controller 实例时失败。
所以我的问题变成了:
我如何在我的测试中模拟/导入 Three.js(或任何其他第三方库),以便 Mocha 知道它并能够测试/初始化它?
我想当我开始测试时,类似的情况也适用于 Angular,因此非常感谢您提供解决方案!
我已经遇到了解决方案,其中一个是从“systemjs”导入系统
并将meta
数据设置为three.js
库以设置global
系统变量,但我无法让它工作。
谢谢!
最佳答案
我现在可以考虑两种可能性。我将用 JS (es6) 编写它,因为我不太熟悉 typescript 和 Jasmine,但你可以很容易地将它转换为 mocha。
set global variable in your test
//QuestDetailCtrl.spec.ts
import THREE from 'three';
window.THREE = THREE;
// or just mock it window.THREE = {}. Its usually not a good thing to
// import whole external library in your tests. If you will mock them,
// don't forget to reset your mock before each test if you need a fresh mock
import three as dependency in your script and mock it in your test
//QuestDetailCtrl.ts
import THREE from 'three';
export default class QuestDetailCtrl {
constructor() { ... }
renderer: THREE.WebGLRenderer = new THREE.WebGLRenderer(); // <-- This line uses a THREE instance.
}
//===================================================================
//QuestDetailCtrl.spec.ts
// be aware, Im using jasmine 2 here. Im passing done value in to function because mocking dependencies are async. Im not sure how this can be done in mocha
beforeAll(done => {
System.delete('three');
System.set('absolute/and/normalized/three/path', System.newModule({
WebGLRenderer: ()=>{},
}));
System.import('absolute/and/normalized/path/QuestDetailCtrl').then(module=>{
ComponentBuilder = module;
done();
});
});
希望对您有帮助。
关于typescript - 如何测试 Mocha & Chai + TypeScript + SystemJS + JSPM + Angular + Three.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32724618/