2016-01-17 88 views
0

我有以下文件;如何解決與摩卡和節點的Typescript外部依賴關係

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

我用tsdmocha.d.ts拉和我使用ts-node以執行節點打字稿所以我如下執行摩卡;

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不是模塊。

我怎樣才能得到這樣的測試,通過命令行執行?

repro

+0

我不知道這應該被標記爲移相器。如果你真的認爲它應該是,那麼正確的標籤就是相位框架。 –

+0

@JamesSkemp歡呼,更新。 –

回答