Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing Javascript Code Inlined Within PHP

At work I've been tasked with unit testing our Javascript code. I settled upon the Jasmine unit testing framework and began to learn how to use it when I was informed that we were now inline our Javascript code within PHP and not even using Javascript files any longer.

Can Jasmine work around this? Is there another framework that could? Should I be using a PHP unit test and a Javascript unit test? What would be my best approach?

Some example code from a fairly basic page to give you an idea:

http://pastebin.com/K3YP33id

http://pastebin.com/QaR8zUh1 <---submit

any sort of insight would be greatly appreciated

like image 209
Goose Avatar asked Nov 14 '22 04:11

Goose


1 Answers

Best practice is to split all PHP out of javascript and Unit test each separately. In the long run, it becomes a nightmare to maintain and test heavily integrated code. Try a modularize as much as possible.

For example, here's how I would do it:

var exampleVariables = {
    'name' = <?=$name?>,
    'occupation' = <?=$occupation?>
};

unitTestableFunction(exampleVariables);
like image 77
keithhackbarth Avatar answered Nov 16 '22 03:11

keithhackbarth