【问题标题】:Angular 2/4 Karma testingAngular 2/4 Karma 测试
【发布时间】:2017-12-01 17:57:38
【问题描述】:

尝试将 typescript 类导入我的测试文件时出现错误。这是我的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']
        }
    });
};

我写了一个简单的测试,它只尝试导入一个类:

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,但我一直收到错误。似乎是什么问题?

【问题讨论】:

  • 我认为对规范文件使用 .ts 扩展名可能会有所帮助。
  • 它是.ts,但它被转译为.js

标签: angular unit-testing typescript testing karma-runner


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 2017-11-08
    • 2015-12-07
    • 1970-01-01
    相关资源
    最近更新 更多