question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NativeScript - how to get unit tests to work properly?

See original GitHub issue

I’m writing an app with NativeScript 6+ and Angular 8+.

I’m trying to write some unit tests and get them up and running.

I have read the documentation on unit testing: https://docs.nativescript.org/tooling/testing/testing

I followed the directions there for setting up the tests and using TestBed. My tests are not working and throwing errors.

Here is my repository: https://github.com/aubrey-fowler/NativeScriptUnitTests

Questions:

  1. The documentation only shows an example of how to write a test for a component. How do I write a test for a service? I also need to use TestBed for this because my service has dependency injection.
  2. My tests are throwing errors. Why and how can I fix them?

Errors:

no reachable hosts on my Android phone

code snippet:

import { ItemsComponent } from '../app/item/items.component';

import {
    nsTestBedAfterEach,
    nsTestBedBeforeEach,
    nsTestBedRender
} from 'nativescript-angular/testing';

describe('ItemsComponent Test', () => {

    beforeEach(nsTestBedBeforeEach([ItemsComponent]));
    afterEach(nsTestBedAfterEach(false));

    it('should be defined', () => {

        nsTestBedRender(ItemsComponent).then((fixture) => {
            fixture.detectChanges();
            const component = fixture.componentInstance;
            expect(component).toBeTruthy;
        });

    });

});

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
NathanWalkercommented, Jun 5, 2020

Thank you for the repo @aubrey-fowler - we’ll take a look!

0reactions
saschaarthurcommented, Sep 16, 2020

@NathanWalker doesnt work either:

tns test android --env.aot
Searching for devices...
File change detected. Starting incremental webpack compilation...
(node:6352) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead
i 「wdm」: wait until bundle finished: noop
i 「wdm」: wait until bundle finished: noop
i 「wdm」: wait until bundle finished: noop
i 「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.
i 「wdm」: Hash: a7620b6683bda0561d99
Version: webpack 4.27.1
Time: 14088ms
Built at: 2020-09-16 12:03:44 PM
                              Asset       Size                     Chunks             Chunk Names
                         runtime.js   5.95 KiB                    runtime  [emitted]  runtime
        src\tests\example.bundle.js  366 bytes          src\tests\example  [emitted]  src\tests\example
   src\tests\item.service.bundle.js    3.5 KiB     src\tests\item.service  [emitted]  src\tests\item.service
src\tests\items.component.bundle.js   7.95 KiB  src\tests\items.component  [emitted]  src\tests\items.component
          src\tests\setup.bundle.js   1.91 KiB            src\tests\setup  [emitted]  src\tests\setup
                   vendor.bundle.js   6.64 MiB                     vendor  [emitted]  vendor
Entrypoint src\tests\item.service = runtime.js vendor.bundle.js src\tests\item.service.bundle.js
Entrypoint src\tests\example = runtime.js src\tests\example.bundle.js
Entrypoint src\tests\items.component = runtime.js vendor.bundle.js src\tests\items.component.bundle.js
Entrypoint src\tests\setup = runtime.js vendor.bundle.js src\tests\setup.bundle.js
[../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 110 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 4.42 KiB {vendor} [built]
[../node_modules/@nativescript/angular/platform.js] ./node_modules/@nativescript/angular/platform.js 2.24 KiB {vendor} [built]
[../node_modules/@nativescript/angular/testing/index.js] ./node_modules/@nativescript/angular/testing/index.js 1.67 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/testing.jasmine.js] ./node_modules/@nativescript/angular/zone-js/testing.jasmine.js 228 bytes {vendor} [built]
[./app/item/item.service.ts] ./src/app/item/item.service.ts 1.59 KiB {src\tests\item.service} {src\tests\items.component} [built]
[./app/item/items.component.ts] ./src/app/item/items.component.ts 733 bytes {src\tests\items.component} [built]
[./tests/example.ts] ./src/tests/example.ts 162 bytes {src\tests\example} [built]
[./tests/item.service.ts] ./src/tests/item.service.ts 430 bytes {src\tests\item.service} [built]
[./tests/items.component.ts] ./src/tests/items.component.ts 877 bytes {src\tests\items.component} [built]
[./tests/setup.ts] ./src/tests/setup.ts 134 bytes {src\tests\setup} [built]
    + 591 hidden modules
