How to correctly setup NativeScript Angular 2 with Karma + Jasmine or Mocha


#1

I’m trying to configure karma.config.js to run with NativeScript Angular project following the official doc:

https://docs.nativescript.org/angular/tooling/testing

The problem is the tests always failed and always running node_modules/ and platforms/ too but I mentioned them in excludeFiles property.

Dependencies version:

  • tns-android: 4.0.1
  • nativescript-unit-test-runner: ^0.3.4
  • karma: ^2.0.2
  • karma-jasmine: ^1.1.2
  • karma-nativescript-launcher: ^0.4.0
  • karma-typescript: ^3.0.12
  • nativescript: ^4.0.1
  • nativescript-dev-typescript: ~0.6.0

I’m trying to do that from two ways:

1. When I’m trying to use with Jasmine

29 05 2018 17:00:32.699:WARN [watcher]: All files matched by "/home/paneladm/my-ns-app/app/tests/*.ts" were excluded or matched by prior matchers.

29 05 2018 17:00:32.965:INFO [compiler.karma-typescript]: Compiling project using Typescript 2.6.2
29 05 2018 17:00:35.600:ERROR [compiler.karma-typescript]: app/utils/file-reader.ts(10,16): error TS2693: 'Promise' only refers to a type, but is being used as a value here.

29 05 2018 17:00:35.601:ERROR [compiler.karma-typescript]: node_modules/@angular/common/src/directives/ng_class.d.ts(48,34): error TS2304: Cannot find name 'Set'.

29 05 2018 17:00:35.601:ERROR [compiler.karma-typescript]: node_modules/@angular/core/src/change_detection/differs/default_iterable_differ.d.ts(12,32): error TS2304: Cannot find name 'Iterable'.

29 05 2018 17:00:35.601:ERROR [compiler.karma-typescript]: node_modules/@angular/core/src/change_detection/differs/default_keyvalue_differ.d.ts(24,16): error TS2304: Cannot find name 'Map'.

29 05 2018 17:00:35.601:ERROR [compiler.karma-typescript]: node_modules/@angular/core/src/change_detection/differs/default_keyvalue_differ.d.ts(32,16): error TS2304: Cannot find name 'Map'.

29 05 2018 17:00:35.601:ERROR [compiler.karma-typescript]: node_modules/@angular/core/src/change_detection/differs/iterable_differs.d.ts(14,48): error TS2304: Cannot find name 'Iterable'.

29 05 2018 17:00:35.602:ERROR [compiler.karma-typescript]: node_modules/@angular/core/src/change_detection/differs/keyvalue_differs.d.ts(22,18): error TS2304: Cannot find name 'Map'.

29 05 2018 17:00:35.602:ERROR [compiler.karma-typescript]: node_modules/@angular/core/src/di/reflective_provider.d.ts(87,123): error TS2304: Cannot find name 'Map'.

29 05 2018 17:00:35.602:ERROR [compiler.karma-typescript]: node_modules/@angular/core/src/di/reflective_provider.d.ts(87,165): error TS2304: Cannot find name 'Map'.

29 05 2018 17:00:35.602:ERROR [compiler.karma-typescript]: node_modules/@angular/platform-browser/src/browser/browser_adapter.d.ts(79,33): error TS2304: Cannot find name 'Map'.

29 05 2018 17:00:35.602:ERROR [compiler.karma-typescript]: node_modules/@angular/platform-browser/src/dom/dom_adapter.d.ts(97,42): error TS2304: Cannot find name 'Map'.

29 05 2018 17:00:35.602:ERROR [compiler.karma-typescript]: node_modules/@angular/platform-browser/src/dom/shared_styles_host.d.ts(11,30): error TS2304: Cannot find name 'Set'.

29 05 2018 17:00:35.602:ERROR [compiler.karma-typescript]: node_modules/@angular/platform-browser/src/dom/shared_styles_host.d.ts(22,30): error TS2304: Cannot find name 'Set'.

29 05 2018 17:00:35.603:ERROR [compiler.karma-typescript]: node_modules/nativescript-angular/dom-adapter.d.ts(79,34): error TS2304: Cannot find name 'Map'.

29 05 2018 17:00:35.603:ERROR [compiler.karma-typescript]: node_modules/nativescript-drop-down/drop-down.d.ts(16,33): error TS2307: Cannot find module 'data/observable-array'.

