Izhaki
Izhaki

Reputation: 23586

Typescript tests, with Karma, Jasmine and Grunt

I've been trying to use Karma+Jasmine to run tests written in TypeScript.

Searching online, it seems quite a few have managed such feat, but I could not find a complete guide as to how to do this. The environment:

How does one set up the whole thing?

Upvotes: 2

Views: 1528

Answers (2)

Josh Leeb-du Toit
Josh Leeb-du Toit

Reputation: 689

Take a look at using the karma-typescript-preprocessor which might be a little easier than watching, compiling and then running the tests.

This can be setup pretty easily to your karma and/or grunt configs.

Upvotes: 1

Izhaki
Izhaki

Reputation: 23586

See this GitHub repository for full source.

package.json

Let's start with devDependencies:

"devDependencies": {
    "grunt": "^0.4.5",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-karma": "^0.12.1",
    "grunt-ts": "^5.1.0",
    "jasmine-core": "^2.3.4",
    "karma": "^0.13.15",
    "karma-jasmine": "^0.3.6",
    "karma-phantomjs-launcher": "^0.2.1",
    "karma-requirejs": "^0.2.3",
    "load-grunt-tasks": "^3.3.0",
    "phantomjs": "^1.9.18",
    "requirejs": "^2.1.22"
}

Gruntfile.js

Watch

We watch all .ts files, and upon change first we transpile them, then run the tests:

watch: {
    typescript: {
        // Watching all typescript files (specs and units)
        files: [ 'source/**/*.ts' ],
        // First transpile, then run tests
        tasks: [ 'ts:default', 'karma:continuous:run' ],
        options: {
            spawn: false
        }
    }
},

Typescript compilation

We transpile all .ts files using the amd module. Note that we need the typings of jasmine:

ts: {
    default: {
        src: [
            // Note that we add the typings for jasmine here
            'typings/jasmine.d.ts',
            // We compile all .ts files (specs and units)
            'source/**/*.ts'
        ],
        options: {
            // We need to convert ES6 to ES5 and use amd so it works
            // with RequireJS
            module: 'amd',
            fast: 'never'
        },
        // We output all files to a temp folder
        outDir: 'transpiled'
    }
},

Karma

One all the .ts files are in the transpiled folder, we can run the karma tests:

karma: {
    options: {
        configFile: 'karma.conf.js'
    },

    continuous: {
        logLevel:  'INFO',
        singleRun: false,
    }
}

karma.conf.js

The trickiest part:

  • We add requirejs as a framework.
  • We load all transpiled files, but with the include flag set to false.
  • A file called test-main.js then scan these files and dynamically load all .spec files for the tests. test-main.js was generated by Karma, see this guide for more info.

Like so:

module.exports = function(config) {
    config.set({
        // Note that we also use `requirejs`
        frameworks: [ 'jasmine', 'requirejs' ],

        files: [
            // We add all transpiled files (specs and unit) with included set
            // to false.
            { pattern: 'transpiled/**/*.js', included: false },

            // Test main will search the files for specs and load them
            // dynamically.
            'test-main.js'
        ],

        reporters: [ 'progress' ],

        browsers: [ 'PhantomJS' ],

        autoWatch: false,
        singleRun: true
    })
}

Upvotes: 2

Related Questions