Hello:
I've recently been faced with updating an old GS extension,
and I decided to use unit tests for it. The approach I took
involved some tooling to be able to test code using karma with
jasmine and PhantomJS, webpack was used to solve most of the
issues I encountered. This should work for any GJS application.
Following are some comments on what I did.
Repository of extension using unit tests:
https://github.com/emerinohdz/power-alt-tab
- GJS Imports mechanism is replaced with ES6 imports, by
defining the target library to UMD for webpack this shouldn't be
a problem. Webpack creates a single file with all the needed
configurations.
- Since we are using ES6 imports now, there's no problem while
testing. Karma has to be setup with support for webpack though.
- For unit testing to work fine, I had to include a GS polyfill
(test/polyfill_gs.js). This polyfill only covers what I needed,
the way I needed it, mostly mocking every global GS object
needed.
- Integration testing was not possible, this would be a huge
step forward. Providing some type of test API where things could
be integrated and tested before moving to production would mean
faster delivery of new functionality IMO. This is of course the
greatest challenge.
- Tests can be run on PhantomJS. For this to work, I had to use
a transpiler (babel) with webpack. This has the added benefit of
generating JS code that is backwards compatible between GS
versions, while allowing us to use ES6 features now.
- This was all integrated with Travis CI (see the github repo
for reference).
NOTE: I had to keep classes that extend native GS classes using
Lang.Class instead of ES6 classes, because there were some
problems with the transpiled code when calling super constructors.
If you are not using a transpiler for releases, all classes can be
refactored to ES6.
I've posted a similar email to the GSE mailing list, but I
thought it would be helpful for any GJS developer looking to
create quality code by applying TDD.
It'll be great to read your thoughts on this approach.
Regards.