Skip to content

unit testing for freshly created angular project is broken on android simulator #4232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ochemerys opened this issue Dec 13, 2018 · 11 comments
Closed
Assignees
Milestone

Comments

@ochemerys
Copy link

Environment
$ tns doctor
✔ Getting environment information

No issues were detected.
✔ Your ANDROID_HOME environment variable is set and points to correct directory.
✔ Your adb from the Android SDK is correctly installed.
✔ The Android SDK is installed.
✔ A compatible Android SDK for compilation is found.
✔ Javac is installed and is configured properly.
✔ The Java Development Kit (JDK) is installed and is configured properly.
✔ Xcode is installed and is configured properly.
✔ xcodeproj is installed and is configured properly.
✔ CocoaPods are installed.
✔ CocoaPods update is not required.
✔ CocoaPods are configured properly.
✔ Your current CocoaPods version is newer than 1.0.0.
✔ Python installed and configured correctly.
✔ The Python 'six' package is found.
✔ Xcode version 10.1.0 satisfies minimum required version 9.
✔ Getting NativeScript components versions information...
✔ Component nativescript has 5.1.0 version and is up to date.
✔ Component tns-core-modules has 5.1.0 version and is up to date.
✔ Component tns-android has 5.1.0 version and is up to date.
✔ Component tns-ios has 5.1.0 version and is up to date.

karma.conf.js contains
// list of files / patterns to load in the browser
files: [
'src/tests/**/*.js'
],

android simulator: Nexus_S_API_28

Describe the bug
unit testing for freshly created angular project is broken on android

To Reproduce run commands

$ tns create demo-mocha-tests-ng --ng
$ tns test init

select mocha

$ tns test android

screen shot 2018-12-13 at 12 41 55 pm

after running test application run is failed
$ tns run android
see screenshot above
app can be run only after command
$ tns platform clean android

Expected behavior
example test should run on android

@Fatme
Copy link
Contributor

Fatme commented Dec 13, 2018

@ochemerys,

You need to add android:usesCleartextTraffic="true" in App_Resources/android/AndroidManifest.xml.

<application
         android:usesCleartextTraffic="true"

@ochemerys
Copy link
Author

Thank you.
When I added suggested line to
App_Resources/Android/src/main/AndroidManifest.xml
I was able to run example test
but when I created a simple typescript test:

import { AppComponent } from '../app/app.component';

// application test
describe('Import Statement', function () {
		it('should be possible to add import statement', function () {
			const app = new AppComponent();
			chai.assert.equal(true, true);
		});
});

the run fails

13 12 2018 15:06:36.794:WARN [NativeScript / 28 (9; Android SDK built for x86)]: Adapter did not report total number of specs.
NativeScript / 28 (9; Android SDK built for x86)  /base/src/tests/example.spec.js?a737542566db4d779d1ad05914405977fcfa4f81 at line 0 FAILED
        ReferenceError: exports is not defined
NativeScript / 28 (9; Android SDK built for x86): Executed 1 of null (1 FAILED) ERROR (0.051 secs / 0 secs)
JS: NSUTR: completeAck
13 12 2018 15:06:37.427:WARN [NativeScript / 28 (9; Android SDK built for x86)]: Disconnected (0 times)Client disconnected from CONNECTED state (transporNativeScript / 28 (9; Android SDK built for x86) ERROR
  DisconnectedClient disconnected from CONNECTED state (transport error)
NativeScript / 28 (9; Android SDK built for x86): Executed 1 of null (1 FAILED) ERROR (0.051 secs / 0 secs)

@ochemerys
Copy link
Author

ochemerys commented Dec 14, 2018

I think the problem is in a new angular project structure not being able to work together with test runner.
In my project with old structure tests work perfectly well, but when I generated a new structure , tests ends with posted above error on both platforms Android and iOS.

Does anybody use unit tests for new angular project structure?

Thank you.

@ochemerys
Copy link
Author

A little bit digging:
issue 1:
It looks like tsc version above 2 (I have 2.6.2) adds the line
Object.defineProperty(exports, "__esModule", { value: true });
if tsconfig.json compilerOptions has
"module": "commonjs"
so global variable exports should already exist in this case before executing js code and
tns run
treats the generated line above correctly (I'm able to run my app without errors)
but
tns test
does not treat it correctly and I have an error:

14 12 2018 12:19:42.611:WARN [NativeScript / 28 (9; Android SDK built for x86)]: Adapter did not report total number of specs.
NativeScript / 28 (9; Android SDK built for x86) /base/src/tests/example.spec.js?956564ba5d8e3e6acba577ca831a46495058d869 at line 0 FAILED
ReferenceError: exports is not defined

issue 2:
if i modify generated test case js file by adding
var exports = {};
at the top of generated file
I am getting different error:

14 12 2018 12:26:33.000:WARN [NativeScript / 28 (9; Android SDK built for x86)]: Adapter did not report total number of specs.
NativeScript / 28 (9; Android SDK built for x86) /base/src/tests/example.spec.js?22508e0d27feaa1a9e3dcff727d04712fa0fc0ef at line 0 FAILED
Error: com.tns.NativeScriptException: Failed to find module: "../app/app.component", relative to: app//
com.tns.Module.resolvePathHelper(Module.java:146)
com.tns.Module.resolvePath(Module.java:55)
com.tns.Runtime.callJSMethodNative(Native Method)
com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1120)
com.tns.Runtime.callJSMethodImpl(Runtime.java:1000)
com.tns.Runtime.callJSMethod(Runtime.java:987)
com.tns.Runtime.callJSMethod(Runtime.java:967)
com.tns.Runtime.callJSMethod(Runtime.java:959)
com.tns.gen.java.lang.Runnable.run(Runnable.java:15)
android.os.Handler.handleCallback(Handler.java:873)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:193)
android.app.ActivityThread.main(ActivityThread.java:6669)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

