带有 Typescript 结果的 Karma + Mocha `Can't find variable: require` 或 `Empty test suite`

Karma + Mocha with Typescript results `Can't find variable: require` or `Empty test suite`

我的 Karma 配置:

module.exports = function (config) {
    config.set({
        basePath: '.',
        frameworks: ['mocha', 'chai'],
        files: ['test/**/*-test.js'],
        exclude: [],
        preprocessors: {
            '**/*.ts': ['typescript']
        },
        reporters: ['progress'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['PhantomJS'],
        singleRun: false,
        concurrency: Infinity,
        plugins: [ 'karma-*' ],
        client: {
            mocha: { ui: 'bdd' }
        },
        typescriptPreprocessor: {
            options: {
                sourceMap: false,
                target: 'es5',
                module: 'commonjs',
                removeComments: true
            },
            transformPath: function(path) {
                return path.replace(/\.ts$/, '.js');
            }
        }
    })
};

和测试文件

// test/app-test.ts
import chai = require('chai');

var expect = chai.expect;

describe('test', () => {
    it('should work', () => {
        expect(true).should.be.true;
    });
});

结果是

Empty test suite.

当我将 files: ['test/**/*-test.js'], 更改为 files: ['test/**/*-test.ts'], 时,结果是:

Can't find variable: require

完整来源位于:https://github.com/Ridermansb/webpackKarmaAngularTypescriptStarter

  1. karma.config.js
  2. 中缺少 typings
  3. 我用的是bdd风格,所以测试应该是true.should.be.true.
  4. 不需要在测试中要求 chai。

已在提交中修复 0a4a20c7558579f8c6910a6054e872f87ec97203