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 when import a package from node_modules #4244
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'
],
the same issue on iOS and Android devices
Describe the bug
unit testing for freshly created angular project is broken when import a package from node_modules
To Reproduce
I followed steps for workarounds provided in #4232
then I added import { expect } from 'chai';
my test fails 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
I commented out "chai" and tried "sinon" instead.
The same result
Expected behavior
I should be able to use import statements for node packages
changed the title [-]unit testing for freshly created angular project is broken when import a package from node_modulesmodule[/-][+]unit testing for freshly created angular project is broken when import a package from node_modules[/+]on Dec 21, 2018
chai module is added as devDependency to your project. This is the reason why it is not included in the built package and the reason to see the following error Failed to find module: "chai", relative to: app/tns_modules/.
Actually you don't need to import the chai module. Karma runner cares to automatically inject the framework (in your case chai and mocha) into tests. This way the example test below works out of the box without any import.
// A sample Mocha test
describe('Array', function () {
describe('#indexOf()', function () {
it('should return -1 when the value is not present', function () {
assert.equal(-1, [1,2,3].indexOf(5));
assert.equal(-1, [1,2,3].indexOf(0));
});
});
});
We released a new version of the nativescript-unit-test-runner package where the issue with nsconfig file is fixed. (the workaround mentioned here #4232 (comment)).
In order to test it on your side, you need the following:
Hi @Fatme
I added import chai just to see how test runner works with import statement
after running npm i nativescript-unit-test-runner
test:
// A sample Mocha test
describe('Array', function () {
describe('#indexOf()', function () {
it('should return -1 when the value is not present', function () {
chai.assert.equal(-1, [1,2,3].indexOf(5));
chai.assert.equal(-1, [1,2,3].indexOf(0));
});
});
});
@Fatme
thank you, I added android:usesCleartextTraffic=“true” (just forgot to check if it is there) and now it works fine if there is no import statement
when i added import {appComponent} from '../app/app.component' it shows me no reachable hosts again.
After running tns platform clean android and tns test android it gives me
@Fatme
Thank you,
I updated my code accordingly your suggestion.
After running tns platform clean android and tns test android test is successful
but when I modified code (var is replaced with const ) and saved i got a message
no reachable hosts
I returned back to var declaration and still the same until I run tns platform clean android
@Fatme
After running tns platform clean android
and my code
var AppComponent = require('../app/app.component');
// A sample Mocha test //
describe('Array', function () {
describe('#indexOf()', function () {
it('should return -1 when the value is not present', function () {
chai.assert.equal(-1, [1,2,3].indexOf(5));
chai.assert.equal(-1, [1,2,3].indexOf(0));
});
});
});
after tns test android
fails with error :
03 02 2019 10:42:58.328: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?2200c3f5880411d7cf4a73aab7d0f13bc31cf409 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)
NativeScript / 28 (9; Android SDK built for x86): Executed 1 of null (1 FAILED) ERROR (0.056 secs / 0 secs)
JS: NSUTR: completeAck
JS: NSUTR-socket.io: io server disconnect
Test run failed.
@Fatme
I have nativescript latest version on today tns doctor returns
✔ 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.1 version and is up to date.
✔ Component tns-core-modules has 5.1.2 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.1 version and is up to date.
steps to reproduce
tns create tns-ng-mocha --template tns-template-hello-world-ng cd tns-ng-mocha npm install tns test init and select mocha
add to AndroidManifest.xml
<application
android:usesCleartextTraffic="true"
run tns test android test is success
remove example.js file and add example.spec.ts file into tests folder
var AppComponent = require('../app/app.component');
// A sample Mocha test //
describe('Array', function () {
describe('#indexOf()', function () {
it('should return -1 when the value is not present', function () {
chai.assert.equal(-1, [1,2,3].indexOf(5));
chai.assert.equal(-1, [1,2,3].indexOf(0));
});
});
});
run tns test android again and test fails with error:
JS: NSUTR: this.error: Error: com.tns.NativeScriptException: Failed to find module: "../app/app.component", relative to: app//
JS: com.tns.Module.resolvePathHelper(Module.java:146)
JS: com.tns.Module.resolvePath(Module.java:55)
JS: com.tns.Runtime.callJSMethodNative(Native Method)
JS: com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1120)
JS: com.tns.Runtime.callJSMethodImpl(Runtime.java:1000)
JS: com.tns.Runtime.callJSMethod(Runtime.java:987)
JS: com.tns.Runtime.callJSMethod(Runtime.java:967)
JS: com.tns.Runtime.callJSMethod(Runtime.java:959)
JS: com.tns.gen.java.lang.Runnable.run(Runnable.java:15)
JS: android.os.Handler.handleCallback(Handler.java:873)
JS: android.os.Handler.dispatchMessage(Handler.java:99)
JS: android.os.Looper.loop(Looper.java:193)
JS: android.app.ActivityThread.main(ActivityThread.java:6669)
JS: java.lang.reflect.Method.invoke(Native Method)
JS: com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
JS: com.android.internal.os.ZygoteInit.main(ZygoteInit.java...
JS: NSUTR: completed test run.
03 02 2019 13:36:01.957: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?9d9673dccee7326a40481232a03ed59b331d3c28 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)
NativeScript / 28 (9; Android SDK built for x86): Executed 1 of null (1 FAILED) ERROR (0.049 secs / 0 secs)
JS: NSUTR: completeAck
03 02 2019 13:36:02.567:WARN [NativeScript / 28 (9; Android SDK built for x86)]: Disconnected (0 times)Client disconnected from CONNECTNativeScript / 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.049 secs / 0 secs)
^CExecuting after-watch hook from /Users/olekschemerys/TRAINING/NativeScript/testing/tns-ng-mocha/hooks/after-watch/nativescript-dev-typescript.js
Stopping tsc watch
Executing after-watch hook from /Users/olekschemerys/TRAINING/NativeScript/testing/tns-ng-mocha/hooks/after-watch/nativescript-dev-webpack.js
Stopping webpack watch
Executing after-watch hook from /Users/olekschemerys/TRAINING/NativeScript/testing/tns-ng-mocha/hooks/after-watch/nativescript-dev-typescript.js
Executing after-watch hook from /Users/olekschemerys/TRAINING/NativeScript/testing/tns-ng-mocha/hooks/after-watch/nativescript-dev-webpack.js
Stopping webpack watch
@Fatme
thank you
I run npm i -g nativescript@next and tns update next
test:
import { AppComponent } from '../app/app.component';
// A sample Mocha test
describe('Array', function () {
describe('#indexOf()', function () {
it('should return -1 when the value is not present', function () {
chai.assert.equal(-1, [1,2,3].indexOf(5));
chai.assert.equal(-1, [1,2,3].indexOf(0));
});
});
});
now gives SUCCESS. Then I modified test to be sure that npm_modules are accessible:
import { expect } from 'chai';
import { AppComponent } from '../app/app.component';
// A sample Mocha test
describe('Array', function () {
describe('#indexOf()', function () {
it('should return -1 when the value is not present', function () {
expect(true, "testing ...");
// chai.assert.equal(-1, [1,2,3].indexOf(5));
// chai.assert.equal(-1, [1,2,3].indexOf(0));
});
});
});
and test fails with error
JS: NSUTR: completed test run.
04 02 2019 19:42:34.086: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:1203)
com.tns.Runtime.callJSMethodImpl(Runtime.java:1083)
com.tns.Runtime.callJSMethod(Runtime.java:1070)
com.tns.Runtime.callJSMethod(Runtime.java:1050)
com.tns.Runtime.callJSMethod(Runtime.java:1042)
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.05 secs / 0 secs)
JS: NSUTR: completeAck
04 02 2019 19:42:34.707:WARN [NativeScript / 28 (9; Android SDK built for x86)]: Disconnected (0 times)Client disconnected from CONNECTNativeScript / 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.05 secs / 0 secs)
chai package is added as devDependency to your project. This means that NativeScript CLI will NOT prepare it and it will NOT be included in the built package. So this means that chai package will not be transferred to your device or emulator which means that the error Failed to find module: "chai", relative to: app/tns_modules/ in runtime is expected.
If you want to test some import from node_modules, you need to use some package that is dependency of your project, not devDependency.
NOTE: NativeScript CLI prepares only packages that are dependencies of your project. devDependencies are not prepared and are not included in the built package.
@Fatme
Thank you very much for clarification
I installed nativescript-toast package, imported it to my test and ran the test. Test was successful.
Thanks.
Activity
[-]unit testing for freshly created angular project is broken when import a package from node_modulesmodule[/-][+]unit testing for freshly created angular project is broken when import a package from node_modules[/+]endarova commentedon Dec 21, 2018
Hi @ochemerys ,
Thanks for reporting this issue. I was able to reproduce it too. We will have to investigate it.
feat: respect nsconfig file from project and correctly navigate to ru…
feat: respect nsconfig file from project and correctly navigate to ru…
fix: respect nsconfig file on `tns test` command
Fatme commentedon Jan 30, 2019
Hi @ochemerys,
Sorry for the late reply.
chaimodule is added asdevDependencyto your project. This is the reason why it is not included in the built package and the reason to see the following errorFailed to find module: "chai", relative to: app/tns_modules/.Actually you don't need to import the
chaimodule.Karma runnercares to automatically inject the framework (in your casechaiandmocha) into tests. This way the example test below works out of the box without any import.We released a new version of the
nativescript-unit-test-runnerpackage where the issue withnsconfigfile is fixed. (the workaround mentioned here #4232 (comment)).In order to test it on your side, you need the following:
Let us know if it works for you.
dtopuzov commentedon Jan 31, 2019
Fixed in
nativescript-unit-test-runner@0.4.1ochemerys commentedon Feb 2, 2019
Hi @Fatme
I added import chai just to see how test runner works with import statement
after running
npm i nativescript-unit-test-runnertest:
still fails for me with message
no reachable hoston android device emulator-5554
Fatme commentedon Feb 2, 2019
@ochemerys,
Do you have
android:usesCleartextTraffic=“true”in AndroidManifest.xml? It is mandatory for emulators with api level 28.ochemerys commentedon Feb 2, 2019
@Fatme
thank you, I added
android:usesCleartextTraffic=“true”(just forgot to check if it is there) and now it works fine if there is no import statementwhen i added
import {appComponent} from '../app/app.component'it shows meno reachable hostsagain.After running
tns platform clean androidandtns test androidit gives mewith
and Test run failed.
Fatme commentedon Feb 3, 2019
@ochemerys,
ochemerys commentedon Feb 3, 2019
@Fatme
Thank you,
I updated my code accordingly your suggestion.
After running
tns platform clean androidandtns test androidtest is successfulbut when I modified code (var is replaced with const ) and saved i got a message
no reachable hosts
I returned back to var declaration and still the same until I run
tns platform clean androidochemerys commentedon Feb 3, 2019
@Fatme
After running
tns platform clean androidand my code
after
tns test androidfails with error :
ochemerys commentedon Feb 3, 2019
@Fatme
I have nativescript latest version on today
tns doctorreturnssteps to reproduce
tns create tns-ng-mocha --template tns-template-hello-world-ngcd tns-ng-mochanpm installtns test initand select mochaadd to AndroidManifest.xml
run
tns test androidtest is successremove example.js file and add example.spec.ts file into tests folder
run
tns test androidagain and test fails with error:Should I do something else to run tests?
Fatme commentedon Feb 4, 2019
@ochemerys,
Can you please try with next version of
nativescriptochemerys commentedon Feb 5, 2019
@Fatme
thank you
I run
npm i -g nativescript@nextandtns update nexttest:
now gives SUCCESS. Then I modified test to be sure that npm_modules are accessible:
and test fails with error
Fatme commentedon Feb 5, 2019
@ochemerys,
chaipackage is added asdevDependencyto your project. This means that NativeScript CLI will NOT prepare it and it will NOT be included in the built package. So this means thatchaipackage will not be transferred to your device or emulator which means that the errorFailed to find module: "chai", relative to: app/tns_modules/in runtime is expected.If you want to test some import from
node_modules, you need to use some package that isdependencyof your project, notdevDependency.ochemerys commentedon Feb 6, 2019
@Fatme
Thank you very much for clarification
I installed nativescript-toast package, imported it to my test and ran the test. Test was successful.
Thanks.