Reputation: 304
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.
Upvotes: 23
Views: 14640
Reputation: 2993
Try to run
ng update @angular/cli --from 14 --to 15 --migrate-only
which should update test.ts file and remove require.context lines below
const context = require.context('./', true, /\.spec\.ts$/);
context.keys().forEach(context);
after that you can execute testing for a single or multiple files using --include param
ng test --include=**/some-file.spec.ts
Upvotes: 14
Reputation: 688
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());
Upvotes: 47