Karma Coverage无法解析文件并出现意外令牌错误

时间:2018-11-22 09:14:59

标签: karma-jasmine karma-coverage

我正在使用业力来测试和报告项目范围。 但我所有的js文件总是收到此错误:

  

无法解析文件:/home/.../src/services/Video/videoServices.js
  22 11 2018 16:16:23.438:错误[preprocessor.coverage]:第1行:意外令牌
  在/ home /.../ src / services / Video / videoServices.js

所有单元测试均正常运行:

PhantomJS 2.1.1 (Linux 0.0.0): Executed 10 of 10 SUCCESS (0.07 secs / 0.058 secs)

但是覆盖率始终为100%0/0。看起来因果报应无法解析我的代码,因此无法呈现覆盖范围。

enter image description here

这是我的karma.config.js文件:

/* eslint-env node, es6 */
const path = require('path');
const autoprefixer = require('autoprefixer');
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
const flexbugfixes = require('postcss-flexbugs-fixes');
const globalImport = require('postcss-global-import');
const LessPluginRi = require('resolution-independence');
const {DefinePlugin, EnvironmentPlugin} = require('webpack');
const {optionParser: app, EnzymeAdapterPlugin, GracefulFsPlugin, ILibPlugin} = require('@enact/dev-utils');

process.env.ES5 = 'true';

module.exports = function(karma) {
    karma.set({
        basePath: process.cwd(),
        frameworks: ['mocha', 'chai', 'dirty-chai', 'jasmine'],
        files: [
            require.resolve('@babel/polyfill/dist/polyfill'),
            require.resolve('mocha-react-proptype-checker'),
            {pattern: 'src/**/*-specs.js', watched: true, serve: true, included: true},
            {pattern: 'src/**/*.js', watched: true, serve: true, included: true}
        ],
        reporters: ['progress', 'coverage'],
        preprocessors: {
            // add webpack as preprocessor
            './!(node_modules|dist|build)/**/*.js': ['webpack'],
            [require.resolve('mocha-react-proptype-checker')]: ['webpack'],
            'src/**/*.js': ['coverage']
        },

        failOnEmptyTestSuite: true,

        webpack: {
            // Use essentially the same webpack config as from the development build setup.
            // We do not include an entry value as Karma will control that.
            devtool: false,
            output: {
                path: './dist',
                filename: '[name].js'
            },
            resolve: {
                extensions: ['.js', '.jsx', '.json'],
                modules: [
                    path.resolve(app.context, './node_modules'),
                    'node_modules',
                ],
                alias: {
                    ilib: '@enact/i18n/ilib/lib',
                    'react-addons-test-utils': 'react-dom/test-utils'
                }
            },
            externals: {
                cheerio: 'window',
                'react/addons': true,
                'react/lib/ExecutionEnvironment': true,
                'react/lib/ReactContext': true
            },
            target: app.environment,
            node: Object.assign({}, app.nodeBuiltins || {}, {
                console: true,
                fs: 'empty',
                net: 'empty',
                tls: 'empty'
            }),
            module: {
                rules: [
                    {
                        exclude: /\.(html|js|jsx|css|less|ejs|json|txt)$/,
                        loader: require.resolve('file-loader'),
                        options: {name: '[path][name].[ext]'}
                    },
                    {
                        test: /\.(html|txt)$/,
                        loader: require.resolve('raw-loader')
                    },
                    {
                        test: /\.(js|jsx)$/,
                        exclude: /node_modules.(?!@enact)/,
                        loader: require.resolve('babel-loader'),
                        options: {
                            cacheDirectory: true,
                            // Generate a unique identifier string based off versons of components and app config.
                            cacheIdentifier: JSON.stringify({
                                'babel-loader': require('babel-loader/package.json').version,
                                'babel-core': require('@babel/core/package.json').version,
                                browsers: app.browsers,
                                node: app.node
                            })
                        }
                    },
                    {
                        test: /\.(c|le)ss$/,
                        use: [
                            require.resolve('style-loader'),
                            {
                                loader: require.resolve('css-loader'),
                                options: {
                                    importLoaders: 2,
                                    modules: true,
                                    localIdentName: '[name]__[local]___[hash:base64:5]'
                                }
                            },
                            {
                                loader: require.resolve('postcss-loader'),
                                options: {
                                    ident: 'postcss',
                                    plugins: () => [
                                        autoprefixer({
                                            browsers: app.browsers,
                                            flexbox: 'no-2009',
                                            remove: false
                                        }),
                                        flexbugfixes,
                                        globalImport
                                    ]
                                }
                            },
                            {
                                loader: require.resolve('less-loader'),
                                options: {
                                    modifyVars: Object.assign({}, app.accent),
                                    plugins: app.ri ? [new LessPluginRi(app.ri)] : []
                                }
                            }
                        ]
                    }
                ],
                noParse: /node_modules\/json-schema\/lib\/validate\.js/
            },
            devServer: {host: '0.0.0.0', port: 8080},
            plugins: [
                new DefinePlugin({'process.env.NODE_ENV': JSON.stringify('development')}),
                new EnvironmentPlugin(Object.keys(process.env).filter(key => /^REACT_APP_/.test(key))),
                new CaseSensitivePathsPlugin(),
                new GracefulFsPlugin(),
                new ILibPlugin({create: false}),
                new EnzymeAdapterPlugin()
            ]
        },
        webpackServer: {
            // please don't spam the console when running in karma!
            noInfo: true,
            progress: false,
            stats: {
                assets: false,
                chunkModules: false,
                chunks: false,
                colors: true,
                errorDetails: false,
                hash: false,
                reasons: false,
                timings: false,
                version: false,
                children: false,
                warnings: false,
                moduleTrace: false
            }
        },
        plugins: [
            'karma-webpack',
            'karma-mocha',
            'karma-chai',
            'karma-dirty-chai',
            'karma-chrome-launcher',
            'karma-phantomjs-launcher',
            'karma-json-reporter',
            'karma-jasmine-jquery',
            'karma-jasmine',
            'karma-coverage',
        ],
        jsonReporter: {
            stdout: true
        },
        client: {
            mocha: {
                timeout: 30000
            }
        },
        port: 9876,
        colors: true,
        logLevel: karma.LOG_INFO,
        browserNoActivityTimeout: 60000,
        autoWatch: true,
        browsers: ['PhantomJS'],
        singleRun: false,
        coverageReporter: { 
            includeAllSources: true,       
            dir: 'coverage/',
            reporters: [
                    { type: 'html', subdir: 'html' },                
                    { type: 'teamcity' },
            ]
        },

    });
};

