NativeScript / nativescript-angular

Integrating NativeScript with Angular
http://docs.nativescript.org/angular/tutorial/ng-chapter-0
Apache License 2.0
1.21k stars 241 forks source link

Default unit testing failing on hello world #1310

Open benjlin opened 6 years ago

benjlin commented 6 years ago

When attempting to run unit testing on the default template app it crashes with

bens-mbp:test benjlin$ tns test ios
Searching for devices...
01 05 2018 07:27:07.314:WARN [karma]: No captured browser, open http://localhost:9876/
01 05 2018 07:27:07.328:INFO [karma]: Karma v2.0.2 server started at http://0.0.0.0:9876/
01 05 2018 07:27:07.329:INFO [launcher]: Launching browser ios with unlimited concurrency
01 05 2018 07:27:07.357:INFO [launcher]: Starting browser NativeScript Unit Test Runner
Executing before-prepare hook from /Users/benjlin/Documents/Code/Nativescript/test/hooks/before-prepare/nativescript-dev-typescript.js
Found peer TypeScript 2.6.2
Preparing project...
01 05 2018 07:27:10.721:INFO [watcher]: Changed file "/Users/benjlin/Documents/Code/Nativescript/test/app/app.component.js".
01 05 2018 07:27:10.722:INFO [watcher]: Changed file "/Users/benjlin/Documents/Code/Nativescript/test/app/app.module.js".
01 05 2018 07:27:10.723:INFO [watcher]: Changed file "/Users/benjlin/Documents/Code/Nativescript/test/app/main.aot.js".
01 05 2018 07:27:10.725:INFO [watcher]: Changed file "/Users/benjlin/Documents/Code/Nativescript/test/app/main.js".
01 05 2018 07:27:10.977:WARN [karma]: No captured browser, open http://localhost:9876/
01 05 2018 07:27:14.197:INFO [Chrome 65.0.3325 (Mac OS X 10.13.4)]: Connected on socket hD-sUKklfXQ2LEbmAAAA with id manual-3472
Project successfully prepared (ios)
Executing after-prepare hook from /Users/benjlin/Documents/Code/Nativescript/test/hooks/after-prepare/nativescript-unit-test-runner.js
Executing before-liveSync hook from /Users/benjlin/Documents/Code/Nativescript/test/hooks/before-liveSync/nativescript-angular-sync.js
Skipping prepare.
Successfully transferred all files.
Refreshing application...
Successfully synced application org.nativescript.test on device AD2FE2D6-4E3F-41B8-AE8C-460BE9C15843.
Executing before-watch hook from /Users/benjlin/Documents/Code/Nativescript/test/hooks/before-watch/nativescript-dev-typescript.js
Found peer TypeScript 2.6.2
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:54:28: NSUTR: fetching http://127.0.0.1:9876/context.json
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:54:28: NSUTR: fetching http://192.168.86.105:9876/context.json
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:54:28: NSUTR: fetching http://169.254.100.211:9876/context.json
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:60:32: NSUTR: found karma at 127.0.0.1
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:97:20: NSUTR: connecting to karma at http://127.0.0.1:9876
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:60:32: NSUTR: found karma at 192.168.86.105
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:60:32: NSUTR: found karma at 169.254.100.211
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:116:24: NSUTR: successfully connected to karma
01 05 2018 07:27:24.238:INFO [NativeScript / 11.3 (11.3; iPhone)]: Connected on socket pi1YRlwENESpccmzAAAB with id NativeScriptUnit-2789
Chrome 65.0.3325 (Mac OS X 10.13.4) ERROR
  {
    "message": "An error was thrown in afterAll\nUncaught ReferenceError: exports is not defined\nUncaught ReferenceError: exports is not defined\nUncaught ReferenceError: exports is not defined\nUncaught ReferenceError: exports is not defined",
    "str": "An error was thrown in afterAll\nUncaught ReferenceError: exports is not defined\nUncaught ReferenceError: exports is not defined\nUncaught ReferenceError: exports is not defined\nUncaught ReferenceError: exports is not defined"
  }
