nativescript-cli icon indicating copy to clipboard operation
nativescript-cli copied to clipboard

Unable to run tests - command tns test android showing error: ReferenceError: window is not defined

Open itsmerockingagain opened this issue 4 years ago • 2 comments

Environment

  • CLI: 6.8.0
  • Cross-platform modules:6.3.2
  • Android Runtime:6.5.3
  • iOS Runtime:6.5.4
  • XCode Version:12.3
  • Plugin(s): { "dependencies": { "@angular/animations": "~8.2.0", "@angular/common": "~8.2.0", "@angular/compiler": "~8.2.0", "@angular/core": "~8.2.0", "@angular/forms": "~8.2.0", "@angular/http": "~8.0.0-beta.10", "@angular/platform-browser": "~8.2.0", "@angular/platform-browser-dynamic": "~8.2.0", "@angular/router": "~8.2.0", "@nativescript/theme": "~2.2.1", "@ngrx/effects": "^10.1.2", "@ngrx/entity": "^10.1.2", "@ngrx/store": "^10.1.2", "@ngrx/store-devtools": "^10.1.2", "@nstudio/nativescript-checkbox": "^1.0.0", "@nstudio/nativescript-loading-indicator": "^3.0.4", "core-js": "^2.6.9", "nativescript-angular": "~8.20.0", "nativescript-drop-down": "^5.0.6", "nativescript-pdf-view": "^2.4.3", "nativescript-plugin-firebase": "^10.6.3", "nativescript-ui-sidedrawer": "^8.0.1", "reflect-metadata": "~0.1.12", "rxjs": "~6.4.0", "tns-core-modules": "~6.3.0", "tns-platform-declarations": "^6.5.15", "tslib": "1.10.0", "zone.js": "~0.9.1" }, "devDependencies": { "@angular-devkit/build-angular": "~0.803.0", "@angular/cli": "~8.3.0", "@angular/compiler-cli": "~8.2.0", "@nativescript/schematics": "~1.0.0", "@nativescript/tslint-rules": "~0.0.5", "@ngrx/schematics": "^10.1.2", "@types/jasmine": "3.5.11", "@types/jasminewd2": "~2.0.3", "@types/node": "~8.9.4", "codelyzer": "^5.0.0", "copy-webpack-plugin": "5.1.1", "jasmine-core": "~3.4.0", "jasmine-spec-reporter": "~4.2.1", "karma": "6.1.0", "karma-chrome-launcher": "^2.2.0", "karma-coverage": "^2.0.3", "karma-coverage-istanbul-reporter": "^2.0.6", "karma-jasmine": "3.3.1", "karma-jasmine-html-reporter": "^1.4.0", "karma-junit-reporter": "^2.0.1", "karma-nativescript-launcher": "0.4.0", "karma-remap-istanbul": "^0.6.0", "karma-sonarqube-unit-reporter": "0.0.23", "karma-spec-reporter": "0.0.32", "karma-typescript": "^5.2.0", "karma-webpack": "3.0.5", "nativescript-dev-webpack": "^1.4.1", "nativescript-unit-test-runner": "^0.7.0", "node-sass": "^4.7.1", "protractor": "~5.4.0", "tns-android": "6.5.3", "tns-ios": "6.5.4", "ts-node": "~7.0.0", "tslint": "~5.15.0", "typescript": "~3.5.3" } }

Karma config

module.exports = function (config) { const options = {

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],


// list of files / patterns to load in the browser
files: ['src/tests/**/*.ts'],


// list of files to exclude
exclude: [
],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: [],

customLaunchers: {
  android: {
    base: 'NS',
    platform: 'android'
  },
  ios: {
    base: 'NS',
    platform: 'ios'
  },
  ios_simulator: {
    base: 'NS',
    platform: 'ios',
    arguments: ['--emulator']
  }
},

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false

};

setWebpackPreprocessor(config, options); setWebpack(config, options);

config.set(options); }

function setWebpackPreprocessor(config, options) { if (config && config.bundle) { if (!options.preprocessors) { options.preprocessors = {}; }

options.files.forEach(file => {
  if (!options.preprocessors[file]) {
    options.preprocessors[file] = [];
  }
  options.preprocessors[file].push('webpack');
});

} }

function setWebpack(config, options) { if (config && config.bundle) { const env = {}; env[config.platform] = true; env.sourceMap = config.debugBrk; env.appPath = config.appPath; options.webpack = require('./webpack.config')(env); delete options.webpack.entry; delete options.webpack.output.libraryTarget; const invalidPluginsForUnitTesting = ["GenerateBundleStarterPlugin", "GenerateNativeScriptEntryPointsPlugin"]; options.webpack.plugins = options.webpack.plugins.filter(p => !invalidPluginsForUnitTesting.includes(p.constructor.name)); } }

