意想不到的令牌' const'当运行webpack,babel和业力时

时间:2018-05-22 08:25:18

标签: webpack karma-runner babeljs

我正在设置我的业力测试,如https://zirho.github.io/2016/06/06/karma-es6/

所示

然后我有错误:

PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
  {
    "message": "An error was thrown in afterAll\nSyntaxError: Unexpected token 'const'",
    "str": "An error was thrown in afterAll\nSyntaxError: Unexpected token 'const'"
  }
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 0 of 0 ERPhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 0 of 0 ERROR (0.003 secs / 0 secs)

我用Google搜索并按照这里的说明进行操作:SyntaxError: Unexpected token 'const' (with Vue, Karma, Webpack, PhantomJS)

但仍然没有运气。

以下是我的档案。

予。的package.json

"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-plugin-lodash": "^3.2.11",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-register": "^6.24.1",
"jasmine-core": "^3.1.0",
"karma": "^2.0.2",
"karma-jasmine": "^1.1.2",
"karma-phantomjs-launcher": "^1.0.4",
"karma-webpack": "^3.0.0",
"phantomjs-prebuilt": "^2.1.16",
"webpack": "^3.4.1",
"webpack-bundle-analyzer": "^2.9.0",
"webpack-dev-server": "^2.6.1"

II。 karma.conf.js

我的前端代码位于/ frontend /.

let karmaInitFile = 'frontend/test/helpers/karma.js';

module.exports = function(config) {
    config.set({
        browsers: ['PhantomJS'],
        files: [
            { pattern: karmaInitFile, watched: false }
        ],
        frameworks: ['jasmine'],
        preprocessors: {
            'frontend/test/**/*.js': ['webpack']
        },
        webpack: {
            module: {
                loaders: [
                    { test: /frontend\/.*\.js$/, 
                        exclude: /node_modules/, 
                        use: {
                            loader: 'babel-loader',
                            options: {
                                presets: ['es2015']
                            }
                        }
                    }
                ]
            },
            watch: true,
            resolve: {
                modules: [
                    'frontend',
                    'node_modules'
                ]
            }
        },
        webpackServer: {
            noInfo: true
        }
    });
};

III。前端/测试/助手/ karma.js

require("babel-polyfill");
var context = require.context('..', true, /_karma\.js$/);
context.keys().forEach(context);

1 个答案:

答案 0 :(得分:1)

我解决了问题,但最终遇到了许多问题。因此,决定使用不需要浏览器界面弹出的ChromeHeadless驱动程序。

config.set({
    basePath: 'frontend',
    browsers: ['ChromeHeadless'],
    files: [
        'karma/**/*_test.js'
    ],
...})