Angular 2/4 业力测试

Angular 2/4 Karma testing

尝试将打字稿 class 导入我的测试文件时出现错误。这是我的 karma.conf.js

// Karma configuration
// Generated on Mon Jun 12 2017 14:21:41 GMT+0200 (Central European Daylight Time)

module.exports = function (config) {
    config.set({

        // base path that will be used to resolve all patterns (eg. files, exclude)
        basePath: 'src/app',


        // frameworks to use
        // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
        frameworks: ['jasmine'],


        // list of files / patterns to load in the browser
        files: [
            { pattern: '**/*.spec.ts' }
        ],

        // 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: {
            '**/*.ts': ['typescript']
        },

        // 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,

        // Concurrency level
        // how many browser should be started simultaneous
        concurrency: Infinity,

        mime: {
            'text/x-typescript': ['ts', 'tsx']
        }
    });
};

我写了一个简单的测试,除了尝试导入 class:

import { ApiResponseTimeRepository } from './ApiResponseTimeRepository';


describe('testing test', () => {
    it('should pass', () => {
        expect(1 + 1).toBe(2);
    });
});

但是当我 运行 测试时,我得到了 Uncaught ReferenceError: exports is not defined at TestRepositories.spec.js:2

当我省略导入部分时测试工作正常,所以我认为打字稿转译有问题,所以我安装了 karma-typescript-preprocessor 但我一直收到错误。好像是什么问题?

万一有人遇到同样的问题,我已经通过安装这个软件包解决了这个问题:karma-typescript 并按照说明设置`karma.conf.js