Chrome 65.0.3325 (Mac OS X 10.13.4): Executed 1 of 1 ERROR (0.01 secs / 0 secs)
01 05 2018 07:27:24.414:INFO [watcher]: Changed file "/Users/benjlin/Documents/Code/Nativescript/test/app/app.component.js".
01 05 2018 07:27:24.578:INFO [watcher]: Changed file "/Users/benjlin/Documents/Code/Nativescript/test/app/app.module.js".
01 05 2018 07:27:24.679:INFO [watcher]: Changed file "/Users/benjlin/Documents/Code/Nativescript/test/app/main.js".
01 05 2018 07:27:24.680:INFO [watcher]: Changed file "/Users/benjlin/Documents/Code/Nativescript/test/app/main.aot.js".
01 05 2018 07:27:24.934:INFO [karma]: Delaying execution, these browsers are not ready: NativeScript / 11.3 (11.3; iPhone)
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:159:20: NSUTR: downloading http://127.0.0.1:9876/context.json
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:200:32: NSUTR: eval script /base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?daba65c98fa088349a3e9d7df843a63405ccfc15
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:200:32: NSUTR: eval script /base/node_modules/karma-jasmine/lib/boot.js?945a38bf4e45ad2770eb94868231905a04a0bd3e
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:200:32: NSUTR: eval script /base/node_modules/karma-jasmine/lib/adapter.js?3e6cdea3167f037eeb11034f8eb9ce63b21a8105
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:196:32: NSUTR: require script /base/app/tests/example.js?d4cad4cd203df70666c986cd09ce402b21d89b39 from ../../tests/example.js
Chrome 65.0.3325 (Mac OS X 10.13.4): Executed 1 of 1 ERROR (0.01 secs / 0 secs)
Chrome 65.0.3325 (Mac OS X 10.13.4): Executed 1 of 1 ERROR (0.01 secs / 0 secs)
NativeScript / 11.3 (11.3; iPhone): Executed 1 of 1 SUCCESS (0.014 secs / 0 secs)
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:258:24: NSUTR: completeAck
Chrome 65.0.3325 (Mac OS X 10.13.4) ERROR
  {
    "message": "An error was thrown in afterAll\nUncaught ReferenceError: exports is not defined\nUncaught ReferenceError: exports is not defined\nUncaught ReferenceError: exports is not defined\nUncaught ReferenceError: exports is not defined",
    "str": "An error was thrown in afterAll\nUncaught ReferenceError: exports is not defined\nUncaught ReferenceError: exports is not defined\nUncaught ReferenceError: exports is not defined\nUncaught ReferenceError: exports is not defined"
  }
Chrome 65.0.3325 (Mac OS X 10.13.4): Executed 1 of 1 ERROR (0.004 secs / 0 secs)
7:27:25 AM - Compilation complete. Watching for file changes.

NativeScript / 11.3 (11.3; iPhone) ERROR
  Disconnected
Chrome 65.0.3325 (Mac OS X 10.13.4): Executed 1 of 1 ERROR (0.004 secs / 0 secs)
^CExecuting after-watch hook from /Users/benjlin/Documents/Code/Nativescript/test/hooks/after-watch/nativescript-dev-typescript.js
Stopping tsc watch

Steps to replicate

  1. run tns create HelloWorld --template nativescript-template-ng-tutorial
  2. cd ./HelloWorld
  3. tns run ios
  4. tns test init
  5. Select Jasmin as the test framework
  6. admire the failed test.

the same error occurs running both android and ios

I suspect that it is something to do with the default Karma configuration file generated

module.exports = function(config) {
  config.set({

    // 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: [
      'app/**/*.js',
    ],

    // 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
  })
}
tsonevn commented 6 years ago

Hi @benjlin, Thank you for contacting us. We will check reported case and will verify if there is a real issue, which could lead to a crash on the application. You can keep track on the issue for further info.

pekevski commented 6 years ago

I also experience this issue within my project and recreated it by following the testing documentation for the downloaded playground application. It seems when invoking the tns test , the client disconnects from the karma server because it cannot compile some javascript.

I get the following errors in the console for the browser:

screen shot 2018-05-04 at 4 53 33 pm
CapellaBlue commented 6 years ago

