Home > Net >  Karma Jasmine - Uncaught TypeError: __webpack_require__(...).context is not a function
Karma Jasmine - Uncaught TypeError: __webpack_require__(...).context is not a function

Time:12-02

I was trying to update my whole project dependencies, and managed to do it, but when I try to run the tests with ng test --no-watch they don't work, giving me the following error.

23 11 2022 12:33:58.268:INFO [Chrome Headless 107.0.5304.107 (Windows 10)]: Connected on socket JpJqOsJswlYxFGjeAAAB with id 12720855
Chrome Headless 107.0.5304.107 (Windows 10) ERROR
  An error was thrown in afterAll
  Uncaught TypeError: __webpack_require__(...).context is not a function
  TypeError: __webpack_require__(...).context is not a function
      at Object.4289 (http://localhost:9876/_karma_webpack_/webpack:/src/test.ts:18:25)
      at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
      at __webpack_exec__ (http://localhost:9876/_karma_webpack_/main.js:10483:48)
      at http://localhost:9876/_karma_webpack_/main.js:10484:54
      at Function.__webpack_require__.O (http://localhost:9876/_karma_webpack_/webpack:/webpack/runtime/chunk loaded:23:1)
      at http://localhost:9876/_karma_webpack_/main.js:10485:56
      at webpackJsonpCallback (http://localhost:9876/_karma_webpack_/webpack:/webpack/runtime/jsonp chunk loading:71:1)
      at http://localhost:9876/_karma_webpack_/main.js:1:87
Chrome Headless 107.0.5304.107 (Windows 10): Executed 0 of 0 ERROR (0.106 secs / 0 secs)

It's interesting how it worked perfectly before doing the update, and suddenly it seems like webpack is unable to find the context? Why?

The dependencies I changed were:

Old dependencies:

"dependencies": {
    "@angular/animations": "13.3.6",
    "@angular/cdk": "13.3.6",
    "@angular/common": "13.3.6",
    "@angular/compiler": "13.3.6",
    "@angular/core": "13.3.6",
    "@angular/forms": "13.3.6",
    "@angular/localize": "13.3.6",
    "@angular/platform-browser": "13.3.6",
    "@angular/platform-browser-dynamic": "13.3.6",
    "@angular/router": "13.3.6",
    "@auth0/angular-jwt": "5.0.2",
    "@fortawesome/angular-fontawesome": "0.10.2",
    "@fortawesome/fontawesome-svg-core": "6.1.1",
    "@fortawesome/free-regular-svg-icons": "6.1.1",
    "@fortawesome/free-solid-svg-icons": "6.1.1",
    "@ngrx/effects": "13.2.0",
    "@ngrx/router-store": "13.2.0",
    "@ngrx/store": "13.2.0",
    "@ngrx/store-devtools": "13.2.0",
    "@ngx-translate/core": "14.0.0",
    "bootstrap": "5.1.3",
    "core-js": "3.22.4",
    "ngx-bootstrap": "8.0.0",
    "@popperjs/core": "2.11.5",
    "primeng": "13.3.3",
    "rxjs": "7.5.5",
    "svg-to-pdfkit": "0.1.8",
    "tslib": "2.4.0",
    "ws-blueprint-api": "file:./build/stubs/domain-a-api",
    "zone.js": "0.11.5"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "13.3.5",
    "@angular-eslint/builder": "13.2.1",
    "@angular-eslint/eslint-plugin": "13.2.1",
    "@angular-eslint/eslint-plugin-template": "13.2.1",
    "@angular-eslint/schematics": "13.2.1",
    "@angular-eslint/template-parser": "13.2.1",
    "@angular/cli": "13.3.5",
    "@angular/compiler-cli": "13.3.6",
    "@angular/language-service": "13.3.6",
    "@ngrx/schematics": "13.2.0",
    "@types/jasmine": "4.0.3",
    "@types/jasminewd2": "2.0.10",
    "@types/node": "17.0.31",
    "@typescript-eslint/eslint-plugin": "5.22.0",
    "@typescript-eslint/parser": "5.22.0",
    "cypress": "9.6.0",
    "cypress-multi-reporters": "1.6.0",
    "cypress-sonarqube-reporter": "1.10.0",
    "eslint": "8.15.0",
    "eslint-formatter-gitlab": "3.0.0",
    "eslint-plugin-import": "2.25.4",
    "eslint-plugin-jsdoc": "38.0.6",
    "eslint-plugin-prefer-arrow": "1.2.3",
    "jasmine-core": "4.1.0",
    "jasmine-spec-reporter": "7.0.0",
    "karma": "6.3.19",
    "karma-chrome-launcher": "3.1.1",
    "karma-coverage": "2.2.0",
    "karma-jasmine": "5.0.0",
    "karma-junit-reporter": "2.0.1",
    "karma-sonarqube-unit-reporter": "0.0.23",
    "mocha-junit-reporter": "2.0.2",
    "ngrx-store-freeze": "0.2.4",
    "ngx-translate-testing": "6.0.1",
    "prettier": "2.6.2",
    "start-server-and-test": "1.14.0",
    "ts-node": "10.7.0",
    "typescript": "4.6.4",
    "webpack-bundle-analyzer": "4.5.0"
  },

New dependencies:

"dependencies": {
    "@angular/animations": "15.0.0",
    "@angular/cdk": "15.0.0",
    "@angular/common": "15.0.0",
    "@angular/compiler": "15.0.0",
    "@angular/core": "15.0.0",
    "@angular/forms": "15.0.0",
    "@angular/localize": "15.0.0",
    "@angular/platform-browser": "15.0.0",
    "@angular/platform-browser-dynamic": "15.0.0",
    "@angular/router": "15.0.0",
    "@auth0/angular-jwt": "5.1.0",
    "@fortawesome/angular-fontawesome": "0.12.0",
    "@fortawesome/fontawesome-svg-core": "6.2.1",
    "@fortawesome/free-regular-svg-icons": "6.2.1",
    "@fortawesome/free-solid-svg-icons": "6.2.1",
    "@ngrx/effects": "14.3.2",
    "@ngrx/router-store": "14.3.2",
    "@ngrx/store": "14.3.2",
    "@ngrx/store-devtools": "14.3.2",
    "@ngx-translate/core": "14.0.0",
    "bootstrap": "5.2.2",
    "core-js": "3.26.1",
    "ngx-bootstrap": "9.0.0",
    "@popperjs/core": "2.11.6",
    "primeng": "14.2.2",
    "rxjs": "7.5.7",
    "svg-to-pdfkit": "0.1.8",
    "tslib": "2.4.1",
    "ws-blueprint-api": "file:./build/stubs/domain-a-api",
    "zone.js": "0.12.0"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "15.0.0",
    "@angular-eslint/builder": "15.0.0",
    "@angular-eslint/eslint-plugin": "15.0.0",
    "@angular-eslint/eslint-plugin-template": "15.0.0",
    "@angular-eslint/schematics": "15.0.0",
    "@angular-eslint/template-parser": "15.0.0",
    "@angular/cli": "15.0.0",
    "@angular/compiler-cli": "15.0.0",
    "@angular/language-service": "15.0.0",
    "@ngrx/schematics": "14.3.2",
    "@types/jasmine": "4.3.0",
    "@types/jasminewd2": "2.0.10",
    "@types/node": "18.11.9",
    "@typescript-eslint/eslint-plugin": "5.43.0",
    "@typescript-eslint/parser": "5.43.0",
    "cypress": "11.1.0",
    "cypress-multi-reporters": "1.6.1",
    "cypress-sonarqube-reporter": "1.11.0",
    "eslint": "8.28.0",
    "eslint-formatter-gitlab": "4.0.0",
    "eslint-plugin-import": "2.26.0",
    "eslint-plugin-jsdoc": "39.6.2",
    "eslint-plugin-prefer-arrow": "1.2.3",
    "jasmine-core": "4.5.0",
    "jasmine-spec-reporter": "7.0.0",
    "karma": "6.4.1",
    "karma-chrome-launcher": "3.1.1",
    "karma-coverage": "2.2.0",
    "karma-jasmine": "5.1.0",
    "karma-junit-reporter": "2.0.1",
    "karma-sonarqube-unit-reporter": "0.0.23",
    "mocha-junit-reporter": "2.2.0",
    "ngrx-store-freeze": "0.2.4",
    "ngx-translate-testing": "6.1.0",
    "prettier": "2.7.1",
    "start-server-and-test": "1.14.0",
    "ts-node": "10.9.1",
    "typescript": "4.8.4",
    "webpack-bundle-analyzer": "4.7.0"
  },

Basically changed most of them to the latest. I even tried to revert and try to update one by one, but as soon as I update angular it breaks, why is so?

Thanks in advance

CodePudding user response:

I had the same issue. For some reason, in our other app doing angular update automatically deleted some code in test.ts, namely:

   const context = require.context('./', true, /\.spec\.ts$/);
   context.keys().forEach(context);

and the declaration of require above that.

I did that in my other app manually and so far it works. Maybe someone knows the reason for this change.

CodePudding user response:

I had the same issue with a components library after an upgrade from v14 to v15.

Unfortunately pasting the code back from git history did not solve the issue with 0 found test for me.

My solution was to delete the test.ts file in projects/components/src and its reference in the files list in tsconfig.spec.ts. Additionally a patch to the test block in angular.json was needed. Alongside with a extended include pattern list in the tsconfig.spec.ts

angular.json before

{
  "builder": "@angular-devkit/build-angular:karma",
  "options": {
    "main": "projects/components/src/test.ts",
    "tsConfig": "projects/components/tsconfig.spec.json",
    "karmaConfig": "projects/components/karma.conf.js"
  }
}

angular.json after

{
  "builder": "@angular-devkit/build-angular:karma",
  "options": {
    "polyfills": [
      "zone.js",
      "zone.js/testing"
    ],
    "tsConfig": "projects/components/tsconfig.spec.json",
    "karmaConfig": "projects/components/karma.conf.js"
  }
}

tsconfig.spec.ts before

{
    "files": [
        "src/test.ts"
    ],
    "include": [
        "**/*.spec.ts",
        "**/*.d.ts"
    ]
}

tsconfig.spec.ts after

{
    "files": [
    ],
    "include": [
        "**/*.spec.ts",
        "**/*.d.ts",
        "**/*.ts"
    ]
}
  • Related