You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
unit testing for freshly created angular project is broken on android simulator #4232
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
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
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)
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?
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.
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:
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
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
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
@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.
Activity
Fatme commentedon Dec 13, 2018
@ochemerys,
You need to add
android:usesCleartextTraffic="true"inApp_Resources/android/AndroidManifest.xml.ochemerys commentedon Dec 13, 2018
Thank you.
When I added suggested line to
App_Resources/Android/src/main/AndroidManifest.xmlI was able to run example test
but when I created a simple typescript test:
the run fails
ochemerys commentedon 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 commentedon Dec 14, 2018
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
exportsshould already exist in this case before executing js code andtns runtreats the generated line above correctly (I'm able to run my app without errors)
but
tns testdoes not treat it correctly and I have an error:
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:
it looks like now
tns testtreats 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 commentedon Dec 15, 2018
@ochemerys,
As a temporary workaround I can suggest you to modify manually
./node_modules/nativescript-unit-test-runner/main-view-model.jsfile. You need the following changes:After that you need to execute
tns test android --justlaunchochemerys commentedon Dec 15, 2018
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
If I run on Android without the flag test runs but displays an error at the end and output is slightly different from iOS:
on Android there is no output lines
Thanks again
I hope the fix will be included into next release of test runner
ochemerys commentedon Dec 15, 2018
Hi,
we still missing something here
when I added
import { expect } from 'chai';my test fails again with error message:
it looks like test runner is not able to find installed chai npm package
ochemerys commentedon Dec 16, 2018
is there any workaround there?
dldinza-web commentedon Jan 9, 2019
@ochemerys
The project is missing chai typescript. So that it will be able to load
expectfrom chainpm install @types/chai --save-devochemerys commentedon Jan 10, 2019
@dldinza-web
Thank you for suggestion
I had
@types/chaiinstalled, 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.
dtopuzov commentedon Jan 31, 2019
I see two issues reported above:
tns runafter tns test requireplatform cleanIssue #1 is fixed in
nativescript-unit-test-runner@0.4.1andnativescript@next(will be shipped in 5.2.0).Issue #2 is duplicate of #3146 (comment)