Skip to content

Instantly share code, notes, and snippets.

@cevek
Last active June 16, 2021 08:42
Show Gist options
  • Save cevek/d64f864ad6677a7f7e46915670a14664 to your computer and use it in GitHub Desktop.
Save cevek/d64f864ad6677a7f7e46915670a14664 to your computer and use it in GitHub Desktop.

create package.js

npm init -y

create tsconfig.json

tsc --init

install deps

npm i karma jasmine karma-webpack ts-loader typescript webpack karma-chrome-launcher karma-jasmine karma-sourcemap-loader

typings

typings install dt~jasmine --global --save

my.conf.js

module.exports = function (config) {
    config.set({
        basePath: '',
        frameworks: ['jasmine'],
        files: ['spec/main.spec.ts'],
        preprocessors: {
            'spec/main.spec.ts': ['webpack', 'sourcemap'],
        },
        webpack: {
            resolve: {
                extensions: ['', '.js', '.ts', '.tsx']
            },
            module: {
                loaders: [
                    {test: /\.tsx?$/, loader: 'ts-loader'}
                ]
            },
            stats: {
                colors: true,
                modules: true,
                reasons: true,
                errorDetails: true
            },
            devtool: 'inline-source-map',
        },
        reporters: ['progress'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['Chrome'],
        singleRun: false,
        concurrency: Infinity
    })
}

spec/main.spec.ts

describe('testgroup', ()=> {
    it('case1', ()=> {
        expect(true).toBe(true);
    })
})

run karma

karma start my.conf.js

to work source maps correctly you need patch node_modules/karma-webpack/index.js

// replace this
webpackOptions.output.filename = "[name]";
// by that
webpackOptions.output.filename = "[name].js";

// find this line
Plugin.prototype.readFile = function(file, callback) {
    // insert this lines
	var origFile = file;
	file += '.js';
	
// Replace 
this.waiting.push(process.nextTick.bind(process, this.readFile.bind(this, file, callback)));
// to that
this.waiting.push(process.nextTick.bind(process, this.readFile.bind(this, origFile, callback)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment