Skip to content

application.android.startActivity and foregroundActivity are undefined in unit tests #4922

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
adrian-niculescu opened this issue Aug 1, 2019 · 4 comments
Assignees

Comments

@adrian-niculescu
Copy link

Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):

  • CLI: 6.0.2
  • Cross-platform modules: 6.0.4
  • Android Runtime: 6.0.1
  • iOS Runtime: 6.0.1

Describe the bug
import * as application from 'tns-core-modules/application';
application.android.startActivity and application.android.foregroundActivity are undefined in unit tests, not even after a few seconds after the app starts.

The problem is huge, because it affects all functionality that needs user permissions, as an activity is needed to request or check permissions on Android.

To Reproduce
Create an app from scratch:
tns create MyApp
tns test init

Write a test to check if application.android.startAcitivity or application.android.foregroundActivity exist.
Example: https://github.com/adrian-niculescu/NativeScriptUnitTestDemo/blob/master/app/tests/example.ts#L24

Expected behavior
application.android.startActivity and application.android.foregroundActivity to be defined.

Sample project
Sample project
It was created with:
tns create MyApp
tns test init
then added this mocha test

@DimitarTachev
Copy link
Contributor

Hi @adrian-niculescu,

After cloning your repository and executing tns test init and tns test android, the provided test is passing and both of the activities are printed on the console.

activitiesTest

Maybe you have some outdated git ignored files locally. Could you try cloning the provided repository in a new folder and running the test again?

@adrian-niculescu
Copy link
Author

Following the exact steps I replicated this on 3 different machines: 2 with MacOS and 1 Linux (CentOS 7).
Additional info about the environement

[centos7]$ node --version
v10.15.1

[macos]$ node --version
node --version
v10.16.0

$ npm --version
6.10.3

Screenshot 2019-08-08 at 18 11 06

@DimitarTachev
Copy link
Contributor

@adrian-niculescu

Thanks for the additional details.

We were able to reproduce the issue. It's caused by a wrong version of karma-webpack (it should be downgraded from 4.0.2 to 3.0.5). You could take a look at this issue for more details.

The issue is already fixed in nativescript@6.0.3, in order to fix it on your side, you could follow the steps below:

  1. npm i nativescript@latest -g
  2. tns test init (this will set the proper karma-webpack version)

@adrian-niculescu
Copy link
Author

Changing the version of karma-webpack to 3.0.5 solves the issue.
Thank you for your response.

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

No branches or pull requests

4 participants