我正在从Angular 8.0升级到Angular 9.1.1。只需一点点工作,一切就可以构建并运行良好。 我遇到了本地化问题https://angular.io/guide/migration-localize,并按照以下说明进行操作-这使我的项目得以构建和运行! 但是,我在运行测试(karma / jasmine)时仍然遇到此问题
我使用CLI升级了我的项目。我似乎找不到其他任何必要的步骤来迁移本地化。
我正在使用TestBed进行单元测试,并且在调用TestBed.createComponent
时失败,并抛出错误:
Error: It looks like your application or one of its dependencies is using i18n.
Angular 9 introduced a global `$localize()` function that needs to be loaded.
Please run `ng add @angular/localize` from the Angular CLI.
(For non-CLI projects, add `import '@angular/localize/init';` to your `polyfills.ts` file.
For server-side rendering applications add the import to your `main.server.ts` file.)
一个失败的测试组件模板中的HTML示例是:
<span i18n>
label
</span>
只需删除i18n
即可运行并通过测试-但这不是应用程序运行时的运行时错误。
关于下一步寻找什么或这里可能涉及什么的任何线索?
我的polypill.js
/***************************************************************************************************
* Load `$localize` onto the global scope - used if i18n tags appear in Angular templates.
*/
import '@angular/localize/init';
/**
* This file includes polyfills needed by Angular and is loaded before the app.
* You can add your own extra polyfills to this file.
*
...
我的package.json
依赖项
"dependencies": {
"@angular/animations": "^9.1.1",
"@angular/common": "^9.1.1",
"@angular/compiler": "^9.1.1",
"@angular/core": "^9.1.1",
"@angular/forms": "^9.1.1",
"@angular/localize": "^9.1.1",
"@angular/platform-browser": "^9.1.1",
"@angular/platform-browser-dynamic": "^9.1.1",
"@angular/router": "^9.1.1",
"core-js": "^3.0.0",
"moment": "^2.24.0",
"prismjs": "^1.16.0",
"rxjs": "~6.5.5",
"tslib": "^1.10.0",
"zone.js": "~0.10.2"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.901.1",
"@angular-devkit/build-ng-packagr": "~0.901.1",
"@angular/cli": "^9.1.1",
"@angular/compiler-cli": "^9.1.1",
"@angular/language-service": "^9.1.1",
"@types/jasmine": "^3.3.13",
"@types/jasminewd2": "^2.0.6",
"@types/node": "^12.11.1",
"codelyzer": "^5.1.2",
"jasmine-core": "~3.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "^4.2.0",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage-istanbul-reporter": "^2.1.0",
"karma-jasmine": "^2.0.0",
"karma-jasmine-html-reporter": "^1.4.2",
"ng-packagr": "^9.0.0",
"ts-node": "^8.2.0",
"tslint": "^5.18.0",
"typescript": "~3.8.3"
}
答案 0 :(得分:2)
我发现必须将@angular/localize/init
导入test.ts
才能运行测试,有关示例,请参见here。
答案 1 :(得分:2)
测试中未包含polyfill,因此我不得不在angular.json
中添加polyfil配置选项
"builder": "@angular-devkit/build-angular:karma",
"options": {
"polyfills": "polyfills.ts",
...
}
},
并确保它已包含在tsconfig.spec.json
"include": [
"**/*.spec.ts",
"**/*.d.ts",
"polyfills.ts"
]
答案 2 :(得分:1)
在将大型Nx / Angular CLI应用程序从Angular 8升级到Angular 10之后,我在单元测试中收到错误。
An error was thrown in afterAll
Uncaught Error: It looks like your application or one of its dependencies is using i18n.
Angular 9 introduced a global `$localize()` function that needs to be loaded.
Please run `ng add @angular/localize` from the Angular CLI.
(For non-CLI projects, add `import '@angular/localize/init';` to your `polyfills.ts` file
错误描述中的解决方案不能解决测试失败的问题。通过Angular CLI生成的应用程序中的库没有任何polyfill.ts文件。所有这些库都有一个由Angular CLI生成的文件test.ts
(例如mproject \ libs \ mylib \ src \ test.ts)。我在这些库的import '@angular/localize/init';
文件中添加了导入语句(test.ts
),以解决此问题。