After the Update from Angular 14 to 15 my Tests won't run anymore. When I run ng test I get the following 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 Module.4289 (http://localhost:9876/_karma_webpack_/webpack:/src/test.ts:22:30)
at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
at __webpack_exec__ (http://localhost:9876/_karma_webpack_/main.js:180646:48)
at http://localhost:9876/_karma_webpack_/main.js:180647:54
at Function.__webpack_require__.O
My test.ts file looks like this and is set in the angular.json as "main".
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import "zone.js/testing";
import { getTestBed } from "@angular/core/testing";
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from "@angular/platform-browser-dynamic/testing";
declare const require: any;
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
// Then we find all the tests.
const context: any = require.context("./", true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
I tried removing the file but this does not seem to solve the Problem. After that I got Error in Third Party Libraries (dhtmlx gantt) that don't come up when running the app with ng build. ng build runs fine, the app works - only the tests are failing.
CodePudding user response:
The upgrade tool removes the context lines because they are not necessary anymore, try this:
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import "zone.js/testing";
import { getTestBed } from "@angular/core/testing";
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from "@angular/platform-browser-dynamic/testing";
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());