0

I am trying to run my unit tests for Typescript class, but I am getting an error about missing Promise when connected to PhantomJS browser. Below I attach some configuration I am using. What I want to achieve is to write tests in Typescript and use ES6 features like imports and arrow functions.

karma.conf.js

module.exports = function (config) {
    config.set({
        basePath: '',
        frameworks: ['systemjs', 'jasmine'],
        plugins: [
            'es6-module-loader',
            'karma-systemjs',
            'karma-jasmine',
            "karma-spec-reporter",
            "karma-phantomjs-launcher"
        ],
        files: [
            'app/test/**/*.spec.ts',
        ],
        systemjs: {
            configFile: './karma.system.conf.js',
            config: {
                baseURL: './'
            },
            serveFiles: [
            ]
        },
        exclude: [],
        preprocessors: {},
        reporters: ['spec'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['PhantomJS'],
        singleRun: true,
        concurrency: Infinity
    })
}

karma.system.conf.js

System.config({
    paths: {
        'es6-module-loader': 'node_modules/es6-module-loader/dist/es6-module-loader.js',
        'jasmine': 'node_modules/karma-jasmine/*',
        systemjs: 'node_modules/systemjs/dist/system.js',
        typescript: 'node_modules/typescript/lib/typescript.js',
        'plugin-typescript': 'node_modules/plugin-typescript/lib/plugin.js'
    },
    meta: {
        '*.ts': {
            format: 'es6'
        }
    },
    packages: {
        'app/': { defaultExtension: 'ts' }
    },
    transpiler: 'typescript',
});

'karma start' output

PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
ReferenceError: Can't find variable: Promise
at node_modules/systemjs/dist/system.js:5

PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 0 of 0 ERROR (0.042 secs / 0 secs)

PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR LOG: 'Error: Not setup properly.  window.Promise is undefined'

Do anyone has an idea what is wrong with that setup?

Nevaan
  • 129
  • 2
  • 11
  • duplicate of http://stackoverflow.com/questions/29391111/karma-phantomjs-and-es6-promises#31166888 ? – timocov Apr 02 '17 at 20:27
  • I tried this solution, and it is not working (same error). I think it applies to .js files while I am having tests written in typescript – Nevaan Apr 03 '17 at 08:50

1 Answers1

1

So, I think I have managed to solve my problem by myself at the end. Configuration which worked for me is:

karma.conf.js

module.exports = function (config) {
    config.set({
        basePath: '',
        frameworks: ['systemjs', 'jasmine'],
        plugins: [
            'karma-systemjs',
            'es6-module-loader',
            'karma-jasmine',
            "karma-spec-reporter",
            "karma-phantomjs-launcher"
        ],
        files: [
            {pattern: 'app/**/*.ts', included: false, watched: true},
            {pattern: 'node_modules/systemjs/dist/system-polyfills.js', included: true, watched: true},
            'app/test/**/*.spec.ts',
        ],
        systemjs: {
            configFile: './karma.system.conf.js',
            config: {
                baseURL: ''
            },
            serveFiles: [
            ]
        },
        exclude: [],
        preprocessors: {},
        reporters: ['spec'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['PhantomJS'],
        singleRun: true,
        concurrency: Infinity
    })
}

karma.system.conf.js

System.config({
    paths: {
        'systemjs': 'node_modules/systemjs/dist/system.js',
        'system-polyfills': 'node_modules/systemjs/dist/system-polyfills.js',
        'es6-module-loader': 'node_modules/es6-module-loader/dist/es6-module-loader.js',
        'jasmine': 'node_modules/karma-jasmine/*',
        typescript: 'node_modules/typescript/lib/typescript.js',
        'plugin-typescript': 'node_modules/plugin-typescript/lib/plugin.js',
        'phantomjs-polyfill': 'node_modules/phantomjs-polyfill/bind-polyfill.js'
    },
    map: {
        'systemjs': 'node_modules/systemjs/dist/system.js',
        'system-polyfills': 'node_modules/systemjs/dist/system-polyfills.js',
        'es6-module-loader': 'node_modules/es6-module-loader/dist/es6-module-loader.js'
    },
    meta: {
        '*.ts': {
            format: 'es6'
        }
    },
    packages: {
        'app/': { defaultExtension: 'ts' }
    },
    transpiler: 'typescript',
});

Of course it was necessary to install used packages with npm install. What is essential, you have to use systemjs at version 0.19.24 for some reason. Leaving it here for anyone who will face the same problem in future.

Nevaan
  • 129
  • 2
  • 11