NativeScript Angular + Karma - ReferenceError: window is not



所以我决定在我的 NS7/NG11 应用程序中添加单元测试,按照 NS 网站上的说明进行操作,并到达应用程序将启动但屏幕空白(全白)的地步。

使用ns test init初始化测试,手动更新karma-webpackpackage.json到 v5.0.0-alpha.5作为默认测试有一个已修复的错误,应该很快就会发布。

用于开始测试的命令是:ns test run --emulator

使用角度在模板Tabs进行测试。当运行ns run ios --emulator运行时,运行良好且可用。

以下是我package.json

{
"name": "@nativescript/template-tab-navigation-ng",
"main": "main.js",
"displayName": "Tabs",
"templateType": "App template",
"version": "7.0.6",
"description": "NativeScript Application",
"author": "NativeScript Team <oss@nativescript.org>",
"license": "SEE LICENSE IN <your-license-filename>",
"publishConfig": {
"access": "public"
},
"keywords": [
"nstudio",
"nativescript",
"mobile",
"angular",
"{N}",
"tns",
"template",
"tab",
"navigation",
"category-general"
],
"repository": "<fill-your-repository-here>",
"homepage": "https://github.com/NativeScript/nativescript-app-templates",
"bugs": {
"url": "https://github.com/NativeScript/NativeScript/issues"
},
"scripts": {
"lint": "tslint "src/**/*.ts""
},
"dependencies": {
"@angular/animations": "~11.0.0",
"@angular/common": "~11.0.0",
"@angular/compiler": "~11.0.0",
"@angular/core": "~11.0.0",
"@angular/forms": "~11.0.0",
"@angular/platform-browser": "~11.0.0",
"@angular/platform-browser-dynamic": "~11.0.0",
"@angular/router": "~11.0.0",
"@nativescript/angular": "~11.0.0",
"@nativescript/core": "~7.1.0",
"@nativescript/theme": "~2.3.0",
"@nativescript/unit-test-runner": "^1.0.2",
"reflect-metadata": "~0.1.12",
"rxjs": "^6.6.0",
"zone.js": "~0.11.1"
},
"devDependencies": {
"@angular/compiler-cli": "~11.0.0",
"@nativescript/ios": "7.1.0",
"@nativescript/webpack": "~4.0.0",
"@ngtools/webpack": "~11.0.0",
"@types/jasmine": "3.6.2",
"codelyzer": "~6.0.0",
"karma": "6.0.0",
"karma-jasmine": "4.0.1",
"karma-nativescript-launcher": "0.4.0",
"karma-webpack": "5.0.0-alpha.5",
"node-sass": "^4.14.1",
"tslint": "~6.1.3",
"typescript": "~4.0.0"
},
"private": "true",
"readme": "NativeScript Application"
}

这是我运行ns test ios --emulator时的控制台输出:

Webpack compilation complete. Watching for file changes.
Webpack build done!
Updating runtime package.json with configuration values...
Project successfully prepared (ios)
Successfully transferred all files on device DF60258A-B86C-4E93-BCEF-4E2ED154E007.
Restarting application on device DF60258A-B86C-4E93-BCEF-4E2ED154E007...
Successfully synced application org.nativescript.karmans7 on device DF60258A-B86C-4E93-BCEF-4E2ED154E007.
(RunningBoardServices) [com.apple.runningboard:connection] Identity resolved as application<org.nativescript.karmans7>
CONSOLE LOG: NSUTR: fetching http://127.0.0.1:9877/context.json
CONSOLE LOG: NSUTR: fetching http://172.16.1.6:9877/context.json
CONSOLE LOG: NSUTR: fetching http://172.16.1.4:9877/context.json
CONSOLE LOG: NSUTR: found karma at 127.0.0.1
CONSOLE LOG: NSUTR: found karma at 172.16.1.4
CONSOLE LOG: NSUTR: found karma at 172.16.1.6
CONSOLE LOG: NSUTR: connecting to karma at http://127.0.0.1:9877
CONSOLE LOG: ReferenceError: window is not defined

这是NativeScript和Karma的问题。他们建议使用 与此同时"karma": "5.2.3",

这也为我修复了它。

NativeScript 不像浏览器那样有window对象。 所以任何需要假装它在浏览器中运行的代码,基本上都需要添加一个全局窗口对象。

在 app.ts(或 main.ts)的最顶部,具体取决于 NS 风格;添加以下行:

global.window = {};global.window = global(如果你使用的是 TS,你将不得不做(<any>global).否则 TypeScript 可能会抱怨全局类型不正确......

这应该可以解决问题。

最新更新