Describe the bug I am trying to run the unit tests by running command tns test android , command is running and karma server is opening on chrome, but i am getting error : command : tns test android Searching for devices... File change detected. Starting incremental webpack compilation... (node:4100) DeprecationWarning: Tapable.plugin is deprecated. Use new API on .hooks instead ℹ 「wdm」: wait until bundle finished: noop ℹ 「wdm」: wait until bundle finished: noop ℹ 「wdm」: wait until bundle finished: noop File change detected. Starting incremental webpack compilation... Webpack compilation complete. Watching for file changes. Webpack compilation complete. Watching for file changes. ℹ 「wdm」: Hash: d8a04dc7e971180049dc Version: webpack 4.39.2 Time: 16068ms Built at: 02/06/2021 09:03:32 Asset Size Chunks Chunk Names runtime.js 5.96 KiB runtime [emitted] runtime src/tests/example.bundle.js 501 bytes src/tests/example [emitted] src/tests/example src/tests/example.tns.bundle.js 513 bytes src/tests/example.tns [emitted] src/tests/example.tns src/tests/setup.bundle.js 2.08 KiB src/tests/setup [emitted] src/tests/setup vendor.bundle.js 6.44 MiB vendor [emitted] vendor Entrypoint src/tests/example.tns = runtime.js src/tests/example.tns.bundle.js Entrypoint src/tests/setup = runtime.js vendor.bundle.js src/tests/setup.bundle.js Entrypoint src/tests/example = runtime.js src/tests/example.bundle.js [../$$_lazy_route_resource lazy recursive] ./$$_lazy_route_resource lazy namespace object 160 bytes {src/tests/setup} [built] [../node_modules/@angular/common/fesm5/common.js] ./node_modules/@angular/common/fesm5/common.js 258 KiB {vendor} [built] [../node_modules/@angular/core/fesm5/core.js] ./node_modules/@angular/core/fesm5/core.js 1.18 MiB {vendor} [built] [../node_modules/@angular/core/fesm5/testing.js] ./node_modules/@angular/core/fesm5/testing.js 111 KiB {vendor} [built] [../node_modules/@nativescript/angular/platform-common.js] ./node_modules/@nativescript/angular/platform-common.js 13.8 KiB {vendor} [built] [../node_modules/@nativescript/angular/platform-providers.js] ./node_modules/@nativescript/angular/platform-providers.js 2.29 KiB {vendor} [built] [../node_modules/@nativescript/angular/platform.js] ./node_modules/@nativescript/angular/platform.js 2.25 KiB {vendor} [built] [../node_modules/@nativescript/angular/testing/index.js] ./node_modules/@nativescript/angular/testing/index.js 1.68 KiB {vendor} [built] [../node_modules/@nativescript/angular/testing/src/nativescript_test_component_renderer.js] ./node_modules/@nativescript/angular/testing/src/nativescript_test_component_renderer.js 1.21 KiB {vendor} [built] [../node_modules/@nativescript/angular/testing/src/util.js] ./node_modules/@nativescript/angular/testing/src/util.js 7.63 KiB {vendor} [built] [../node_modules/@nativescript/angular/zone-js/dist/zone-nativescript.jasmine.js] ./node_modules/@nativescript/angular/zone-js/dist/zone-nativescript.jasmine.js 62.8 KiB {vendor} [built] [../node_modules/@nativescript/angular/zone-js/testing.jasmine.js] ./node_modules/@nativescript/angular/zone-js/testing.jasmine.js 228 bytes {vendor} [built] [./tests/example.tns.ts] ./src/tests/example.tns.ts 299 bytes {src/tests/example.tns} [built] [./tests/example.ts] ./src/tests/example.ts 299 bytes {src/tests/example} [built] [./tests/setup.ts] ./src/tests/setup.ts 271 bytes {src/tests/setup} [built] + 549 hidden modules ℹ 「wdm」: Compiled successfully. 06 02 2021 09:03:33.104:WARN [karma]: No captured browser, open http://localhost:9876/ 06 02 2021 09:03:33.109:INFO [karma-server]: Karma v6.1.0 server started at http://localhost:9876/ 06 02 2021 09:03:33.109:INFO [launcher]: Launching browsers android with concurrency unlimited 06 02 2021 09:03:33.151:INFO [launcher]: Starting browser NativeScript Unit Test Runner Preparing project... File change detected. Starting incremental webpack compilation...

webpack is watching the files…

