2017-10-05 25 views
2

我正在努力获得使用Jasmine测试的一些Typescript类。我创建了一些类,通过模块的声明相互组成:这在茉莉花测试中意味着什么:Uncaught TypeError:对象原型可能只是一个Object或null:undefined?

module xxx.DeviceData { 
    export class ConsoleListener extends DataListener { 
     constructor() { 
      super("ConsoleListener"); 
     } 

     addData(data: string) { 
      console.log(this.title + ": " + data); 
     } 

     clear() { 
      console.clear(); 
     } 
    } 
} 

我撰写的茉莉花测试一起以同样的方式:

module xxx.DeviceData { 
describe('ConsoleListener test', function() { 
    var consoleListener, 
     consoleListenerObj; 

    beforeEach(() => { 
     consoleListener = jasmine.createSpy("consoleListener"); 

     consoleListenerObj = jasmine.createSpyObj('consoleListenerObj', ['onSuccess', 'onFailure', 'addData', 'clear']); 
     consoleListenerObj.onSuccess(); 
     consoleListenerObj.onFailure(); 
     consoleListenerObj.addData(); 
     consoleListenerObj.clear(); 
    }); 

    it('test#1 columnDefinition defined', function() { 
     let consoleListener = new ConsoleListener(); 

     expect(consoleListener).not.toBeNull(); 
    }); 

    it('test#2 call onSuccess', function() { 
     expect(consoleListenerObj.onSuccess).toHaveBeenCalled(); 
    }); 

    it('test#3 call onFailure', function() { 
     expect(consoleListenerObj.onFailure).toHaveBeenCalled(); 
    }); 

    it('test#4 call addData', function() { 
     expect(consoleListenerObj.addData('123')); 
    }); 

    it('test#5 call clear', function() { 
     expect(consoleListenerObj.clear()); 
    }); 
}); 
} 

这一切都完美transpiles。当我尝试执行测试,我收到此错误

遗漏的类型错误:对象的原型可能只是一个对象或null:在脚本/ DeviceData/ConsoleListener.js未定义:5:27

事情错了在传送的JS的第5行,对不对?这里是:

var __extends = (this && this.__extends) || (function() { 
    var extendStatics = Object.setPrototypeOf || 
     ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || 
     function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; 
    return function (d, b) { 
     extendStatics(d, b); 
     function __() { this.constructor = d; } 
     d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 
    }; 
})(); 
var xxx; 
(function (xxx) { 
    var DeviceData; 
    (function (DeviceData) { 
     var ConsoleListener = (function (_super) { 
      __extends(ConsoleListener, _super); 
      function ConsoleListener() { 
       return _super.call(this, "ConsoleListener") || this; 
      } 
      ConsoleListener.prototype.addData = function (data) { 
       console.log(this.title + ": " + data); 
      }; 
      ConsoleListener.prototype.clear = function() { 
       console.clear(); 
      }; 
      return ConsoleListener; 
     }(DeviceData.DataListener)); 
     DeviceData.ConsoleListener = ConsoleListener; 
    })(DeviceData = xxx.DeviceData || (xxx.DeviceData = {})); 
})(xxx|| (xxx= {})); 
//# sourceMappingURL=ConsoleListener.js.map 

当然,第5行似乎是在谈论对象和原型。

我试过不同的方式让模块彼此交谈,但是这个模块方法是我能够始终如一地工作的唯一方法。需要在这里传递的业力/茉莉花背景中是否缺少某些东西?

这里是我的karma.config:

module.exports = function (config) { 
     config.set({ 

      frameworks: ["jasmine","karma-typescript"], 

      preprocessors: { 
       "Scripts/**/*.ts": ["karma-typescript"] 
      }, 

      files: [ 
       'Scripts/DeviceData/*.ts', 
       'Scripts/UnitTests/*.spec.ts' 
      ], 

      exclude: [ 
       'Scripts/**/NodeJSDataSocket.ts' 
      ], 
      reporters: ["progress", "karma-typescript"], 

      //reporters: ["dots", "karma-typescript"], 

      browsers: ["Chrome"], 
      karmaTypescriptConfig: { 
       compilerOptions: { 
        "module": "commonjs", 
        "sourceMap": true, 
        "target": "es5" 
"moduleResolution": "classic", 
"noImplicitAny": false 
       }, 
       tsconfig: "./tsconfig.json", 
      }, 
     }); 
    }; 

回答

2

错误看起来是从extends功能打字稿未来当你从一个超类继承注入。

看代码,我会说你DataListener不提供给你,当你使用它:

extends DataListener 

它可能是不完全丢失,否则编译器会提醒你 - 那么它要么是茉莉花运行时不包括(即加载),或者你的东西装载不规则。

让他们有序,并希望...欢乐!

 files: [ 
      'Scripts/DeviceData/DataListener.ts', 
      'Scripts/DeviceData/ConsoleListener.ts', 
      'Scripts/UnitTests/*.spec.ts' 
     ], 
+0

优秀 - 这在回顾过程中显得很明显。我的第一个想法是,它没有包含在karma.config中,但它应该是 - 我已经在上面发布了。任何其他想法为什么它不包括在内?与此同时,我有一个独立的文件可以分离和测试以确认这一点。 –

+0

是的,这工作,所以它绝对是在那里继承。 –

+0

哪个文件是'DataListener'中的? – Fenton

1

__extends() function由TypeScript编译器生成以处理类继承。 b表示基类,d表示派生类。所以,问题是缺少基类DataListener。检查编译和捆绑脚本的方式。一个命名空间(参见module关键字)可以在多个文件中定义,但是它们的收集/捆绑必须由专人或使用compiler option --outFile来处理。