Karmatesting,PhantomJS错误:无法find模块'事件'

我正在使用TypeScript编写一个Node.js项目。 编译是使用tsc完成的,我没有使用像Gulp或Grunt这样的任务运行。 我会尽可能简洁,但如果需要更多的细节,我会很乐意提供。

我有以下class级:

 // Foo.ts import { EventEmitter } from 'events'; export class Foo extends EventEmitter { //... } 

当我尝试使用茉莉花与Karma和PhantomJStesting,我得到以下错误: Error: Could not find module 'events' from '(...)/foo.js' 。 对我来说,PhantomJS似乎不知道从何处获取events模块。

我的karma.conf.js看起来如下所示:

 var createCoverageReport = false; module.exports = function(config) { config.set({ // base path that will be used to resolve all patterns (eg. files, exclude) basePath: '', frameworks: ['jasmine', 'karma-typescript', 'es6-shim'], files: [ 'src/**/*.ts' ], exclude: [ ], preprocessors: { 'src/**/*.ts': ['karma-typescript'] }, reporters: ['spec'].concat(createCoverageReport ? ['karma-typescript'] : []), port: 9876, colors: true, logLevel: config.LOG_INFO, autoWatch: true, browsers: ['PhantomJS'], singleRun: false, concurrency: Infinity }) } 

我怎么让PhantomJS知道events模块来自Node.js内部模块?

那么根据对SO的类似问题的回答,这是不可能的。

Karma是一个testing运行者, 用于testing前端代码。 这就是它的devise。 因此,如果不使用奇怪的解决方法,则不需要任何节点内部库。

如果您想testing后端代码,请继续使用例如Mocha和Chai,而不是Karma和Jasmine。 搬到这个库不是很痛苦,我完全可以推荐这样做。

此外, 本文提供了有关如何使用Mocha与TypeScript的好介绍,所以我build议先阅读它。