Angular 2 Karma测试(PhantomJS) - SyntaxError:使用保留字' import'

时间:2016-11-30 11:23:28

标签: angular phantomjs karma-runner

由于jenkins的目的,我试图使用PhantomJS设置Angular 2 Karma测试。 虚拟测试工作正常,当我尝试从angular本身导入服务时出现问题。

问题

正在运行npm test

PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
  SyntaxError: Use of reserved word 'import'
  at test/components/talks.spec.ts:1
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 0 of 0 ERROR (0.114 secs / 0 secs)
npm ERR! Test failed.  See above for more details.

原因

档案talks.spec.ts

import {TalkService} from "../../app/services/talk.service"; <- Error here

    describe('Integration testing for talks', function() {
        let service : TalkService = new TalkService();

    it('should be able to run a method', function() {
        service.something();
    });
});

出于某种原因,我在使用PhantomJS时无法导入。我已经尝试了大量的解决方案,在我的项目充满依赖性试图寻找解决方案之后,我知道我无法解决这个问题。

文件

karma.conf.js

module.exports = function(config) {
    config.set({
        basePath: '.',
        frameworks: ['jasmine'],
        plugins : [
            'karma-jasmine',
            'karma-html-reporter',
            'karma-phantomjs-launcher',
            'karma-junit-reporter'
        ],
        files: [
            './test/components/dashboard.spec.js',
            './test/components/talks.spec.ts'
        ],
        exclude: [],
        preprocessors: {},
        reporters: ['progress', 'junit', 'html'],
        htmlReporter: {
            outputDir: 'test', // where to put the reports
            templatePath: null, // set if you moved jasmine_template.html
            focusOnFailures: true, // reports show failures on start
            namedFiles: false, // name files instead of creating sub-directories
            pageTitle: null, // page title for reports; browser info by default
            urlFriendlyName: false, // simply replaces spaces with _ for files/dirs
            reportName: 'html', // report summary filename; browser info by default

            // experimental
            preserveDescribeNesting: false, // folded suites stay folded
            foldAll: false // reports start folded (only with preserveDescribeNesting)
        },
        junitReporter: {
            outputDir: 'test/xml-reports/'
        },
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: false,
        browsers: ['PhantomJS'],
        singleRun: true,
        concurrency: Infinity
    })
};

果报测试shim.js

// Tun on full stack traces in errors to help debugging
Error.stackTraceLimit = Infinity;

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;

// // Cancel Karma's synchronous start,
// // we will call `__karma__.start()` later, once all the specs are loaded.
__karma__.loaded = function() {};

System.config({
    packages: {
        'base/dist': {
            defaultExtension: false,
            format: 'cjs',
            map: Object.keys(window.__karma__.files).filter(onlyAppFiles).reduce(createPathRecords, {})
        }
    }
});

System.import('@angular/core/testing')
    .then(function(browser_adapter) { browser_adapter.BrowserDomAdapter.makeCurrent(); })
    .then(function() { return Promise.all(resolveTestFiles()); })
    .then(function() { __karma__.start(); }, function(error) { __karma__.error(error.stack || error); });

function createPathRecords(pathsMapping, appPath) {
    // creates local module name mapping to global path with karma's fingerprint in path, e.g.:
    // './vg-player/vg-player':
    // '/base/dist/vg-player/vg-player.js?f4523daf879cfb7310ef6242682ccf10b2041b3e'
    var pathParts = appPath.split('/');
    var moduleName = './' + pathParts.slice(Math.max(pathParts.length - 2, 1)).join('/');
    moduleName = moduleName.replace(/\.js$/, '');
    pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath];
    return pathsMapping;
}

function onlyAppFiles(filePath) {
    return /\/base\/dist\/(?!.*\.spec\.js$).*\.js$/.test(filePath);
}

function onlySpecFiles(path) {
    return /\.spec\.js$/.test(path);
}

function resolveTestFiles() {
    return Object.keys(window.__karma__.files)  // All files served by Karma.
        .filter(onlySpecFiles)
        .map(function(moduleName) {
            // loads all spec files via their global module names (e.g.
            // 'base/dist/vg-player/vg-player.spec')
            return System.import(moduleName);
        });
}

0 个答案:

没有答案