i 「wdm」: Compiled successfully.
16 09 2020 12:03:44.617:WARN [karma]: No captured browser, open http://localhost:9877/
16 09 2020 12:03:44.633:INFO [karma-server]: Karma v4.4.1 server started at http://0.0.0.0:9877/
16 09 2020 12:03:44.633:INFO [launcher]: Launching browsers android with concurrency unlimited
16 09 2020 12:03:44.666:INFO [launcher]: Starting browser NativeScript Unit Test Runner
Preparing project...
File change detected. Starting incremental webpack compilation...

webpack is watching the files…

Hash: f3ade68811e5c01b58be
Version: webpack 4.27.1
Time: 5982ms
Built at: 2020-09-16 12:03:52 PM
              Asset       Size   Chunks             Chunk Names
          bundle.js   31.1 KiB   bundle  [emitted]  bundle
       package.json  112 bytes           [emitted]
         runtime.js   13.9 KiB  runtime  [emitted]  runtime
tns-java-classes.js    0 bytes           [emitted]
          vendor.js   16.3 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) 263 bytes {bundle} [built]
[./app/item/item.service.ts] 1.63 KiB {bundle} [built]
[./app/item/items.component.ts] 396 bytes {bundle} [built]
[./main.ts] 1.1 KiB {bundle} [built]
[./package.json] 107 bytes {bundle} [optional] [built]
[./tests/example.ts] 162 bytes {bundle} [optional] [built]
[./tests/item.service.ts] 430 bytes {bundle} [optional] [built]
[./tests/items.component.ts] 877 bytes {bundle} [optional] [built]
[./tests/setup.ts] 134 bytes {bundle} [optional] [built]
    + 621 hidden modules
