him*_*tim 20 testing karma-jasmine angular angular15
从 Angular 14 更新到 15 后,我的测试将不再运行。当我运行 ng test 时,出现以下错误:
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
Run Code Online (Sandbox Code Playgroud)
我的 test.ts 文件如下所示,并在 angular.json 中设置为“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);
Run Code Online (Sandbox Code Playgroud)
我尝试删除该文件,但这似乎并不能解决问题。之后,我在第三方库(dhtmlx甘特图)中遇到错误,当使用 ng build 运行应用程序时,不会出现该错误。ng build 运行良好,应用程序可以运行 - 只有测试失败。
Osc*_*bcn 41
升级工具删除了上下文行,因为它们不再需要,请尝试以下操作:
// 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());
Run Code Online (Sandbox Code Playgroud)
Ale*_*sov 11
尝试运行
ng update @angular/cli --from 14 --to 15 --migrate-only
Run Code Online (Sandbox Code Playgroud)
应该更新test.ts文件并删除下面的 require.context 行
const context = require.context('./', true, /\.spec\.ts$/);
context.keys().forEach(context);
Run Code Online (Sandbox Code Playgroud)
之后,您可以使用--include param对单个或多个文件执行测试
ng test --include=**/some-file.spec.ts
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10006 次 |
最近记录: |