29 05 2018 17:00:35.603:ERROR [compiler.karma-typescript]: node_modules/nativescript-drop-down/drop-down.d.ts(17,62): error TS2307: Cannot find module 'ui/core/view'.

29 05 2018 17:00:35.603:ERROR [compiler.karma-typescript]: node_modules/nativescript-drop-down/drop-down.d.ts(18,30): error TS2307: Cannot find module 'ui/gestures/gestures'.

29 05 2018 17:00:35.603:ERROR [compiler.karma-typescript]: node_modules/nativescript-drop-down/drop-down.d.ts(19,29): error TS2307: Cannot find module 'ui/list-picker'.

29 05 2018 17:00:35.603:ERROR [compiler.karma-typescript]: node_modules/rxjs/Observable.d.ts(58,60): error TS2693: 'Promise' only refers to a type, but is being used as a value here.

29 05 2018 17:00:35.603:ERROR [compiler.karma-typescript]: node_modules/rxjs/Observable.d.ts(74,59): error TS2693: 'Promise' only refers to a type, but is being used as a value here.

29 05 2018 17:00:35.604:ERROR [compiler.karma-typescript]: node_modules/tns-core-modules/ui/core/view-base/view-base.d.ts(243,24): error TS2304: Cannot find name 'Set'.

29 05 2018 17:00:35.604:ERROR [compiler.karma-typescript]: node_modules/tns-core-modules/ui/core/view-base/view-base.d.ts(244,30): error TS2304: Cannot find name 'Set'.

29 05 2018 17:00:35.604:ERROR [compiler.karma-typescript]: node_modules/tns-core-modules/ui/core/view/view.d.ts(352,17): error TS2304: Cannot find name 'Set'.

29 05 2018 17:00:35.604:ERROR [compiler.karma-typescript]: node_modules/tns-core-modules/ui/core/view/view.d.ts(353,23): error TS2304: Cannot find name 'Set'.

29 05 2018 17:00:35.604:ERROR [compiler.karma-typescript]: node_modules/tns-core-modules/ui/styling/css-selector/css-selector.d.ts(16,18): error TS2304: Cannot find name 'Set'.

29 05 2018 17:00:35.604:ERROR [compiler.karma-typescript]: node_modules/tns-core-modules/ui/styling/css-selector/css-selector.d.ts(17,24): error TS2304: Cannot find name 'Set'.

29 05 2018 17:00:35.604:ERROR [compiler.karma-typescript]: node_modules/tns-core-modules/ui/styling/css-selector/css-selector.d.ts(57,41): error TS2304: Cannot find name 'Map'.

29 05 2018 17:00:35.604:ERROR [compiler.karma-typescript]: node_modules/tns-core-modules/ui/styling/css-selector/css-selector.d.ts(60,18): error TS2304: Cannot find name 'Set'.

29 05 2018 17:00:35.605:ERROR [compiler.karma-typescript]: node_modules/tns-core-modules/ui/styling/css-selector/css-selector.d.ts(61,21): error TS2304: Cannot find name 'Set'.

