I've done some Cordova plugins and I would like to write unit tests on it. The idea is that, after I run cordova build android, for example, the test files would be moved to the correct folder on Android and I could run tests with Java. Is that possible?
I've seen some plugins to make possible to test a Cordova plugin, like cordova plugin test framework. The problem here is that it only tests the call and the callback. It's not possible to mock objects, for example. For instance, I have a plugin to track GPS and I would like to test it.
I also would like to test the iOS side as well.
You were right in your research that the current tests for the Cordova Core plugins are all using cordova-plugin-test-framework to implement tests that are run on the device with e.g. cordova-paramedic or cordova-mobile-spec (see the CI configuration in the GitHub repositories on how to trigger a build with cordova-paramedic for example).
The only Cordova projects where "normal" unit tests, written in Java for Android and Obj-C for iOS, are used are the platforms. For Android for example this folder exists: https://github.com/apache/cordova-android/tree/master/test These tests can be run via run_java_unit_tests.js, which is also available via npm as npm run java-unit-tests.
I am pretty sure the same concept could be applied to plugin tests.
(Please let me know if you try and this actually works!)
来源:https://stackoverflow.com/questions/51650827/how-to-unit-test-a-cordova-plugin-using-junit