Webpack compilation complete. Watching for file changes.
Webpack build done!
Project successfully prepared (android)
Successfully transferred bundle.js on device d123a500.
Restarting application on device d123a500...
16 09 2020 12:03:57.242:INFO [NativeScript / 29 (10; IN2023)]: Connected on socket BOhHVyPD6SsFebx8AAAA with id NativeScriptUnit-6768
Successfully synced application org.nativescript.UnitTesting on device d123a500.
NativeScript / 29 (10; IN2023) ItemsComponent Test should be defined FAILED
        error properties: Object({ originalStack: 'Error: Can't resolve all parameters for ApplicationModule: (?).
            at ZoneAwareError (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:85903:33)
            at syntaxError (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:9475:17)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getDependenciesMetadata (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28631:35)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getTypeMetadata (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28523:26)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleMetadata (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28391:24)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/com ...
        Error: Can't resolve all parameters for ApplicationModule: (?).
            at ZoneAwareError (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:85903:33)
            at syntaxError (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:9475:17)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getDependenciesMetadata (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28631:35)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getTypeMetadata (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28523:26)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleMetadata (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28391:24)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleSummary (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28201:35)
            at file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28288:55
            at <Jasmine>
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleMetadata (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28266:49)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleSummary (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28201:35)
            at file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28288:55
            at <Jasmine>
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleMetadata (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28266:49)
            at JitCompiler.push.../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._loadModules (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:34433:51)
            at JitCompiler.push.../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileModuleAndAllComponents (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:34421:36)
        error properties: Object({ originalStack: 'Error: Can't resolve all parameters for ApplicationModule: (?).
            at ZoneAwareError (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:85903:33)
            at syntaxError (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:9475:17)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getDependenciesMetadata (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28631:35)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getTypeMetadata (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28523:26)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleMetadata (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28391:24)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/com ...
        Error: Can't resolve all parameters for ApplicationModule: (?).
            at ZoneAwareError (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:85903:33)
            at syntaxError (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:9475:17)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getDependenciesMetadata (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28631:35)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver._getTypeMetadata (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28523:26)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleMetadata (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28391:24)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleSummary (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28201:35)
            at file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28288:55
            at <Jasmine>
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleMetadata (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28266:49)
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleSummary (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28201:35)
            at file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28288:55
            at <Jasmine>
            at CompileMetadataResolver.push.../node_modules/@angular/compiler/fesm5/compiler.js.CompileMetadataResolver.getNgModuleMetadata (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:28266:49)
            at JitCompiler.push.../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._loadModules (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:34433:51)
            at JitCompiler.push.../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._compileModuleAndAllComponents (file:///data/data/org.nativescript.UnitTesting/files/app/vendor.js:34421:36)
NativeScript / 29 (10; IN2023): Executed 5 of 5 (1 FAILED) (0.092 secs / 0.012 secs)
TOTAL: 1 FAILED, 4 SUCCESS
JS: NSUTR: fetching http://10.10.10.250:9877/context.json
JS: NSUTR: fetching http://172.21.208.1:9877/context.json
JS: NSUTR: fetching http://192.168.1.91:9877/context.json
JS: NSUTR: fetching http://192.168.56.1:9877/context.json
JS: NSUTR: fetching http://127.0.0.1:9877/context.json
JS: NSUTR: found karma at 192.168.1.91
JS: NSUTR: connecting to karma at http://192.168.1.91:9877
JS: NSUTR: successfully connected to karma
JS: getFrameById() is deprecated. Use Frame.getFrameById() instead.
JS: NSUTR: downloading http://192.168.1.91:9877/context.json
JS: NSUTR: eval script /base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?fbc7eaf339ea249d8c912b258548d4e7b4dc180c
JS: NSUTR: eval script /base/node_modules/karma-jasmine/lib/boot.js?760d54bbca4f739f1f8b252c1636d76201cc4e88
JS: NSUTR: eval script /base/node_modules/karma-jasmine/lib/adapter.js?f693fb2743611cfdd0e82f7adbb709f510583603
JS: NSUTR: eval script /base/src/tests/example.bundle.js?c90d47fe20565ea5946bb869444e23f7780dbee6
JS: NSUTR: eval script /base/src/tests/item.service.bundle.js?28f45223f86d9681ae9d6923a2587254c4527866
JS: NSUTR: eval script /base/src/tests/items.component.bundle.js?ba8a69c5a4e8a138634a38a3d02c2efb5b49f794
JS: NSUTR: eval script /base/src/tests/setup.bundle.js?64a7258273dc1e95ccf16770a4b50ef47f6af080
JS: NSUTR: beginning test run
NativeScript / 29 (10; IN2023) ERROR
  DisconnectedClient disconnected from CONNECTED state (transport error)
NativeScript / 29 (10; IN2023): Executed 5 of 5 (1 FAILED) (0.092 secs / 0.012 secs)
JS: NSUTR: completed test run.
JS: NSUTR: completeAck

To reproduce:

# git log -n 1
commit 86134c5baf509b8e9b805ad70fcb58bbb03f80ca (HEAD -> master, origin/master, origin/HEAD)
Author: Aubrey Quinn <xxxx>
Date:   Wed Mar 18 11:24:36 2020 +0000

    Update items.component.ts
    
    alternative ItemsComponent Test written

#git remote -v
origin  https://github.com/aubrey-fowler/NativeScriptUnitTests.git (fetch)
origin  https://github.com/aubrey-fowler/NativeScriptUnitTests.git (push)

#git diff
diff --git a/src/tests/items.component.ts b/src/tests/items.component.ts
index 7904be5..4833001 100644
--- a/src/tests/items.component.ts
+++ b/src/tests/items.component.ts
@@ -7,7 +7,7 @@ import {
     nsTestBedRender
 } from 'nativescript-angular/testing';
 
-xdescribe('ItemsComponent Test', () => {
+describe('ItemsComponent Test', () => {
 
     beforeEach(nsTestBedBeforeEach([ItemsComponent]));
     afterEach(nsTestBedAfterEach(false));
@@ -34,4 +34,4 @@ describe('ItemsComponent Test', () => {
         expect(component).toBeTruthy();
     });
 
-});
\ No newline at end of file
+});

Sadly my project runs into the exact same issues about setting up the UnitTests, thats why its impossible to use them at the moment, which is pretty bad because some stuff needs to be covered with it…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit Testing - NativeScript Docs
Write and execute unit tests to ensure that newly added features are working correctly and no regressions are introduced in the mobile app....
Read more >
NativeScript - how to get unit tests to work properly?
The documentation only shows an example of how to write a test for a component. How do I write a test for a...
Read more >
NativeScript - Testing - Tutorialspoint
Unit testing is the easiest method to test an application. It is based on ensuring the correctness of a piece of code (a...
Read more >
Unit Testing For NativeScript - YouTube
Lets discuss how you can take your really cool app; and make sure it continues to work after you make bug fixes and...
Read more >
Adding Unit Tests to Your NativeScript App
Enter the unit test. Unit testing has us write tests, in code, to test the smallest pieces of our apps. In theory, as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found