2016-03-27 89 views
0

我使用es6 + jspm + babel + karma组合。我创建了一个小项目,把所有东西放在一起,配置了npm,jspm并启动了业力,但是得到了一个错误,import返回undefined值在我的spec文件中。导入收益未定义Karma + ES6 + jspm

我的配置

的src/app.js

'use strict'; 

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

测试/ 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:[email protected]^5.8.24", 
     "babel-runtime": "npm:[email protected]^5.8.24", 
     "core-js": "npm:[email protected]^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得到正确执行,因为它反映的语法错误。我无法获得任何导出/导入工作。

+0

'从'src/app.js'导入{Greeter};'试试这个 –

回答

2

导出应用程序是这样的:

export default class Greeter { 

它需要一个默认的导出导入的方式。或改变进口:

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

即使使用命名的导出。

+0

谢谢,就是这样。我怎么能错过这个。 –