Cordova / PhoneGap TDD: How to use Jasmine and Karma to test mobile app?
Asked Answered
A

1

9
To be able to develop in TDD
As a PhoneGap developer using Jasmine and Karma
I want to be able to run Jasmine spec files inside a PhoneGap application
So I can verify my Cordova specific code in my unit tests

Based on karma #472, it doesn't work for some/most people, but some claim it can be done.

What I'm trying to achieve is to have access to the Cordova environment (eg. FileSystem API, battery events, etc.) in my spec files/tests. (I do mock a lot of services by the way, but I'd like to access the real API)

I have tried to redirect the Cordova app to Karma runner page with window.location = 'http://karma:8080', and while I can load the base cordova.js file inside the tests, deviceready never fires (see ticket), plus I'm unable to grab the JS code that are required to have plugin functionality (ie. code depending on plugins can't be tested).

I've tried to create an <iframe> pointing to Karma inside the Cordova app, but that also couldn't access cordova.

I have found others are injecting cordova to window.parent, but I don't really see how that could work (I can't access window.parent from my <iframe>).

I have tried to do these in ripple-emulator and in Android AVDs, with the same lack of success.

Any pointers are really appreciated. I'm using Cordova 3.4.1 on Android (not targeting iOS), and latest Karma & Jasmine, plus AngularJS 1.3.

Archibald answered 12/5, 2014 at 22:8 Comment(3)
Did anything happen here for you?Aeolic
No, I never got any further.Archibald
I haven't done any Cordova stuff lately, so nope.Archibald
S
0

You could create two separate files index_real.html and index_test.html in your www folder. index_real.html is the index.html which you have currently. index_test.html would be instrumented with Jasmine version of index_real.html.

Create real.sh:

cp index_real.html index.html cordova run

and test.sh:

cp index_test.html index.html cordova run

Running real.sh will run your not instrumented version of application, and test.sh will run your test suite. If you setup index_test.html close to your index_real.html your will have UI automation tests, or if you will put standard Jasmine test runner you will have unit testing suite.

Sundin answered 9/12, 2014 at 2:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.