it looks like now
tns test
treats app folder as a root folder for tests which is correct for angular projects old structure but it is different for new structure where both app and tests folders are the same level and under src folder.

Hope it will help

@Fatme
Copy link
Contributor

Fatme commented Dec 15, 2018

@ochemerys,

As a temporary workaround I can suggest you to modify manually ./node_modules/nativescript-unit-test-runner/main-view-model.js file. You need the following changes:

  1. https://github.com/NativeScript/nativescript-unit-test-runner/blob/master/main-view-model.ts#L228 should be replaced with
var appPrefix = '/base/src/';
  1. https://github.com/NativeScript/nativescript-unit-test-runner/blob/master/main-view-model.ts#L282 should be replaced with
return url.startsWith('/base/src/tests/') || !url.startsWith('/base/src/');

After that you need to execute tns test android --justlaunch

@ochemerys
Copy link
Author

Thank you.
It works now on both platforms with --justlaunch flag.
If I run on iOS without the flag test runs correctly but displays an error at the end

CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:212:24: NSUTR: beginning test run
NativeScript / 12.1 (12.1; iPhone): Executed 1 of 2 SUCCESS (0 secs / 0.001 secs)
NativeScript / 12.1 (12.1; iPhone): Executed 2 of 2 SUCCESS (0.025 secs / 0.001 secs)
TOTAL: 2 SUCCESS
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:258:24: NSUTR: completeAck
NativeScript / 12.1 (12.1; iPhone) ERROR
  DisconnectedClient disconnected from CONNECTED state (transport error)
NativeScript / 12.1 (12.1; iPhone): Executed 2 of 2 SUCCESS (0.025 secs / 0.001 secs)

If I run on Android without the flag test runs but displays an error at the end and output is slightly different from iOS:

NativeScript / 28 (9; Android SDK built for x86): Executed 2 of 2 SUCCESS (0.054 secs / 0 secs)
TOTAL: 2 SUCCESS
JS: NSUTR: completeAck
15 12 2018 10:33:01.331:WARN [NativeScript / 28 (9; Android SDK built for x86)]: Disconnected (0 times)Client disconnected from CONNECTED state (transporNativeScript / 28 (9; Android SDK built for x86) ERROR
  DisconnectedClient disconnected from CONNECTED state (transport error)
NativeScript / 28 (9; Android SDK built for x86): Executed 2 of 2 SUCCESS (0.054 secs / 0 secs)
I hope those fixes will be included into test runner

on Android there is no output lines

NativeScript / 12.1 (12.1; iPhone): Executed 1 of 2 SUCCESS (0 secs / 0.001 secs)
NativeScript / 12.1 (12.1; iPhone): Executed 2 of 2 SUCCESS (0.025 secs / 0.001 secs)
TOTAL: 2 SUCCESS

Thanks again

I hope the fix will be included into next release of test runner

@ochemerys
Copy link
Author

ochemerys commented Dec 15, 2018

Hi,
we still missing something here
when I added
import { expect } from 'chai';
my test fails again with error message:

5 12 2018 15:02:23.006:WARN [NativeScript / 28 (9; Android SDK built for x86)]: Adapter did not report total number of specs.
NativeScript / 28 (9; Android SDK built for x86) ../../tests/example.spec.js at line 0 FAILED
Error: com.tns.NativeScriptException: Failed to find module: "chai", relative to: app/tns_modules/
com.tns.Module.resolvePathHelper(Module.java:146)
com.tns.Module.resolvePath(Module.java:55)
com.tns.Runtime.callJSMethodNative(Native Method)
com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1120)
com.tns.Runtime.callJSMethodImpl(Runtime.java:1000)
com.tns.Runtime.callJSMethod(Runtime.java:987)
com.tns.Runtime.callJSMethod(Runtime.java:967)
com.tns.Runtime.callJSMethod(Runtime.java:959)
com.tns.gen.java.lang.Runnable.run(Runnable.java:15)
android.os.Handler.handleCallback(Handler.java:873)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:193)
android.app.ActivityThread.main(ActivityThread.java:6669)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
NativeScript / 28 (9; Android SDK built for x86): Executed 1 of null (1 FAILED) ERROR (0.056 secs / 0 secs)
Test run failed.

it looks like test runner is not able to find installed chai npm package

@ochemerys
Copy link
Author

is there any workaround there?

@dldinza-web
Copy link

@ochemerys
The project is missing chai typescript. So that it will be able to load expect from chai

npm install @types/chai --save-dev

@ochemerys
Copy link
Author

@dldinza-web
Thank you for suggestion
I had @types/chai installed, the problem is with a new structure of angular project and test runner,
to reproduce the issue you can follow #4244
It is not resolved yet. I hope the fix will be included into test runner new release soon.

@Fatme Fatme added this to the 5.2.0 milestone Jan 30, 2019
@Fatme Fatme self-assigned this Jan 30, 2019
@dtopuzov dtopuzov self-assigned this Jan 31, 2019
@dtopuzov
Copy link
Contributor

I see two issues reported above:

  1. Unit testing do not work on new NG projects (because nsconfig is not respected)
  2. tns run after tns test require platform clean

Issue #1 is fixed in [email protected] and nativescript@next (will be shipped in 5.2.0).

Issue #2 is duplicate of #3146 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants