karma-webpack: Syntax error when using webpack 5 and karma 6

Apologies if this isn’t the correct place to report this, but getting a bit desparate. I’ve converted my app to work with webpack 5, but I’m unable to get the tests running. All configs look correct. I’ve also tried more or less plugging in the example config from here, but still getting the same error: https://github.com/appzuka/karma-webpackv5-test

Getting a strong feeling that something related to webpack or karma is completely off, but I’m unable to figure out what it is. As described below, the weird message I get no matter what I do is this:

Chrome 89.0.4389.114 (Windows 10) ERROR
  Uncaught SyntaxError: Unexpected token ':'
  at C:/Users/einarq/AppData/Local/Temp/_karma_webpack_1/commons.js:3:66

I’ve pasted the beginning of that generated file below.

  • Operating System: Windows 10
  • Node Version: 14.15.4
  • NPM Version: 6.14.10
  • webpack Version: 5.31.0
  • karma-webpack Version: 5.0.0

Expected Behavior

Tests should run fine

Actual Behavior

08 04 2021 20:38:17.964:INFO [karma-server]: Karma v6.3.2 server started at http://localhost:9999/ 08 04 2021 20:38:17.965:INFO [launcher]: Launching browsers ChromeNoSandbox with concurrency unlimited 08 04 2021 20:38:17.971:INFO [launcher]: Starting browser Chrome 08 04 2021 20:38:20.213:INFO [Chrome 89.0.4389.114 (Windows 10)]: Connected on socket Arz-bQzd3v3C9CF0AAAB with id 68760601 Chrome 89.0.4389.114 (Windows 10) ERROR Uncaught SyntaxError: Unexpected token ‘:’ at C:/Users/einarq/AppData/Local/Temp/_karma_webpack_1/commons.js:3:66

SyntaxError: Unexpected token ‘:’

Code

{
      mode: 'development',
      output: {
        publicPath: '/',
      },
      cache: true,
      devtool: false,
      resolve: {
        modules: [path.resolve('./www/js/app'), 'www/js/libs', 'www/svg', 'node_modules', 'test/jasmine-ui', 'server'],
        extensions: ['.js', '.jsx', '.html', '.css', '.svg'],
        fallback: {stream: require.resolve('stream-browserify')},
      },
      plugins: [
        new webpack.ProvidePlugin({
          process: 'process/browser',
        }),
      ],
      module: {
        rules: [
          {
            test: /\.(jsx|js)?$/,
            include: [path.resolve(__dirname, 'www/js/app'), path.resolve(__dirname, 'server')],
            use: [
              {
                loader: 'babel-loader',
                options: {
                  cacheDirectory: true,
                },
              },
            ],
          },
        ],
      },
    },
    webpackMiddleware: {
      noInfo: true
    },
  }

Geneated file contents:

{
/***/ "./node_modules/@babel/runtime-corejs2/core-js/date/now.js":
/*!*****************************************************************!*\
  !*** ./node_modules/@babel/runtime-corejs2/core-js/date/now.js ***!
  \*****************************************************************/
/*! dynamic exports */
/*! export __esModule [maybe provided (runtime-defined)] [no usage info] [provision prevents renaming (no use info)] -> ./node_modules/@babel/runtime-corejs2/node_modules/core-js/library/fn/date/now.js .__esModule */
/*! other exports [maybe provided (runtime-defined)] [no usage info] -> ./node_modules/@babel/runtime-corejs2/node_modules/core-js/library/fn/date/now.js */
/*! runtime requirements: module, __webpack_require__ */
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

module.exports = __webpack_require__(/*! core-js/library/fn/date/now */ "./node_modules/@babel/runtime-corejs2/node_modules/core-js/library/fn/date/now.js");

/***/ }),

/***/ "./node_modules/@babel/runtime-corejs2/core-js/number/is-integer.js":
/*!**************************************************************************!*\
  !*** ./node_modules/@babel/runtime-corejs2/core-js/number/is-integer.js ***!

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 18 (5 by maintainers)

Most upvoted comments

I’m working on the same project as @einarq , and can it definitely looks to be something with the way our browserslist is defined.

If we remove the environments from our browserslist and f.ex change the browserslist to either:

last 2 Chrome versions
not Chrome < 60
last 2 Firefox versions
not Firefox < 54

or

last 5 Chrome versions
last 5 Firefox versions
last 5 Edge versions
Safari >= 10
iOS >= 10.1
IE >= 11

the tests runs fine.

Another option is to leave the browserslist as-is with both environments defined, and instead set the target config in karma/webpack config to either target: 'browserslist:modern or target: 'browserslist:legacy'. When doing this, the tests also run fine. This is what we are doing for now.

It’s a webpack option, so we put it on the weppack part of the karma.conf.js file:

webpack: { mode: ‘development’,

output: { publicPath: ‘/’, },

cache: true,

target: ‘browserslist:modern’,

On Wed, Aug 25, 2021 at 10:20 AM Peaceful James @.***> wrote:

@einarq https://github.com/einarq Thanks. I will try that. The target: ‘browserslist:modern’, that you put in karma.conf.js, is that a root level config option? I can’t find it in the docs.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ryanclark/karma-webpack/issues/507#issuecomment-905288965, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABT6D2LDOPIH5ZD3HSZYMLT6SRUTANCNFSM42UNNVNA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

This is a much smaller repro, I’ll try and look at this sometime over the weekend to troubleshoot.