@pekevski Did you discover the cause of your "uncaught ReferenceError: exports is not defined"

slaven3kopic commented 6 years ago

Same behavior while running "tns test android" for template app (https://github.com/NativeScript/template-drawer-navigation-ng). Any info about this issue?

CapellaBlue commented 6 years ago

Here's what I learned:

Use an emulator. When you start your app, (for me it became 'npm start') and Karma asked to open a browser,I was doing so...only to get the exports not defined error.

Having an emulator set up within the AVD management portion of Android studio helped. After lots of requests for me to open a browser, karma finally just opens the emulator.

However, I was still unable to run tests. I ran into "Zone is undefined" afterwards...which is likely related to the karma.config file set up, although I'm unsure why just yet.

Most recommendations I've seen advise not to use Karma on mobile apps, which has been in response to a lack of solution being found.

Either way, look at your karma.config file and try using an emulator.

benjlin commented 6 years ago

The initial error that I got was on an emulator, I tried again on the simulator and also found that to be broken, I posted the results from the ios emulator dump and it seems that when my post was edited to read cleaner that was lost.

I will try again in the next few days to see if anything has changed with the 4.1.X release as well as downgrade to 3.4.X to see if it is present there

benjlin commented 6 years ago

I ran a clean install of nativescript on a new computer, and when i downloaded the helloWorld template

tns create HelloWorld --template nativescript-template-ng-tutorial

I found it still broken by default, however after upgrading the typescript dev definition from "~2.7.2" to "2.9.2" i found that the tests work, after successfully running the test, the app crashes but it does successfully run the tests

19 07 2018 13:45:28.905:WARN [karma]: No captured browser, open http://localhost:9876/ Skipping prepare. Building project... Gradle build...

slaven3kopic commented 6 years ago

@pekevski , @CapellaBlue ,

I had the same problem. I've solved it by doing "tns test init", and picking the mocha framework. Then add the following files to /test folder:

test-config.ts . With content:

(global).mocha.setup({ timeout: 20000 });

import * as chai from 'chai'; export let assert: typeof chai.assert = (global).chai.assert;

test-main.ts . With content:

import 'nativescript-angular/zone-js/testing.mocha'; import {nsTestBedInit} from 'nativescript-angular/testing'; nsTestBedInit();

example.ts . With content:

import {assert} from './test-config';

describe('A suite', function() { it('contains spec with an expectation', function() { assert.equal(true, true); }); });

Result:

JS: NSUTR: eval script /base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae JS: NSUTR: eval script /base/node_modules/karma-chai/adapter.js?bab484a2111791847997ed88414622bccfbd2749 JS: NSUTR: eval script /base/node_modules/mocha/mocha.js?8bf1d1adf34e719cca6e8f6915b9cd6eabf83d6b JS: NSUTR: eval script /base/node_modules/karma-mocha/lib/adapter.js?e8826109cf50b958ee4bd667517729ffdd4de363 JS: NSUTR: require script /base/app/tests/example.js?5f2953ee9642c9402922f1858f6c6daeb474e644 from ../../tests/example.js JS: NSUTR: require script /base/app/tests/test-config.js?0feba9db7ae5fc2c47ffef99cf2314ec89d56428 from ../../tests/test-config.js JS: NSUTR: require script /base/app/tests/test-main.js?20993e7c421b970122391ecef7a52ed3b0866208 from ../../tests/test-main.js JS: NSUTR: beginning test run NativeScript / 27 (8.1.0; Android SDK built for x86): Executed 0 of 1 SUCCESS (0 secs / 0 secs) NativeScript / 27 (8.1.0; Android SDK built for x86): Executed 1 of 1 SUCCESS (0.141 secs / 0.002 secs) TOTAL: 1 SUCCESS

m-abs commented 5 years ago

I ran into this too. I created a new project with tns create <NAME> --ng and configured testing with tns test init --framework jasmine.

This gave the same error as @benjlin reported.

After a while I tried renaming src to app and updated my tsconfig.json and nsconfig.json to match that and that solved the problem. I don't understand why it worked, but it did.

irwansyahwii commented 5 years ago

@m-abs I believe it works because the hooks didn't ran when you are using the new structure but when you changed to the old structure then the hooks will ran and the tests will success

Oatelaus commented 5 years ago

I've managed to resolve this (exports is not defined) by degrading jasmine-core to ^2.9.0.

vakrilov commented 5 years ago

Hey folks. I also managed to get the test running by renaming src -> app as @m-abs mentioned. I had to change src to app in karma.config.js, nsconfig.json and tsconfig.json as well.

vakrilov commented 5 years ago

After more digging around I found the reason why tests aren't working with src folder.

The unit-test-runner app has a hardcoded app folder here. However files are in base/src instead of base/app. So the test files are not treated as "local" and are eval-ed instead of required.

Maybe the solution is to pass the actual app folder to the unit-test-app trough the config.js options, so that it can correctly determine which files are tests. @rosen-vladimirov - what do you think? I can do a PR if you think this is the correct way to go?

m-abs commented 5 years ago

Even with a new project and latest versions, I had to rename src => app to run my tests without getting ReferenceError: exports is not defined.

I tried switching between mocha and jasmine same result.

I also needed to add android:usesCleartextTraffic="true" to my AndroidManifest.xml because I ran the test on an emulator with Android API 28.

My package.json:

{
  "nativescript": {
    "id": "org.nativescript.demo",
    "tns-android": {
      "version": "5.1.0"
    },
    "tns-ios": {
      "version": "5.1.0"
    }
  },
  "description": "NativeScript Application",
  "license": "SEE LICENSE IN <your-license-filename>",
  "repository": "<fill-your-repository-here>",
  "dependencies": {
    "@angular/animations": "~7.2.0",
    "@angular/common": "~7.2.0",
    "@angular/compiler": "~7.2.0",
    "@angular/core": "~7.2.0",
    "@angular/forms": "~7.2.0",
    "@angular/http": "~7.2.0",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",
    "@nota/nativescript-ngx-tools": "file:../src",
    "nativescript-angular": "~7.2.0",
    "nativescript-theme-core": "~1.0.4",
    "nativescript-unit-test-runner": "^0.4.1",
    "reflect-metadata": "~0.1.13",
    "rxjs": "^6.4.0",
    "tns-core-modules": "~5.1.2",
    "zone.js": "^0.8.29",
    "typescript": "~3.1.6"
  },
  "devDependencies": {
    "@angular/compiler-cli": "~7.2.0",
    "@nativescript/schematics": "~0.5.0",
    "@ngtools/webpack": "~7.2.0",
    "@types/jasmine": "^3.3.8",
    "karma": "4.0.0",
    "karma-jasmine": "2.0.1",
    "karma-nativescript-launcher": "0.4.0",
    "nativescript-dev-typescript": "~0.7.9",
    "nativescript-dev-webpack": "~0.19.1",
    "prettier": "^1.16.4",
    "prettier-check": "^2.0.0",
    "tns-platform-declarations": "^5.1.2"
  },
  "readme": "NativeScript Application"
}

Project was created with tns create --ng followed by tns test init --framework jasmine.

Fatme commented 5 years ago

@m-abs,

This should be fixed in next version of {N} CLI, so you need

npm i -g nativescript@next
pekevski commented 4 years ago

I am getting the following error when running karma unit tests:

Chrome 81.0.4044.138 (Mac OS 10.14.6) ERROR An error was thrown in afterAll Uncaught ReferenceError: global is not defined ReferenceError: global is not defined at http://127.0.0.1:9876src/tests/example.bundle.js:1:1

I am running tns cli v6.5.0 and its failing on the example unit test on a fresh angular NS projection

Reproduced by:

  1. tns create <test> --ng
  2. tns test init
  3. tns test ios --emulator
pekevski commented 4 years ago

I added (window as any).global = window; in my main.ts and it seems to have gone away based on this

jitendraashutec commented 4 years ago

@pekevski I have added (window as any).global = window; in my main.ts but still I am getting error. Do I need more changes to resolve this error?

shansb-dev commented 4 years ago

I have the same issue. Running the auto generated example.ts results in an error:

An error was thrown in afterAll
  Uncaught ReferenceError: global is not defined

Adding (window as any).global = window; to main.ts does not solve the issue either.