7
document
オブジェクトを何とか模擬して、従来のTypeScriptクラスを単体テストできるようにする必要があります。クラスは、第三者モジュールのimport
を持つ別のクラス(View.ts
)をインポートし、次に、document
が存在すると仮定して別のクラスをインポートします。ユニットテスト:インポートされたTypeScript依存関係によって使用されるモックドキュメントですか?
輸入のマイチェーン:
proxyquireとモックView
をしようとしました
ReferenceError: document is not defined
を、次のように:
beforeEach(() => {
viewStub = {};
let view:any = proxyquire('../../src/View', { 'View': viewStub });
viewStub.constructor = function():void { console.log('hello!'); };
});
私の私が取得しています
controller.ts -> view.ts -> dragula -> crossvent -> custom-event
// Controller.ts:
import { View } from './View';
// View.ts:
const dragula = require('dragula');
// dragula requires crossvent, which requires custom-event, which does this:
module.exports = useNative() ? NativeCustomEvent :
'function' === typeof document.createEvent ? function CustomEvent (type, params) {
var e = document.createEvent('CustomEvent');
// ...
} : function CustomEvent (type, params) {
// ...
}
// controller.spec.ts:
import { Controller } from '../../src/Controller';
エラー問題はimport
ステートメントのために、View
が初期化される前にそのエラーがポップアップします。