mercredi 28 juin 2017

Angular 2/4 Karma testing

I have an error when trying to import a typescript class to my test file. This is my karma.conf.js

// Karma configuration
// Generated on Mon Jun 12 2017 14:21:41 GMT+0200 (Central European Daylight Time)

module.exports = function (config) {
    config.set({

        // base path that will be used to resolve all patterns (eg. files, exclude)
        basePath: 'src/app',


        // frameworks to use
        // available frameworks: http://ift.tt/1ft83uu
        frameworks: ['jasmine'],


        // list of files / patterns to load in the browser
        files: [
            { pattern: '**/*.spec.ts' }
        ],

        // list of files to exclude
        exclude: [
        ],

        // preprocess matching files before serving them to the browser
        // available preprocessors: http://ift.tt/1gyw6MG
        preprocessors: {
            '**/*.ts': ['typescript']
        },

        // test results reporter to use
        // possible values: 'dots', 'progress'
        // available reporters: http://ift.tt/1ft83KQ
        reporters: ['progress'],


        // web server port
        port: 9876,


        // enable / disable colors in the output (reporters and logs)
        colors: true,


        // level of logging
        // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
        logLevel: config.LOG_INFO,


        // enable / disable watching file and executing tests whenever any file changes
        autoWatch: true,


        // start these browsers
        // available browser launchers: http://ift.tt/1ft83KU
        browsers: ['Chrome'],


        // Continuous Integration mode
        // if true, Karma captures browsers, runs the tests and exits
        singleRun: false,

        // Concurrency level
        // how many browser should be started simultaneous
        concurrency: Infinity,

        mime: {
            'text/x-typescript': ['ts', 'tsx']
        }
    });
};

I have wrote a simple test which does nothing but tries to import a class:

import { ApiResponseTimeRepository } from './ApiResponseTimeRepository';


describe('testing test', () => {
    it('should pass', () => {
        expect(1 + 1).toBe(2);
    });
});

But when I run the test I get Uncaught ReferenceError: exports is not defined at TestRepositories.spec.js:2.

Test works fine when I omit the import part, so I figured there was a problem with typescript transpiling, so I installed karma-typescript-preprocessor but I keep getting the error. What seems to be the problem?

Aucun commentaire:

Enregistrer un commentaire