SystemJS + Karma + TypeScript - 未运行测试

时间:2016-09-05 18:50:37

标签: javascript typescript phantomjs karma-runner systemjs

我现在尝试运行一些单元测试几天但没有成功。我的应用程序是用typescript编写的(单元测试也是用typescript编写的)。 这是我的业力配置文件:

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

    basePath: "../../../../",

    frameworks: [
      "jasmine",
      "systemjs"
    ],

    browsers: ['PhantomJS'],

    singleRun: true,
    reporters: ['progress'],

    systemjs: {
      config: {
        paths: {
          systemjs: "src/main/webapp/static/js/engage/lib/bower/system.js/dist/system.src.js",
          typescript: "build/node_modules/typescript/lib/typescript.js"
        },
        transpiler: "typescript"
      },

      // testFileSuffix: ".spec.ts"
    },

    files: [
      'src/main/webapp/static/js/engage/dist/vendor/vendor.min.js',
      'src/main/webapp/static/js/engage/lib/bower/system.js/dist/system-polyfills.src.js',
      'src/main/webapp/static/js/engage/lib/bower/angular-mocks/angular-mocks.js',
      {pattern: "src/main/webapp/static/js/engage/app/**/*.spec.ts", served: true, included: false}
    ]
  });
};

当我运行karma start时,我收到以下错误:

05 09 2016 21:39:45.543:INFO [karma]: Karma v1.2.0 server started at http://localhost:9876/
05 09 2016 21:39:45.546:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
05 09 2016 21:39:45.558:INFO [launcher]: Starting browser PhantomJS
05 09 2016 21:39:47.631:INFO [PhantomJS 1.9.8 (Mac OS X 0.0.0)]: Connected on socket /#c-ZoxCczyYymPDAtAAAA with id 8911412
05 09 2016 21:39:47.650:WARN [web-server]: 404: /base/src/main/webapp/static/js/engage/lib/bower/system.js/dist/system-polyfills.js
PhantomJS 1.9.8 (Mac OS X 0.0.0) ERROR
  You need to include some adapter that implements __karma__.start method!

如果我将浏览器属性更改为Chrome,则会收到以下错误:

Chrome 52.0.2743 (Mac OS X 10.11.6) ERROR
  Error: ReferenceError: jasmineRequire is not defined
            at eval (build/node_modules/karma-jasmine/lib/boot.js:14:32)
            at eval (build/node_modules/karma-jasmine/lib/boot.js:41:2)
        Evaluating build/node_modules/karma-jasmine/lib/boot.js
        Error loading build/node_modules/karma-jasmine/lib/boot.js

这是我的package.json相关依赖项:

"phantomjs": "^1.9.11",
"karma-systemjs": "^0.14.0",
"karma-jasmine": "^1.0.2",
"karma-chrome-launcher": "^0.1.5",
"jasmine-core": "^2.5.0"

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:3)

  1. 我会尝试将systemjs作为第一个框架包含在文档
  2. 在使用systemjs加载打字稿之前编译你的打字稿,不要使用systemjs typescript compile。这将降低你的业力测试的复杂性,使他们更加坚实
  3. 尝试将spec包含更改为true,这有助于解决一些问题。但我不确定
  4. 将你的basePath留在./,因为这只会造成麻烦
  5. 检查system.js的路径,因为它会抛出404错误
  6. 我对system.js的配置如下所示

    systemjs: {
        configFile: 'dev/systemjs.config.js',
        config: {
            map: {
                'systemjs':          'node_modules/systemjs/dist/system.js',
                'system-polyfills':  'node_modules/systemjs/dist/system-polyfills.js',
                'es6-module-loader': 'node_modules/es6-module-loader/dist/es6-module-loader.js'
            }
        }
    }
    

    也许这些问题的一些配置片段可能会有所帮助: Angular 2 + Karma + karma-jspm + karma-coverage + typescript report phantomJS failure

答案 1 :(得分:1)

尝试将插件选项添加到karma配置

plugins: [
  'karma-jasmine',
  'karma-chrome-launcher'
],

使用最后一个node.js 6.5并将依赖项更新为最新版本