Setting up unit tests

245 views
Skip to first unread message

Thanasis Polychronakis

unread,
May 27, 2012, 8:38:49 AM5/27/12
to Closure Library Discuss
We are prospecting to port our project's JS unit tests to the google's
closure library jsUnit testing facilities.

Is there a way we can have unit testing on compiled code? Our
reasoning behind this is to test that the compiled API of our
application (website) works after compilation, so as to make sure no
symbols were forgotten or any AJAX (or whatnot) communication will not
work...

We are also not very happy having to create stand alone .html files
for every .js file. Apart from polluting a pure JS folder structure,
it is a daunting task to upkeep so many static .html files... Plus, as
an application builder (and not a library one), we want to unit test
our JS base on the environment it is intended to run on, along with
all the other loaded libraries and classes polluting the JS
namespace...

The last point is especially important as a unit test may pass for a
specific JS class we built, however when thrown out in the wild with
all the other nasty kids who bully it and it's namespace we have
issues... It is important to know that before deploying our website to
live...


Nick Santos

unread,
May 29, 2012, 4:36:52 PM5/29/12
to closure-lib...@googlegroups.com
are there any actual obstacles to doing these things?

Daniel Steigerwald

unread,
May 29, 2012, 5:32:38 PM5/29/12
to closure-lib...@googlegroups.com
You do not need html google tests suit. You can use http://visionmedia.github.com/mocha/ (for example) and run tests in nodejs with mocked DOM. Much faster.

Peter StJ

unread,
May 30, 2012, 1:07:18 PM5/30/12
to closure-lib...@googlegroups.com
If the code touches any host object, I would not trust mocked environment. 

In case of a more procedural code we test using nclosure node module as a first line of defense (again without ever depending on host object, which is possible, but rare in client side development).

There is the concept of mocked objects, if a class behavior / correctness need to be tested, it is better to do mocking of the dependencies if possible.

Compiled tests: We do so by exporting the required symbols in a new namespace and then require in there the tested module. Then the module is compiled. It can then be included as a script tag and it will have the needed symbol available. Another approach (in case modules are already used in the project for example) is to include a new module with the tests inside and run the compiler with the module specified. Then in the main module require the test module. This will also be helpful with your other issue: the need to have the whole app running when the tests are run. 

As a whole the scenarios described are possible, but I would recommend rethinking the whole test story. If for example a test fails when the whole app is loaded in the browser, how would one determine the cause of the issue if only a single module is tested that depends on 10 others which in their turn depends on 30 others. On some projects the list of files pulled might be over a thousand. 

Daniel Steigerwald

unread,
May 30, 2012, 2:10:00 PM5/30/12
to closure-lib...@googlegroups.com
| If the code touches any host object, I would not trust mocked environment. 

True, but my business logic usually does not touch host objects. I use Google Closure for that stuff and it is well tested.

Rhys Brett-Bowen

unread,
Jun 4, 2012, 2:13:13 PM6/4/12
to Closure Library Discuss
I'm using Plovr and it's test runner. It uses a template file for the
tests that isn't in the same directory.

On May 27, 5:38 am, Thanasis Polychronakis <thanpo...@gmail.com>
wrote:

thanpolas

unread,
Jun 21, 2012, 2:57:10 PM6/21/12
to closure-lib...@googlegroups.com
none, except i don't know how... 

That was the purpose of this post, asking how to do it :)



On Tuesday, May 29, 2012 11:36:52 PM UTC+3, Nick Santos wrote:
are there any actual obstacles to doing these things?

On Sun, May 27, 2012 at 8:38 AM, Thanasis Polychronakis
Reply all
Reply to author
Forward
0 new messages