【问题标题】:PhantomJS 'window.Promise is undefined' when running typescript unit test运行打字稿单元测试时 PhantomJS 'window.Promise is undefined'
【发布时间】:2017-04-02 14:25:57
【问题描述】:

我正在尝试为 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'

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

【问题讨论】:

  • 我试过这个解决方案,但它不起作用(同样的错误)。我认为它适用于 .js 文件,而我正在用 typescript 编写测试

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


【解决方案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。留在这里给以后遇到同样问题的人。

【讨论】:

  • 这里的关键是其中一个模块的重大变化
猜你喜欢
  • 2020-07-10
  • 2019-04-05
  • 2021-03-19
  • 2018-09-06
  • 1970-01-01
  • 2021-10-20
  • 1970-01-01
  • 2017-10-26
  • 2019-09-08
相关资源
最近更新 更多