4

Repo with an example: https://github.com/OriolInvernonLlaneza/karma-webpack-error-example

I had Karma + Jasmine tests running correctly with Angular 11 and Webpack 4. However, when trying to update Angular and Webpack to v12 and v5, I'm getting stuck on the following error when launching the tests:

enter image description here

This is my Karma conf:

module.exports = (config) => {
  config.set({
    // ... normal karma configuration

    // make sure to include webpack as a framework
    frameworks: ['jasmine', 'webpack'],

    plugins: [
      'karma-webpack',
      'karma-jasmine',
      'karma-chrome-launcher',
      'karma-jasmine-html-reporter',
      'karma-coverage-istanbul-reporter',
      'karma-sourcemap-loader',
    ],

    files: [
      // all files ending in ".spec.ts"
      // !!! use watched: false as we use webpacks watch
      { pattern: './src/**/*.spec.ts', watched: false }
    ],

    preprocessors: {
      // add webpack as preprocessor
      "**/*.ts": ['webpack', 'sourcemap']
    },

    browsers: ['Chrome'],
    reporters: ['progress'],
    port: 9876,

    webpack: {
      externals: [
        /^@example\/*/,
      ],
      module: {
        rules: [
          {
            test: /\.ts$/,
            loader: 'ts-loader',
          },
        ],
      },
    }
  });
}

And my Webpack conf:

const singleSpaAngularWebpack = require('single-spa-angular/lib/webpack').default
    const path = require('path');
    
    module.exports = (angularWebpackConfig, options) => {
      const singleSpaWebpackConfig = singleSpaAngularWebpack(angularWebpackConfig, options);
        
      singleSpaWebpackConfig.externals = [
        /^@example\/*/,
      ];
    
      singleSpaWebpackConfig.condig.resolve.extensions = ['', '.ts', '.js']

      singleSpaWebpackConfig.module.rules = [
          { test: /\.ts$/, use: ['angular2-template-loader', 'ts-loader'], exclude: './node_modules' },
          { test: /\.js$/, use: 'babel-loader', exclude: './node_modules' }
      ];
}

I also tried with the custom rules I had before and got the same error.

Thank you in advance!

Edit:

Here's the package.json:

"dependencies": {
    "@angular/common": "~12.1.3",
    "@angular/compiler": "~12.1.3",
    "@angular/core": "~12.1.3",
    "@angular/forms": "~12.1.3",
    "@angular/platform-browser": "~12.1.3",
    "@angular/platform-browser-dynamic": "~12.1.3",
    "@angular/router": "~12.1.3",
    "angular-i18next": "^10.3.0",
    "core-js": "^3.6.4",
    "i18next": "^20.3.5",
    "rxjs": "^6.6.7",
    "single-spa": "~5.9.3",
    "single-spa-angular": "~5.0.2",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-builders/custom-webpack": "12",
    "@angular-devkit/build-angular": "~12.1.3",
    "@angular/cli": "~12.1.3",
    "@angular/compiler-cli": "~12.1.3",
    "@angular/language-service": "~12.1.3",
    "@babel/core": "^7.6.4",
    "@babel/preset-env": "^7.6.3",
    "@types/jasmine": "^3.6.0",
    "@types/jasminewd2": "^2.0.3",
    "@types/node": "^16.4.5",
    "angular2-template-loader": "^0.6.2",
    "autoprefixer": "^9.6.5",
    "babel-core": "^6.26.3",
    "babel-loader": "^8.0.6",
    "enhanced-resolve": "^4.1.1",
    "jasmine-core": "~3.8.0",
    "jasmine-spec-reporter": "^7.0.0",
    "karma": "~6.3.4",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.7.0",
    "karma-source-map-support": "^1.4.0",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-webpack": "^5",
    "ts-loader": "^9.2.5",
    "ts-node": "^10.2.1",
    "tslib": "^2.0.0",
    "tslint": "~6.1.0",
    "typescript": "4.3.5",
    "webpack": "^5",
    "webpack-cli": "^4.7.2",
    "webpack-dev-server": "^3.11.2"
  }

.babelrc:

{
  "presets": [
    ["@babel/preset-env", {
      "targets": {
        "browsers": ["last 2 versions"]
      }
    }]
  ],
  "plugins": [
    "@babel/syntax-dynamic-import"
  ]
}

Update: I changed the test framework to Jest as I couldn't find the fix for this.

Oriol_IL
  • 120
  • 1
  • 14
  • You may want to to check this question, which looks similar to me: https://stackoverflow.com/questions/35990003/you-may-need-an-appropriate-loader-for-this-file-type-webpack-cant-parse-ang – gerstams Aug 17 '21 at 13:13
  • I added the resolve.extensions array and babel-loader for JS. Still get the same error. Will edit the question. – Oriol_IL Aug 18 '21 at 06:46
  • What version of `ts-loader` have you got installed? Could you share your `package.json` as well please? – VVS Aug 23 '21 at 14:28
  • Added the package.json :) – Oriol_IL Aug 24 '21 at 06:24
  • Could you please update the question with the `.babelrc` content? – Milan Tenk Aug 24 '21 at 07:19
  • 1
    .babelrc added :) – Oriol_IL Aug 24 '21 at 08:14
  • I noticed your using 2 loaders, can you try using only `ts-loader` ? as I think it might be breaking on the `angular2-template-loader`, since it was last updated 5 years ago. – VVS Aug 24 '21 at 08:38
  • Oops, I had it on the earlier versions of the app and didn't notice it wasn't updated since so long ago. I changed it but i get the same error when reading the "const". – Oriol_IL Aug 24 '21 at 08:45

2 Answers2

0

ts-loader 6.x.x is not compatible with webpack 5.x.x. Based on the documentation ts-loader 9.x.x needs to be used with webpack 5.x.x.

Please try to update the ts-loader to the latest version. It might not solve the problem fully, but lets give it a try.

Milan Tenk
  • 2,415
  • 1
  • 17
  • 24
  • 1
    Thanks for answering. I upgraded ts-loader to 9.2.5 but i get the same error. I'll update the package.json on the question. – Oriol_IL Aug 24 '21 at 08:18
0

From your error, your issue is definitely with the ts files, as it states "error in ./src/test.ts"

Please update ts-loader to version 9, as github Compatibility documentation for ts-loader states:

TypeScript: 3.6.3+

webpack: 5.x+ (please use ts-loader 8.x if you need webpack 4 support)

node: 12.x+

Also further Research states the following:

The minimum webpack version supported is now webpack 5. This simplifies the codebase, which previously had to if/else the various API registrations based on the version of webpack being used. The minimum node version supported is now node 12

According to webpack documentation that states:

Webpack 5 requires at least Node.js 10.13.0 (LTS)

Upgrade webpack-cli to the latest available version (when used)

So to be clear:

ts-loader - to be updated to version 9 minimum.

node - to be updated to version 12 minimum.

I notice your using 2 loaders for ts, I'm not sure whether the angular2-template-loader is supported for webpack 5, as it was last published 5 years ago according to the angular2-template-loader documentation.

From looking at webpack Integration documentation, specifically for integration using karma it says you can add webpack configuration within karma.config. So it leads me to believe that it might be struggling to read your webpack.config. could you try updating your karma.config specifically the webpack key to the below:


 webpack: {
      resolve: {
        modules: ['node_modules']
      },
      module: {
        rules: [
          {
            test: /\.ts$/,
            loader: 'ts-loader',
          },
        ],
      },
    }

I've added the resolve - modules to the webpack key as it:

Tells webpack what directories should be searched when resolving modules.

VVS
  • 293
  • 2
  • 15
  • Thanks for answering. I upgraded ts-loader to 9.2.5 but i get the same error. I'll update the package.json on the question. – Oriol_IL Aug 24 '21 at 08:18
  • It changes the results but now gives "ERROR in ./src/*whatever*.spec.ts 3:0-54 Module not found: Error: Can't resolve *whatever* in ./home/user/*whatever*" for every import in every .spec.ts (or so it seems). I'll update the post. Thank you! – Oriol_IL Aug 24 '21 at 09:49
  • Can you update the `webpack` key i've added the resolve to look through `node_modules` – VVS Aug 24 '21 at 10:12
  • Thanks! It still gives the same error. Maybe I explained myself wrongly. The error it gives about not finding X module is about my own components, modules and services not the ones imported from node_modules. – Oriol_IL Aug 24 '21 at 10:19
  • Is there a github or stackblitz example I could possibly see please? – VVS Aug 24 '21 at 11:58
  • I'll prepare a github example if i have time tomorrow. Thanks for your help! – Oriol_IL Aug 24 '21 at 12:30
  • Here's the example giving the same error: https://github.com/OriolInvernonLlaneza/karma-webpack-error-example – Oriol_IL Aug 25 '21 at 12:02
  • I've got a little bit further with this, seems to be something in the `karma.conf` as when i add `'src/app/app.component.ts',` to the `files` key that error goes and your `external.service` becomes an error. I tried the pattern of `ts` files but it didn't find all the necessary modules. I Also played around with the `alias` key within `module resolve` to see if that helped but I think theres something else that I might be missing. – VVS Aug 26 '21 at 13:36
  • Thanks for your time. I knew it had to be something with karma or the webpack config caused by the upgrade of Angular and Webpack. This is something that everytime that I'm forced to change it I need to seek help because I don't really grasp how they work and how should I configurate them. – Oriol_IL Aug 30 '21 at 08:00