Syntax error when using webpack 5 and karma 6

See original GitHub issue

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 ***!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:18

github_iconTop GitHub Comments

2reactions
haakemoncommented, Apr 21, 2021

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.

1reaction
einarqcommented, Aug 25, 2021

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 .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · ryanclark/karma-webpack - GitHub
Syntax error when using webpack 5 and karma 6. #507 opened on Apr 9, 2021 by einarq · 18. Bundle is invalid when...
Read more >
webpack + Karma configuration through Uncaught SyntaxError
I configure webpack + karma + angular 2 and have some Uncaught SyntaxError : Unexpected token import with it. I do not understand...
Read more >
Configuration File - Karma
The easiest way to generate an initial configuration file is by using the karma init command. ... You might get errors like SyntaxError:...
Read more >
an error was thrown in afterall error: expected false to be true.
Angular 6 Unit Tests: An error was thrown in afterAll\nReferenceError: Can't ... I'm using webpack 4.35, karma 6.3 karma-webpack 4.0.2, karma-jasmine 2.0.1 ...
Read more >
Asset Modules - webpack
Asset Modules is a type of module that allows one to use asset files (fonts, icons, etc) without configuring additional loaders. Prior to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found