Search code examples
typescriptkarma-runnerkarma-mochatypescript1.6

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


My Karma config:

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');
            }
        }
    })
};

and test file

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

var expect = chai.expect;

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

The result is

Empty test suite.

When I change the files: ['test/**/*-test.js'], to files: ['test/**/*-test.ts'],, the result is:

Can't find variable: require

The full source is in: https://github.com/Ridermansb/webpackKarmaAngularTypescriptStarter


Solution

    1. Missing typings in karma.config.js
    2. I use bdd style, so the test should be true.should.be.true.
    3. No need to require chai in tests.

    Fixed in commit 0a4a20c7558579f8c6910a6054e872f87ec97203