运行打字稿单元测试时,PhantomJS'window.Promise未定义'

时间:2017-04-02 14:25:57

标签: unit-testing typescript phantomjs karma-runner karma-jasmine

我正在尝试运行Typescript类的单元测试,但是当连接到PhantomJS浏览器时,我收到有关丢失Promise的错误。下面我附上一些我正在使用的配置。我想要实现的是在Typescript中编写测试并使用ES6功能,如导入和箭头功能。

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',
});

'业力开始'输出

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'

有人知道该设置有什么问题吗?

1 个答案:

答案 0 :(得分:1)

所以,我想我最终已经设法自己解决了我的问题。对我有用的配置是:

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',
});

当然有必要使用npm install安装使用过的软件包。重要的是,出于某种原因,必须使用版本0.19.24的systemjs。将它留给今后将面临同样问题的任何人。