Hash: f03e0f0582fdad0e5f72 Version: webpack 4.39.2 Time: 8017ms Built at: 02/06/2021 09:03:42 Asset Size Chunks Chunk Names assets/i18n/en.json 1.93 KiB [emitted]
assets/i18n/fr.json 2.21 KiB [emitted]
assets/i18n/pa.json 3.08 KiB [emitted]
assets/scanned.png 414 KiB [emitted]
bundle.js 17.4 KiB bundle [emitted] bundle fonts/Menlo Bold Italic.ttf 288 KiB [emitted]
fonts/Menlo Bold.ttf 434 KiB [emitted]
fonts/Menlo Italic.ttf 308 KiB [emitted]
fonts/Menlo-Regular.ttf 464 KiB [emitted]
fonts/Roboto-Black.ttf 167 KiB [emitted]
fonts/Roboto-Bold.ttf 166 KiB [emitted]
fonts/Roboto-BoldItalic.ttf 170 KiB [emitted]
fonts/Roboto-Medium.ttf 168 KiB [emitted]
fonts/Roboto-MediumItalic.ttf 172 KiB [emitted]
fonts/Roboto-Regular.ttf 167 KiB [emitted]
fonts/Roboto-RegularItalic.ttf 169 KiB [emitted]
fonts/RobotoMono-Medium.ttf 107 KiB [emitted]
fonts/RobotoMono-MediumItalic.ttf 115 KiB [emitted]
fonts/RobotoMono-Regular.ttf 107 KiB [emitted]
fonts/RobotoMono-RegularItalic.ttf 112 KiB [emitted]
fonts/cn-ico-moon.ttf 205 KiB [emitted]
fonts/rawline-200.ttf 254 KiB [emitted]
fonts/rawline-200i.ttf 262 KiB [emitted]
fonts/rawline-300.ttf 262 KiB [emitted]
fonts/rawline-300i.ttf 266 KiB [emitted]
fonts/rawline-400.ttf 257 KiB [emitted]
fonts/rawline-400i.ttf 266 KiB [emitted]
fonts/rawline-600.ttf 263 KiB [emitted]
package.json 112 bytes [emitted]
runtime.js 13.9 KiB runtime [emitted] runtime tns-java-classes.js 0 bytes [emitted]
tslint.json 314 bytes [emitted]
vendor.js 16.1 MiB vendor [emitted] vendor Entrypoint bundle = runtime.js vendor.js bundle.js [../$$lazy_route_resource lazy recursive] ../$$lazy_route_resource lazy namespace object 160 bytes {bundle} [built] [../node_modules/nativescript-unit-test-runner/app sync recursive (?<!\bApp_Resources\b.)(?<!./\btests\b/.?).(xml|css|js|kt|(?<!.d.)ts|(?<!\b[\w-].)scss)$] ../node_modules/nativescript-unit-test-runner/app sync (?<!\bApp_Resources\b.)(?<!./\btests\b/.*?).(xml|css|js|kt|(?<!.d.)ts|(?<!\b[\w-].)scss)$ 632 bytes {bundle} [built] [./ sync recursive tests/..(ts|js)] . sync tests/.*.(ts|js) 231 bytes {bundle} [built] [./main.tns.ts] 1.1 KiB {bundle} [built] [./package.json] 104 bytes {bundle} [optional] [built] [./tests/example.tns.ts] 299 bytes {bundle} [optional] [built] [./tests/example.ts] 299 bytes {bundle} [optional] [built] [./tests/setup.ts] 271 bytes {bundle} [optional] [built] + 587 hidden modules Webpack compilation complete. Watching for file changes. Webpack build done! Project successfully prepared (android) Successfully transferred all files on device emulator-5554. Restarting application on device emulator-5554... 06 02 2021 09:03:46.384:INFO [Chrome 88.0.4324.146 (Mac OS 10.15.7)]: Connected on socket FVDvfUxwpLODbbZuAAAB with id manual-1329 Successfully synced application ca.cn.dma on device emulator-5554. JS: NSUTR: fetching http://127.0.0.1:9876/context.json JS: NSUTR: fetching http://192.168.29.206:9876/context.json JS: NSUTR: found karma at 192.168.29.206 JS: NSUTR: connecting to karma at http://192.168.29.206:9876 JS: ReferenceError: window is not defined Chokidar raised event change for /Users/santosh_singh20/Desktop/External-Driver-App/FE-external-driver-app/App_Resources/Android/AndroidManifest.xml. Building project... Gradle build... + setting applicationId + applying user-defined configuration from /Users/santosh_singh20/Desktop/External-Driver-App/FE-external-driver-app/App_Resources/Android/app.gradle + using android X library androidx.legacy:legacy-support-v4:1.0.0 + adding nativescript runtime package dependency: nativescript-optimized-with-inspector + adding aar plugin dependency: /Users/santosh_singh20/Desktop/External-Driver-App/FE-external-driver-app/node_modules/@nstudio/nativescript-loading-indicator/platforms/android/nativescript_loading_indicator.aar + adding aar plugin dependency: /Users/santosh_singh20/Desktop/External-Driver-App/FE-external-driver-app/node_modules/nativescript-plugin-firebase/platforms/android/nativescript_plugin_firebase.aar + adding aar plugin dependency: /Users/santosh_singh20/Desktop/External-Driver-App/FE-external-driver-app/node_modules/nativescript-ui-sidedrawer/platforms/android/TNSSideDrawer-release.aar + adding aar plugin dependency: /Users/santosh_singh20/Desktop/External-Driver-App/FE-external-driver-app/node_modules/kinvey-nativescript-sdk/platforms/android/kinvey_nativescript_sdk.aar + adding aar plugin dependency: /Users/santosh_singh20/Desktop/External-Driver-App/FE-external-driver-app/node_modules/nativescript-fingerprint-auth/platforms/android/nativescript_fingerprint_auth.aar + adding aar plugin dependency: /Users/santosh_singh20/Desktop/External-Driver-App/FE-external-driver-app/node_modules/nativescript-geolocation/platforms/android/nativescript_geolocation.aar + adding aar plugin dependency: /Users/santosh_singh20/Desktop/External-Driver-App/FE-external-driver-app/node_modules/nativescript-ui-core/platforms/android/TNSCore-release.aar + adding aar plugin dependency: /Users/santosh_singh20/Desktop/External-Driver-App/FE-external-driver-app/node_modules/nativescript-background-http/platforms/android/nativescript_background_http.aar + adding aar plugin dependency: /Users/santosh_singh20/Desktop/External-Driver-App/FE-external-driver-app/node_modules/tns-core-modules-widgets/platforms/android/widgets-release.aar Project successfully built. The build result is located at: /Users/santosh_singh20/Desktop/External-Driver-App/FE-external-driver-app/platforms/android/app/build/outputs/apk/debug/app-debug.apk Installing on device emulator-5554... Successfully installed on device with identifier 'emulator-5554'. Restarting application on device emulator-5554... Successfully synced application ca.cn.dma on device emulator-5554. JS: NSUTR: fetching http://127.0.0.1:9876/context.json JS: NSUTR: fetching http://192.168.29.206:9876/context.json JS: NSUTR: found karma at 192.168.29.206 JS: NSUTR: connecting to karma at http://192.168.29.206:9876 JS: ReferenceError: window is not defined

