5

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

9
  • 1
    That's a tough spot to be in (in there myself). IMO, you really need to test both, for the JS you probably need something like PhantomJS to test the actual JavaScript output from the HTTP Request. Commented Jun 5, 2012 at 18:12
  • 7
    "we were now inline our Javascript code within PHP and not even using Javascript files any longer." ... wow ... who thought that was a good idea???? Commented Jun 5, 2012 at 18:24
  • Aknosis - I'll look into PhantomJS and a PHP Unit Testing framework, thanks for your response! Commented Jun 5, 2012 at 18:31
  • rdlowrey- It seems like a couple people around the office have been saying this! Everything seems to be working ok outside of this issue so far though. Commented Jun 5, 2012 at 18:38
  • 2
    We've decided to split the PHP and Javascript, a prudent decision I'd say :) Thank you for the input JMC! Commented Jun 6, 2012 at 15:57

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.