2016-03-27 3 views
0

Я использую es6 + jspm + babel + сочетание кармы. Я создал небольшой проект, собрал все вместе, настроил npm, jspm и начал карму, но получил ошибку, вызванную import, возвращающую значение undefined в моем файле spec.Импортируемые уроки неопределенные с кармой + 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

'import {Greeter} из 'src/app.js';' попробуйте это –

ответ

2

Экспорт приложение, как это:

export default class Greeter { 

Это необходимо по умолчанию экспортировать как вы импортировать его. Или изменить импорт:

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

I.e. используйте указанный экспорт.

+0

Спасибо, вот и все. Как я мог пропустить это. –