Emulator: Screenshot 2021-02-06 at 9 23 51 AM Karma server Screenshot 2021-02-06 at 9 24 16 AM

Debug console Screenshot 2021-02-06 at 9 24 42 AM

To Reproduce tns test android

Expected behavior run tests and see output on chrome and emulator.

Please help me on this

itsmerockingagain avatar Feb 06 '21 03:02 itsmerockingagain

Hi,

Same problem here.

It happens with newly created projects created by both cli 6.8.0 and cli 7.1.2 (the problem seems framework independent, although I've tested it with Angular projects only)

Turns out I had a project created by cli v6.8.0 which didn't had this problem. By carefully checking the dependencies I realized that in my previous project karma version was 4.4.1 and karma-jasmine was 2.0.1.

By updating those entries in the package.json file as follow:

  • "karma": "6.1.0" -> "karma": "4.4.1"
  • "karma-jasmine": "3.3.1" -> "karma-jasmine": "2.0.1"

The problem is gone.

@itsmerockingagain, since you have other karma packages you'll need to carefully check if other packages need downgrade or not, to match with that previous version of karma. Perhaps, none of them is incompatible though.

The problem seems to come from a breaking change induced by karma from v4.4.1 to v6.1.0. Perhaps something has to be updated in the karma-nativescript-laucher package or the karma.conf.js file? Just a guess, hadn't check in detail.

Hope this helps. This is a problem anyone creating a new project is going to face.


Edit: It seems there's no need to downgrade karma-jasmine to v2.0.1. Just replacing karma package version with 4.4.1 solves the problem. My project only has the example test though. I don't know if this can induce errors in larger projects.

agonper avatar Feb 08 '21 18:02 agonper

Karma 5.2.3 works as well as pointed out in #5472.

agonper avatar Feb 08 '21 18:02 agonper