Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用typescript和angular 2进行业力测试时,会抛出“Typeerror”;System.config";_Javascript_Unit Testing_Typescript_Karma Jasmine_Angular - Fatal编程技术网

Javascript 使用typescript和angular 2进行业力测试时,会抛出“Typeerror”;System.config";

Javascript 使用typescript和angular 2进行业力测试时,会抛出“Typeerror”;System.config";,javascript,unit-testing,typescript,karma-jasmine,angular,Javascript,Unit Testing,Typescript,Karma Jasmine,Angular,我对测试非常陌生,我正在使用angular 2作为学习的新开始 错误: INFO [Chrome 42.0.2311 (Mac OS X 10.9.3)]: Connected on socket xo5ufjmFKLGc5QSuAAAB with id 34336816 WARN [web-server]: 404: /base/jspm_packages/es6-module-loader.js ERROR [karma]: Uncaught TypeError: System.config

我对测试非常陌生,我正在使用angular 2作为学习的新开始

错误:

INFO [Chrome 42.0.2311 (Mac OS X 10.9.3)]: Connected on socket xo5ufjmFKLGc5QSuAAAB with id 34336816
WARN [web-server]: 404: /base/jspm_packages/es6-module-loader.js
ERROR [karma]: Uncaught TypeError: System.config is not a function
at http://localhost:9876/base/js/angular.js?4c894ae47e8d04bb01965dbf22fa08aed20f0eb2:25575

ERROR [karma]: Uncaught ReferenceError: require is not defined
at http://localhost:9876/base/app/app.js?8a00e7f6717b2dd7118e800d05625a443a4b2065:13
import Foo from '../app/app.js';

describe('ES6 Foo', function () {

  let foo;

  beforeEach(()=>{
      foo = new Foo();
  });

    it('should call foo.thing and test default value', function(){
        expect(foo.thing).toEqual(0);
    });
});
问题: 我怎样才能得到纠正TypeError和uncaught引用错误的方法,这样我的main.spec.js就可以通过了


Karma.conf

// Karma configuration
// Generated on Mon May 18 2015 18:17:07 GMT-0700 (PDT)

module.exports = function(config) {
  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: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
     'app/app.js',
     'tests/*.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: ['progress'],


    // 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: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false
  });
};
测试/main.spec.js:

INFO [Chrome 42.0.2311 (Mac OS X 10.9.3)]: Connected on socket xo5ufjmFKLGc5QSuAAAB with id 34336816
WARN [web-server]: 404: /base/jspm_packages/es6-module-loader.js
ERROR [karma]: Uncaught TypeError: System.config is not a function
at http://localhost:9876/base/js/angular.js?4c894ae47e8d04bb01965dbf22fa08aed20f0eb2:25575

ERROR [karma]: Uncaught ReferenceError: require is not defined
at http://localhost:9876/base/app/app.js?8a00e7f6717b2dd7118e800d05625a443a4b2065:13
import Foo from '../app/app.js';

describe('ES6 Foo', function () {

  let foo;

  beforeEach(()=>{
      foo = new Foo();
  });

    it('should call foo.thing and test default value', function(){
        expect(foo.thing).toEqual(0);
    });
});
index.html

<html>
    <head>
        <script src="./js/traceur.js"></script>
        <script src="./jspm_packages/system.js"></script>
        <script src="./js/angular2.js"></script>
    </head>
    <body>
        <main-app></main-app>
        <script>
            System.import('app/app');
        </script>
    </body>
</html>

系统导入(“应用程序/应用程序”);
应用程序ts

/// <reference path="../typings/angular2/angular2"/>
import {bootstrap, Component, View} from 'angular2/angular2'

@Component({
    selector:'main-app',
    injectables: [

    ]
})

@View({
  templateUrl: 'app/main.html',
})

class MainApp {
    thing;
    constructor(){
        this.thing = 0
    }

    add(){
        this.thing++;
    }
    subtract(){
        this.thing--;
    }
}

bootstrap(MainApp);
//
从'angular2/angular2'导入{bootstrap,Component,View}
@组成部分({
选择器:'main-app',
注射剂:[
]
})
@看法({
templateUrl:'app/main.html',
})
类MainApp{
事情
构造函数(){
这个东西=0
}
添加(){
这个;
}
减去(){
这件事;
}
}
引导(MainApp);

您的问题与此错误直接相关

问题是模块加载器是从错误的URL加载的(因此您看到404错误),这使得系统不可用


Angular2团队使用带有Webdriver的量角器。您应该能够让Chrome在该配置中工作。

为什么您要将.js文件提供给karma.conf而不是.ts?