0

I have a django application with all js and jquery modules embedded in django templates between 'script' tags,

{% extends "myfile/base.html" %} 
  {% load static %}
  {% block content %}
  <!doctype html>
  <html>
  <body>
   <h2>Welcome</h2>
   <button id="mybtn" ></button>
  </body>
  <script type="text/javascript">
    $("#mybtn").on("click",function(){
     //process some user data
      //manipulate DOM elements
     }
    function myFun(par1,par2){
       return par1+par2;
    }
   </script>
   </html>
  {% endblock %}

i wanted to write Unit Test cases for the above jquery and js functions, is it possible to do with this kind of template or do i need to separate the js from this template in to a '.js' file,and load that js in to this template? Is it valid to perform UT for js in django?

Lisa
  • 655
  • 3
  • 10
  • 34
  • If you want to use a javascript framework to unit test your javascript functions, you'll need to move them to a separate js file (and maybe have some pure html files, not django templates, that use them). You can also use selenium to test the pages rendered by django including the javascript, for functional tests (rather than unit tests). – dirkgroten Dec 16 '19 at 16:49
  • yes, i have separated all the js from these html files and loaded the js file in a test.js script, but there comes another issue saying $ is not defined, since the js file contains jquery along with js and this test script is unable to recognize the '$' – Lisa Dec 17 '19 at 05:09
  • No, it’s indeed not that simple. What framework are you using to run your js tests? Mocha? To be frank, looking at the type of js you’re testing, I would not go for unit testing but only functional testing with selenium. Full unit testing of you js makes more sense if you’re using a full-fledged framework on the front-end like angular or react. – dirkgroten Dec 17 '19 at 07:23
  • yes, i am using mocha framework. – Lisa Dec 17 '19 at 07:36
  • Maybe look at [this](https://gist.github.com/robballou/9ee108758dc5e0e2d028) or [this](https://stackoverflow.com/questions/38590957/how-to-fix-is-not-defined-error-when-unit-testing-jquery-with-typescript-usi) – dirkgroten Dec 17 '19 at 10:57

0 Answers0