使用 Karma + ES6 + jspm 导入收益未定义

Import yields undefined with Karma + ES6 + jspm

我正在使用 es6 + jspm + babel + karma 组合。我创建了一个小项目,将所有东西放在一起,配置了 npm、jspm 并启动了 karma,但是由于 import 在我的规范文件中返回 undefined 值而导致错误。

我的配置

src/app.js

'use strict';

export class Greeter {
    get greeting() {
        return 'It works!';
    }
}

test/sample.spec.js

'use strict';

import Greeter from 'src/app.js';

describe('A test suite', function() {

  it('should work', function() {
      let greeter = new Greeter();
      expect(greeter.greeting).toEqual('It works!');
  });
});

karma.conf.js

module.exports = function(config) {
  'use strict';

  config.set({
    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',

    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jspm', 'mocha', 'sinon-chai'],

    jspm: {
      loadFiles: ['test/**/*.js'],
      serveFiles: ['src/**/*.js']
    },

    // list of files to exclude
    exclude: [
    ],

    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },

    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['mocha'],

    // web server port
    port: 9876,

    // enable / disable colors in the output (reporters and logs)
    colors: true,

    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,

    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,

    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['PhantomJS'],

    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false
  });
};

但是我得到了

FAILED TESTS:
  A test suite
    ✖ should work
      PhantomJS 2.1.1 (Mac OS X 0.0.0)
    undefined is not a constructor (evaluating 'new Greeter()')

结构

.
├── config.js
├── gulpfile.js
├── index.html
├── karma.conf.js
├── package.json
├── src
│   ├── app.js
│   └── sass
│       └── main.scss
└── test
    └── sample.spec.js

package.json

{
  "jspm": {
    "devDependencies": {
      "babel": "npm:babel-core@^5.8.24",
      "babel-runtime": "npm:babel-runtime@^5.8.24",
      "core-js": "npm:core-js@^1.1.4"
    }
  },
  "devDependencies": {
    "babel": "^6.5.2",
    "babel-preset-es2015": "^6.6.0",
    "babel-register": "^6.7.2",
    "browser-sync": "^2.11.2",
    "chai": "^3.5.0",
    "gulp": "^3.9.1",
    "gulp-autoprefixer": "^3.1.0",
    "gulp-cached": "^1.1.0",
    "gulp-concat": "^2.6.0",
    "gulp-csso": "^1.1.0",
    "gulp-html-replace": "^1.5.5",
    "gulp-inject": "^4.0.0",
    "gulp-jspm": "^0.5.8",
    "gulp-load-plugins": "^1.2.0",
    "gulp-rename": "^1.2.2",
    "gulp-sass": "^2.2.0",
    "gulp-shell": "^0.5.2",
    "gulp-sourcemaps": "^1.6.0",
    "gulp-uglify": "^1.5.3",
    "gulp-util": "^3.0.7",
    "gulp-watch": "^4.3.5",
    "karma": "^0.13.22",
    "karma-chrome-launcher": "^0.2.3",
    "karma-firefox-launcher": "^0.1.7",
    "karma-jspm": "^2.1.0",
    "karma-mocha": "^0.2.2",
    "karma-mocha-reporter": "^2.0.0",
    "karma-phantomjs-launcher": "^1.0.0",
    "karma-sinon-chai": "^1.2.0",
    "lolex": "^1.4.0",
    "mocha": "^2.4.5",
    "phantomjs-prebuilt": "^2.1.7",
    "require-dir": "^0.3.0",
    "rimraf": "^2.5.2",
    "run-sequence": "^1.1.5",
    "sinon": "^1.17.3",
    "sinon-chai": "^2.8.0"
  }
}

似乎 babel 正确执行了,因为它反映了语法错误。我无法得到任何 export/import 工作。

像这样导出应用程序:

export default class Greeter {

它需要按照您导入它的方式进行默认导出。或者更改导入:

import { Greeter } from 'src/app.js';

即使用命名导出。