4

我在运行 Karma 测试时遇到文件表溢出问题,我不知道如何调试它。

业力.conf.js:

module.exports = function (config) {
    config.set({
        frameworks: ['jspm', 'jasmine'],
        files: [

            'node_modules/karma-babel-preprocessor/node_modules/babel-core/browser-polyfill.js',
            'node_modules/jasmine-async-sugar/jasmine-async-sugar.js'
        ],

        jspm: {
            config: 'jspm.conf.js',
            loadFiles: ['src/app/app.js', 'src/app/**/*.spec.js'], //'src/app/**/!(*.e2e|*.po).js'
            serveFiles: ['src/app/**/*.+(js|html|css|json)'] // *.{a,b,c} to *.+(a|b|c) https://github.com/karma-runner/karma/issues/1532
        },

        proxies: {
            '/test/': '/base/test/',
            '/src/app/': '/base/src/app/',
            '/jspm_packages/': '/base/jspm_packages/'
        },

        //reporters: process.argv.slice(2).find((argv) => argv.includes('--nocoverage') || argv.includes('--no-coverage')) ? ['dots', 'junit'] : ['dots', 'junit', 'coverage'],

        // use dots reporter, as Travis terminal does not support escaping sequences;
        // when using Travis publish coverage to coveralls
        reporters: coveralls ? ['dots', 'junit', 'coverage', 'coveralls'] : nocoverage ? ['dots'] : ['dots', 'junit', 'coverage'],

        junitReporter: {
            outputDir: 'test-reports/unit-test-report/',
            suite: 'unit'
        },

        preprocessors: {
            // source files, that you wanna generate coverage for - do not include tests or libraries
            // (these files will be instrumented by Istanbul)
            'src/**/!(*.spec|*.mock|*-mock|*.e2e|*.po|*.test).js': ['babel', 'coverage']
        },

        // transpile with babel since the coverage reporter throws error on ES6 syntax
        babelPreprocessor: {
            options: {
                stage: 1,
                sourceMap: 'inline'
            }
        },

        coverageReporter: {
            instrumenters: { isparta : require('isparta') },
            instrumenter: {
                'src/**/*.js': 'isparta'
            },
            dir: 'test-reports/coverage/',
            subdir: normalizationBrowserName,
            reporters: [
                {type: 'html'}, // will generate html report
                {type: 'json'}, // will generate json report file and this report is loaded to make sure failed coverage cause gulp to exit non-zero
                {type: 'lcov'}, // will generate Icov report file and this report is published to coveralls
                {type: 'text-summary'} // it does not generate any file but it will print coverage to console
            ]
        },

        browsers: [process.env.TRAVIS ? 'Firefox' : 'Chrome'],

        browserNoActivityTimeout: 50000
    });

    function normalizationBrowserName(browser) {
        return browser.toLowerCase().split(/[ /-]/)[0];
    }
};

包.json:

"karma": "0.13.14",
"karma-jspm": "2.0.1",
"karma-jasmine": "0.3.6",
"karma-coverage": "douglasduteil/karma-coverage#next",
"karma-coveralls": "1.1.2",
"karma-ie-launcher": "0.2.0",
"karma-junit-reporter": "0.3.8",
"karma-chrome-launcher": "0.2.1",
"karma-safari-launcher": "0.1.1",
"karma-firefox-launcher": "0.1.6",
"karma-phantomjs-launcher": "0.2.1",
"karma-babel-preprocessor": "5.2.2"

测试单元.js:

 gulp.task('karma', (cb) => {
    // remove 'coverage' directory before each test
    del.sync(path.test.testReports.coverage);
    // run the karma test
    const server = new Server({
        configFile: path.test.config.karma,
        browsers: BROWSERS.split(','),
        singleRun: !argv.watch,
        autoWatch: !!argv.watch
    }, function(code) {
        // make sure failed karma tests cause gulp to exit non-zero
        if(code === 1) {
            LOG(COLORS.red('Error: unit test failed '));
            return process.exit(1);
        }
        cb();
    });
    server.start();
});

错误:

12) 在 /Users/Abhi/Documents/projects/test/node_modules/karma/lib/file-list.js:161:14 在 [object Object].List._refresh (/Users/Abhi) 的 Array.map (native) /Documents/projects/test/node_modules/karma/lib/file-list.js:153:37) 在 [object Object].List.refresh (/Users/Abhi/Documents/projects/test/node_modules/karma/lib/ file-list.js:252:27) 在 [object Object].Server._start (/Users/Abhi/Documents/projects/test/node_modules/karma/lib/server.js:177:12) 在 [object Object] .invoke (/Users/Abhi/Documents/projects/test/node_modules/karma/node_modules/di/lib/injector.js:75:15) 在 [object Object].Server.start (/Users/Abhi/Documents/projects /test/node_modules/karma/lib/server.js:101:18) 在 Gulp。(/Users/Abhi/Documents/projects/test/gulp/tasks/test-unit.js:53:12)在模块。

业力问题跟踪器:https ://github.com/karma-runner/karma/issues/1979

4

1 回答 1

4

阿比,

我为此度过了一个美好的下午,并找到了一种解决方法,我承认这是一个最好的猜测问题,我对为什么会发生零概念。

我的问题是 Karma 配置中 serveFiles 和 loadFiles 内部的 glob。为了解决这个问题,我使用 glob.sync 进行了自己的 globbing,以根据需要生成数组,这很有效!

module.exports = function(config) {

    var options = {cwd:"www"};
    var glob = require("glob");
    var filesToServe = glob.sync("./src/**/*.@(js|ts|css|scss|html)", options);
    var specsToLoad = glob.sync("./src/**/*.@(spec.ts)", options).map(function(file){
        return file.substr(2);
    });

    config.set({
        basePath: 'www',

        frameworks: ['jspm', 'jasmine'],

        jspm: {
            config: './test/config.js',
            loadFiles: ['test/boot.ts'].concat(specsToLoad),
            serveFiles: filesToServe,
        },

        files: [
        'http://cdn.auth0.com/js/lock-8.2.min.js'
        ],

        proxies: {
            '/src/': '/base/src/',
            '/.test/': '/base/.test/'
        },

        port: 9876,

        logLevel: config.LOG_DISABLE,

        colors: true,

        autoWatch: true,

        browsers: ['Chrome'],

        plugins: [
            'karma-jspm',
            'karma-jasmine',
            'karma-chrome-launcher',
            'karma-spec-reporter'
        ],

        reporters: ['spec'],

        singleRun: true
    });
};

filesToServe 和 specsToLoad 的处理方式略有不同,我需要从要加载的文件中删除 ./ ,因为它会干扰 SystemJS 的内部加载(这可以通过尝试加载 .ts.js 文件来识别)。我的工作也放在一个子文件夹“www”中,您可能不需要它,例如删除 cwd。

希望您能看到我在这里所做的事情,它可以帮助您找到解决方法。如果有人知道为什么像 glob 这样的东西会破裂,我很想知道。

为了证明它是 glob 我做了一个简单的测试,使用

require(glob)("src/**/*",function(file){ console.log(file); });

这触发了相同的错误,显然与太多文件或文件表问题无关。如果这在其他地方弹出,我认为我将不得不再次全新安装操作系统。但是在我的代码库中,我在其他地方使用 glob,没有任何问题。我想知道这是否可能是使用该过程的“同步”版本之间的区别......

经过一天的选择,最终来到这里……我希望能找到更好的答案。

ķ

于 2016-03-09T16:57:25.623 回答