29 05 2018 17:00:35.708:INFO [compiler.karma-typescript]: Compiled 17 files in 2717 ms.
29 05 2018 17:00:35.723:WARN [karma]: Port 9876 in use
29 05 2018 17:00:35.724:INFO [karma]: Karma v2.0.2 server started at http://0.0.0.0:9877/
29 05 2018 17:00:35.724:INFO [launcher]: Launching browser android with unlimited concurrency
29 05 2018 17:00:35.752:INFO [launcher]: Starting browser NativeScript Unit Test Runner
Copying template files...
⠙ Installing tns-android
⠸ 29 05 2018 17:00:36.854:ERROR [karma]: Error: Unable to resolve module [./src/localize] from [/home/paneladm/my-ns-app/node_modules/nativescript-localize/angular.js]
{
  "basedir": "/home/paneladm/my-ns-app",
  "extensions": [
    ".js",
    ".json",
    ".ts",
    ".tsx"
  ],
  "moduleDirectory": [
    "node_modules"
  ],
  "modules": {
    "assert": "/home/paneladm/my-ns-app/node_modules/assert/assert.js",
    "buffer": "/home/paneladm/my-ns-app/node_modules/buffer/index.js",
    "child_process": "/home/paneladm/my-ns-app/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "cluster": "/home/paneladm/my-ns-app/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "console": "/home/paneladm/my-ns-app/node_modules/console-browserify/index.js",
    "constants": "/home/paneladm/my-ns-app/node_modules/constants-browserify/constants.json",
    "crypto": "/home/paneladm/my-ns-app/node_modules/crypto-browserify/index.js",
    "dgram": "/home/paneladm/my-ns-app/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "dns": "/home/paneladm/my-ns-app/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "domain": "/home/paneladm/my-ns-app/node_modules/domain-browser/source/index.js",
    "events": "/home/paneladm/my-ns-app/node_modules/events/events.js",
    "fs": "/home/paneladm/my-ns-app/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "http": "/home/paneladm/my-ns-app/node_modules/stream-http/index.js",
    "https": "/home/paneladm/my-ns-app/node_modules/https-browserify/index.js",
    "module": "/home/paneladm/my-ns-app/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "net": "/home/paneladm/my-ns-app/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "os": "/home/paneladm/my-ns-app/node_modules/os-browserify/browser.js",
    "path": "/home/paneladm/my-ns-app/node_modules/path-browserify/index.js",
    "punycode": "/home/paneladm/my-ns-app/node_modules/punycode/punycode.js",
    "querystring": "/home/paneladm/my-ns-app/node_modules/querystring-es3/index.js",
    "readline": "/home/paneladm/my-ns-app/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "repl": "/home/paneladm/my-ns-app/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "stream": "/home/paneladm/my-ns-app/node_modules/stream-browserify/index.js",
    "_stream_duplex": "/home/paneladm/my-ns-app/node_modules/readable-stream/duplex.js",
    "_stream_passthrough": "/home/paneladm/my-ns-app/node_modules/readable-stream/passthrough.js",
    "_stream_readable": "/home/paneladm/my-ns-app/node_modules/readable-stream/readable.js",
    "_stream_transform": "/home/paneladm/my-ns-app/node_modules/readable-stream/transform.js",
    "_stream_writable": "/home/paneladm/my-ns-app/node_modules/readable-stream/writable.js",
    "string_decoder": "/home/paneladm/my-ns-app/node_modules/string_decoder/lib/string_decoder.js",
    "sys": "/home/paneladm/my-ns-app/node_modules/util/util.js",
    "timers": "/home/paneladm/my-ns-app/node_modules/timers-browserify/main.js",
    "tls": "/home/paneladm/my-ns-app/node_modules/karma-typescript/dist/bundler/resolve/empty.js",
    "tty": "/home/paneladm/my-ns-app/node_modules/tty-browserify/index.js",
    "url": "/home/paneladm/my-ns-app/node_modules/url/url.js",
    "util": "/home/paneladm/my-ns-app/node_modules/util/util.js",
    "vm": "/home/paneladm/my-ns-app/node_modules/vm-browserify/index.js",
    "zlib": "/home/paneladm/my-ns-app/node_modules/browserify-zlib/lib/index.js",
    "_process": "/home/paneladm/my-ns-app/node_modules/process/browser.js"
  },
  "filename": "",
  "paths": []
}
Error: Cannot find module './src/localize' from '/home/paneladm/my-ns-app/node_modules/nativescript-localize'
    at /home/paneladm/my-ns-app/node_modules/karma-typescript/dist/bundler/resolve/resolver.js:163:27
    at /home/paneladm/my-ns-app/node_modules/browser-resolve/index.js:265:24
    at /home/paneladm/my-ns-app/node_modules/browser-resolve/node_modules/resolve/lib/async.js:55:18
    at load (/home/paneladm/my-ns-app/node_modules/browser-resolve/node_modules/resolve/lib/async.js:69:43)
    at onex (/home/paneladm/my-ns-app/node_modules/browser-resolve/node_modules/resolve/lib/async.js:92:31)
    at /home/paneladm/my-ns-app/node_modules/browser-resolve/node_modules/resolve/lib/async.js:22:47
    at FSReqWrap.oncomplete (fs.js:170:21)
Test run failed.

2. I’m trying to do that using karma-typescript dependency

The karma.conf.js using typescript:

I found an issues about that:

And @erikbarke explanation here:

Both, 1 and 2 scenarios were posted on StackOverflow:

Here a resume of console error:

  • Error: Can't find entrypoint .../example.ts
  • /base/base.spect.ts? FAILED SyntaxError: Unexpected token import

Support to setup nativescript unit test mocha,chai,karma on android emulator