javascript - Karma - 如果我不使用 PhantomJS 浏览器,则不会运行测试

标签 javascript angular karma-runner karma-jasmine jhipster

我正在尝试为我的 jHipster 项目编写 Karma 测试。我收到了 TypeError: Attempted to allocate to readonly property 因此我决定尝试通过浏览器对其进行调试。

原本使用的浏览器是PhantomJS,但为了能够调试,我将其更改为Firefox。我还添加了详细信息。

但不幸的是,当我使用与 PhantomJS 不同的东西时,没有测试正在运行:

Firefox 53.0.0 (Windows 7 0.0.0): Executed 0 of 0 SUCCESS (0 secs / 0 secs)
Firefox 53.0.0 (Windows 7 0.0.0): Executed 0 of 0 ERROR (0.004 secs / 0 secs)
15 06 2017 11:08:20.352:DEBUG [reporter.remap-istanbul]: Writing coverage to target/test-results/coverage
15 06 2017 11:08:20.422:DEBUG [reporter.remap-istanbul]: Writing coverage to null

=============================== Coverage summary ===============================
Statements   : 100% ( 0/0 )
Branches     : 100% ( 0/0 )
Functions    : 100% ( 0/0 )
Lines        : 100% ( 0/0 )

这是我的 karma.conf.js :

'use strict';

const path = require('path');
const webpack = require('webpack');
const WATCH = process.argv.indexOf('--watch') > -1;
const LoaderOptionsPlugin = require("webpack/lib/LoaderOptionsPlugin");

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

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

    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine', 'intl-shim'],

    // list of files / patterns to load in the browser
    files: [
        'spec/entry.ts'
    ],


    // list of files to exclude
    exclude: ['e2e/**'],

    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
        'spec/entry.ts': ['webpack', 'sourcemap']
    },

    webpack: {
        resolve: {
            extensions: ['.ts', '.js']
        },
        module: {
            rules: [
                {
                    test: /\.ts$/, enforce: 'pre', loader: 'tslint-loader', exclude: /(test|node_modules)/
                },
                {
                    test: /\.ts$/,
                    loaders: ['awesome-typescript-loader', 'angular2-template-loader?keepUrl=true'],
                    exclude: /node_modules/
                },
                {
                    test: /\.(html|css)$/,
                    loader: 'raw-loader',
                    exclude: /\.async\.(html|css)$/
                },
                {
                    test: /\.async\.(html|css)$/,
                    loaders: ['file?name=[name].[hash].[ext]', 'extract']
                },
                {
                    test: /\.scss$/,
                    loaders: ['to-string-loader', 'css-loader', 'sass-loader']
                },
                {
                    test: /src[\/|\\]main[\/|\\]webapp[\/|\\].+\.ts$/,
                    enforce: 'post',
                    exclude: /(test|node_modules)/,
                    loader: 'sourcemap-istanbul-instrumenter-loader?force-sourcemap=true'
                },
                {
                    test: /\.(jpe?g|png|gif|svg|woff|woff2|ttf|eot)$/i,
                    loaders: [
                        'file-loader?hash=sha512&digest=hex&name=[hash].[ext]', {
                            loader: 'image-webpack-loader',
                            query: {
                                gifsicle: {
                                    interlaced: false
                                },
                                optipng: {
                                    optimizationLevel: 7
                                }
                            }
                        }
                    ]
                }
                ]
        },
        devtool: 'inline-source-map',
        plugins: [
            new webpack.ContextReplacementPlugin(
                // The (\\|\/) piece accounts for path separators in *nix and Windows
                /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
                root('./src') // location of your src
            ),
            new LoaderOptionsPlugin({
                options: {
                    tslint: {
                        emitErrors: !WATCH,
                        failOnHint: false
                    }
                }
            })
        ]
    },

    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['dots', 'junit', 'progress', 'karma-remap-istanbul'],

    junitReporter: {
        outputFile: '../../../../target/test-results/karma/TESTS-results.xml'
    },

    remapIstanbulReporter: {
        reports: { // eslint-disable-line
            'html': 'target/test-results/coverage',
            'text-summary': null
        }
    },

    // 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_DEBUG,

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

    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Firefox'],

    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: !WATCH
    });
};

function root(__path) {
    return path.join(__dirname, __path);
}

最佳答案

通过添加修复:

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

karma.conf.js 中。

关于javascript - Karma - 如果我不使用 PhantomJS 浏览器,则不会运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44564820/

相关文章:

javascript - 使 'scroll-sneak' 工作

javascript - react native - 在 FlatList 中有静态和动态元素

javascript - Sencha 触摸2 : How can I add Google+ Login?

javascript - 在 Angular 2/Ionic 2 中访问表单中选定的对象属性

angular - NgClass 没有在 true 上添加所有指定的类

javascript - Karma 未按预期运行 Angular 测试

angularjs - 使用 angularjs 和 karma 测试登录表单

javascript - Node.js - jQuery。从服务器获取PDF并在前端显示

javascript - 使用 Open WC Karma-ESM 插件测试 Typescript 文件时出错

css - :host , :host() , 之间有什么不同:主机上下文选择器