JSPM

  • Created
  • Published
  • Downloads 84341
  • Score
    100M100P100Q149022F
  • License MIT

Preprocessor for transpiling Typescript files on the fly with sourcemaps for debugging.

Package Exports

  • karma-typescript

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (karma-typescript) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

karma-typescript

This is a preprocessor for transpiling Typescript on the fly for unit testing with Karma.

Sourcemaps

Sourcemaps can optionally be created for debugging in a web browser when running tests in a web browser.

Local tsconfig.json

If specified, the preprocessor will load a tsconfig.json file, relative to the directory the test are run from, and merge the compiler options with the preprocessor options.

Note: the preprocessor options will override the tsconfig.json options.

Npm installation:

npm install karma-typescript --save-dev

Configuration

The karmaTypescript configuration section is optional, if left out the files will be transpiled using the compiler default options.

preprocessors: {
    '**/*.ts': ['karma-typescript']
},

karmaTypescript: {

    /*
    Relative path to a tsconfig.json file, the preprocessor will look for the file from the directory where karma was started.
    This property is optional.
    */
    tsconfigPath: 'tsconfig.json',

    /*
    A function for custom file path transformation.
    This property is optional.
    */
    transformPath: function(filepath) {
        return filepath.replace(/\.ts$/, '.js')
    },

    /*
    These are options for the Typescript compiler.
    They will override the options in the tsconfig.json file specified in tsconfigPath above.
    This property is optional.
    */
    options: {
        sourceMap: true,
        target: 'es5',
        module: 'commonjs'
        // ... For more options, please see https://www.typescriptlang.org/docs/handbook/compiler-options.html
    }
}

Example project using jasmine and commonjs + sourcemaps:

There is a working example in the folder example-project.

In the example, the .ts files are transpiled to es5 format with sourcemaps, and then run through the commonjs preprocessor for module loading.

To run the example test, issue the following commands:

cd example-project
npm install
npm test

This should run the unit tests in Chrome, with source maps available if you press the button debug in the browser window.

Example breakdown

The unit test:

import { IHelloService } from './hello-service.interface';
import { HelloComponent } from './hello.component';

class MockHelloService implements IHelloService {

    public sayHello(): string {
        return 'hello world!';
    }
}

describe('verb page', () => {

    it('should', () => {

        let mockHelloService = new MockHelloService();
        let helloComponent = new HelloComponent(mockHelloService);

        expect(helloComponent.sayHello()).toEqual('hello world!');
    });
});

Essential parts of karma.conf.js:

frameworks: ['jasmine', 'commonjs'],

files: [
    { pattern: 'src/**/*.ts' }
],

preprocessors: {
    '**/*.ts': ['karma-typescript', 'commonjs']
},

karmaTypescript: {
    tsconfigPath: 'tsconfig.json',
    options: {
        sourceMap: true,
        target: 'es5',
        module: 'commonjs'
    }
}

Licensing

This software is licensed with the MIT license.

© 2016 Monounity