如何使用 mocha 和 node 解决 Typescript 外部依赖
How to resolve Typescript external dependencies with mocha and node
我有以下文件;
MyClass.ts
/// <reference path="node_modules/phaser/typescript/phaser.d.ts" />
export class MyClass {
d: Phaser.Sprite;
constructor() {
this.d = new Phaser.Sprite(new Phaser.Game, 10, 10);
}
win() : boolean {
return true;
}
}
test.ts
/// <reference path="../typings/mocha/mocha.d.ts" />
import MyModule = require('../MyClass');
describe('MyClass', () => {
var subject : MyModule.MyClass;
beforeEach(function () {
subject = new MyModule.MyClass();
});
describe('#win', () => {
it('should pass', () => {
var result : boolean = subject.win();
if (result !== true) {
throw new Error('Expected true but was ' + result);
}
});
});
});
我已经使用 tsd to pull in mocha.d.ts
and I'm using ts-node 在 node 中执行 typescript,所以我按如下方式执行 mocha;
mocha --compilers ts:ts-node/register
编译成功,但是运行时测试失败,因为未定义 Phaser;
MyClass
#win
1) "before each" hook for "should pass"
0 passing (47ms)
1 failing
1) MyClass "before each" hook for "should pass":
ReferenceError: Phaser is not defined
at new MyClass (c:\Users\stafford\Documents\git\ts-node-test\MyClass.ts:5:22)
at Context.<anonymous> (c:\Users\stafford\Documents\git\ts-node-test\test\test.ts:8:19)
at callFn (C:\Users\stafford\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:286:21)
at Hook.Runnable.run (C:\Users\stafford\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:279:7)
at next (C:\Users\stafford\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:297:10)
at Immediate._onImmediate (C:\Users\stafford\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:319:5)
我想我将不得不做这样或类似的事情;
import Phaser = require('phaser');
但这会中断编译并出现 phaser.d.ts;
的错误
phaser.d.ts is not a module.
如何通过命令行执行这样的测试?
import Phaser = require('phaser');
您应该使用以下
创建一个vendor.d.ts
declare module 'phaser' {
export = Phaser;
}
这将为您创建一个类型安全的模块phaser
。不要忘记还包括 phaser.d.ts :https://github.com/photonstorm/phaser/blob/v2.4.4/typescript/phaser.d.ts
更多
https://basarat.gitbooks.io/typescript/content/docs/project/modules.html
我有以下文件;
MyClass.ts
/// <reference path="node_modules/phaser/typescript/phaser.d.ts" />
export class MyClass {
d: Phaser.Sprite;
constructor() {
this.d = new Phaser.Sprite(new Phaser.Game, 10, 10);
}
win() : boolean {
return true;
}
}
test.ts
/// <reference path="../typings/mocha/mocha.d.ts" />
import MyModule = require('../MyClass');
describe('MyClass', () => {
var subject : MyModule.MyClass;
beforeEach(function () {
subject = new MyModule.MyClass();
});
describe('#win', () => {
it('should pass', () => {
var result : boolean = subject.win();
if (result !== true) {
throw new Error('Expected true but was ' + result);
}
});
});
});
我已经使用 tsd to pull in mocha.d.ts
and I'm using ts-node 在 node 中执行 typescript,所以我按如下方式执行 mocha;
mocha --compilers ts:ts-node/register
编译成功,但是运行时测试失败,因为未定义 Phaser;
MyClass
#win
1) "before each" hook for "should pass"
0 passing (47ms)
1 failing
1) MyClass "before each" hook for "should pass":
ReferenceError: Phaser is not defined
at new MyClass (c:\Users\stafford\Documents\git\ts-node-test\MyClass.ts:5:22)
at Context.<anonymous> (c:\Users\stafford\Documents\git\ts-node-test\test\test.ts:8:19)
at callFn (C:\Users\stafford\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:286:21)
at Hook.Runnable.run (C:\Users\stafford\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:279:7)
at next (C:\Users\stafford\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:297:10)
at Immediate._onImmediate (C:\Users\stafford\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:319:5)
我想我将不得不做这样或类似的事情;
import Phaser = require('phaser');
但这会中断编译并出现 phaser.d.ts;
的错误phaser.d.ts is not a module.
如何通过命令行执行这样的测试?
import Phaser = require('phaser');
您应该使用以下
创建一个vendor.d.ts
declare module 'phaser' {
export = Phaser;
}
这将为您创建一个类型安全的模块phaser
。不要忘记还包括 phaser.d.ts :https://github.com/photonstorm/phaser/blob/v2.4.4/typescript/phaser.d.ts
更多
https://basarat.gitbooks.io/typescript/content/docs/project/modules.html