unit test the javascript functions in my django application

I have a Django app, which contains backend logic in views.py and front end logic/user data processing written in javascript and jQuery inside HTML files. Now I wanted to write unit tests, can I use mocha-chai for doing this or any other alternative to do unit testing a JavaScript code in Django.

my directory structure

myapp
--->templates
----->base.html
----->home.html

base.html contains:

<html>
<body>
<h1>WElcome</h1>
<script>
function myfunc(){
console.log("welcome")
//some user logic 
}
</sctipt>

which framework to use in order to do write a unit test case for this 'myfunc' inside the Django template

1 answer

  • answered 2019-12-09 13:50 Risadinha

    We have tried the following:

    1. Use the Django testing framework as base
    2. extend it to use Nightwatch with a headless browser to run JS
    3. use Selenium for integration tests

    We failed, however, to get it to work completely because the headless browser did not run all of our JS code. This was some years ago, and the libraries surely have evolved since then.

    If you are happy with integration tests only:

    Setup the selenium with Django LiveServerTestCase and a headless browser that runs all of your JS code correctly.

    https://docs.djangoproject.com/en/2.2/topics/testing/tools/#liveservertestcase