和我的package.json文件:

{
"main": "src/index.js",
  "scripts": {
    "build": "webpack --config ./config/webpack.config.prod",
    "pack": "node ./scripts/pack.js",
    "pack-p": "node ./scripts/pack.js -p",
    "serve": "webpack --config ./config/webpack.config.dev && node ./scripts/serve.js",
    "watch": "node ./scripts/pack.js --watch",
    "clean": "node ./scripts/clean.js",
    "lint": "node ./scripts/lint.js .",
    "license": "node ./scripts/license.js",
    "test": "node ./scripts/test.js start --single-run",
    "test-watch": "node ./scripts/test.js start"
  },
  "license": "Copyright (c) 2018 Framgia Inc",
  "private": true,
  "repository": "",
  "enact": {
    "theme": "moonstone"
  },
  "eslintIgnore": [
    "node_modules/*",
    "build/*",
    "dist/*",
    "config/*",
    "scripts/*"
  ],
  "eslintConfig": {
    "extends": "enact"
  },
  "babel": {
    "extends": "./config/.babelrc.js"
  },
  "dependencies": {
    "@enact/cli": "^1.2.0",
    "@enact/core": "^2.1.1",
    "@enact/i18n": "^2.1.1",
    "@enact/moonstone": "^2.1.1",
    "@enact/spotlight": "^2.1.1",
    "@enact/ui": "^2.1.1",
    "@enact/webos": "^2.1.1",
    "axios": "^0.18.0",
    "i18next": "^11.6.0",
    "kadira-storybook": "^1.38.3",
    "lodash": "^4.17.10",
    "moment": "^2.22.2",
    "moment-duration-format": "^2.2.2",
    "polished": "^1.9.3",
    "prop-types": "^15.6.2",
    "react": "^16.4.2",
    "react-dom": "^16.4.2",
    "react-inlinesvg": "^0.8.1",
    "react-loadable": "^5.5.0",
    "react-native-base64": "0.0.2",
    "react-redux": "^5.0.7",
    "react-router-dom": "^4.3.1",
    "react-text-marquee": "^1.0.1",
    "react-truncate": "^2.4.0",
    "react-truncate-markup": "^2.0.1",
    "reactstrap": "^6.4.0",
    "redux": "^4.0.0",
    "redux-actions": "^2.6.1",
    "redux-logger": "^3.0.6",
    "redux-saga": "^0.16.0",
    "styled-components": "^3.4.2"
  },
  "devDependencies": {
    "@babel/core": "7.0.0-beta.41",
    "@babel/plugin-transform-react-inline-elements": "7.0.0-beta.41",
    "@babel/plugin-transform-react-jsx-self": "7.0.0-beta.41",
    "@babel/plugin-transform-react-jsx-source": "7.0.0-beta.41",
    "@babel/polyfill": "7.0.0-beta.41",
    "@babel/preset-env": "7.0.0-beta.41",
    "@babel/preset-react": "7.0.0-beta.41",
    "@babel/preset-stage-0": "7.0.0-beta.41",
    "@enact/dev-utils": "1.1.1",
    "@enact/template-moonstone": "1.0.0",
    "autoprefixer": "8.1.0",
    "babel-core": "^6.26.3",
    "babel-eslint": "8.2.2",
    "babel-loader": "8.0.0-beta.2",
    "babel-plugin-dev-expression": "0.2.1",
    "babel-plugin-transform-react-remove-prop-types": "^0.4.14",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "case-sensitive-paths-webpack-plugin": "2.1.2",
    "chai": "3.5.0",
    "chalk": "2.3.2",
    "console-snoop": "0.1.1",
    "cross-spawn": "6.0.5",
    "css-loader": "0.28.10",
    "dirty-chai": "2.0.1",
    "enzyme": "3.3.0",
    "enzyme-adapter-react-16": "1.1.1",
    "eslint": "^5.3.0",
    "eslint-config-airbnb": "^17.1.0",
    "eslint-config-enact": "1.2.0",
    "eslint-import-resolver-babel-module": "^4.0.0",
    "eslint-loader": "1.9.0",
    "eslint-plugin-babel": "4.1.2",
    "eslint-plugin-enact": "0.1.3",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-jsx-a11y": "^6.1.1",
    "eslint-plugin-react": "^7.10.0",
    "expose-loader": "0.7.5",
    "extract-text-webpack-plugin": "3.0.2",
    "file-loader": "1.1.11",
    "filesize": "3.6.0",
    "fs-extra": "5.0.0",
    "html-webpack-plugin": "2.30.1",
    "istanbul": "^0.4.5",
    "jest": "^23.6.0",
    "karma": "^1.5.0",
    "karma-babel-preprocessor": "8.0.0-beta.0",
    "karma-chai": "0.1.0",
    "karma-chrome-launcher": "2.2.0",
    "karma-coverage": "^1.1.2",
    "karma-dirty-chai": "2.0.0",
    "karma-jasmine": "^2.0.1",
    "karma-jasmine-jquery": "^0.1.1",
    "karma-json-reporter": "1.2.1",
    "karma-mocha": "1.3.0",
    "karma-phantomjs-launcher": "1.0.4",
    "karma-sourcemap-loader": "0.3.7",
    "karma-webpack": "2.0.6",
    "less": "3.0.1",
    "less-loader": "4.1.0",
    "license-checker": "16.0.0",
    "minimist": "1.2.0",
    "mocha": "5.0.4",
    "mocha-react-proptype-checker": "0.1.0",
    "phantomjs": "^2.1.7",
    "postcss-flexbugs-fixes": "3.3.0",
    "postcss-global-import": "1.0.6",
    "postcss-loader": "2.1.1",
    "postcss-remove-classes": "1.0.4",
    "raw-loader": "1.0.0-beta.0",
    "react-dev-utils": "5.0.0",
    "react-test-renderer": "16.4.1",
    "redux-mock-store": "^1.5.3",
    "resolution-independence": "1.0.0",
    "sinon": "4.4.6",
    "strip-ansi": "4.0.0",
    "style-loader": "0.20.3",
    "thread-loader": "1.1.5",
    "uglifyjs-webpack-plugin": "1.2.5",
    "webpack": "3.11.0",
    "webpack-dev-server": "2.11.1"
  }
}

我的树文件夹如下:

-App
 |-node_modules
 |-src
 | |- some_code.js
 | |- tests
 | | |-some_code-specs.js

我坚持了几天,尝试大量更改业力配置并找到除业力覆盖范围之外的另一个覆盖率工具,但无济于事。任何人都对这个问题有理想的选择吗?谢谢您的宝贵时间。

0 个答案:

没有答案