简体   繁体   English

使用打字稿和Angular 2进行的业力测试正在引发“ System.config”的Typeerror

[英]Karma testing with typescript and angular 2 is throwing Typeerror for “System.config”

I'm super new to testing, I'm using angular 2 as a new beginning to learning. 我是测试的新手,我将angular 2用作学习的新起点。

ERRROR: ERRROR:

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

QUESTION: 题:

How can I get remedy the TypeError and uncaught Reference Error, so my main.spec.js can pass? 我该如何补救TypeError和未捕获的Reference Error,以便我的main.spec.js可以通过?


Karma.conf 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
  });
};

tests/main.spec.js: 测试/ main.spec.js:

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 的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>

app.ts app.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);

Your problem is directly linked to this bug 您的问题与该错误直接相关

https://github.com/angular/angular/issues/2049 https://github.com/angular/angular/issues/2049

The problem is that the module loader is being loaded from the wrong URL (hence the 404 error you are seeing) and that makes System not be available. 问题是模块加载器是从错误的URL加载的(因此会出现404错误),从而导致系统不可用。

The Angular2 team uses Protractor with Webdriver. Angular2团队使用带有Webdriver的量角器。 You should be able to get Chrome to work in that configuration. 您应该能够使Chrome在该配置下运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM