NativeScript / nativescript-cli

Command-line interface for building NativeScript apps
https://www.npmjs.com/package/nativescript
Apache License 2.0
1.04k stars 196 forks source link

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

Open itsmerockingagain opened 3 years ago

itsmerockingagain commented 3 years ago

Environment

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]

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 (?<!\bAppResources\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]

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

agonper commented 3 years ago

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:

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 commented 3 years ago

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