Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run unit tests without generating assets when using django-webpack-loader

Tags:

webpack

django

I'm working on a Django project with some amount of front-end JavaScript code. I'm in the process of migrating the JavaScript packaging and minification from djanog-pipeline to webpack with django-webpack-loader.

django-webpack-loader works by running webpack separate from any Django processes to generate packed bundles. django-webpack-loader will then read a JSON file written by webpack-bundle-tracker and use the information to insert the correct paths into HTML templates.

This works flawlessly but there is one catch: Some of our unit tests will access the Django application using the integrated Django test client, which renders full HTML responses so that tests can inspect the generated result. Test may be run without any webpack-related setup having been done. So the packed bundles and JSON file may not exist. These are not necessary for the purpose of testing the frontend code, only the dynamically generated HTML is inspected. Having test fail just because someone forgot to run webpack leads to frustration.

Ideally, I would have django-webpack-loader just use dummy URLs in the inserted <script> tags while running tests, removing the dependency on files generated by webpack. What options do I have to resolve this dependency?

like image 313
Feuermurmel Avatar asked Apr 27 '17 14:04

Feuermurmel


People also ask

How to add unit testing to your Django project?

How To Add Unit Testing to Your Django Project Step 1 — Adding a Test Suite to Your Django Application. A test suite in Django is a collection of all the test cases in... Step 2 — Testing Your Python Code. In this step, you will test the logic of the code written in the models.py file. In... Step 3 ...

Can I use WebPack with Django?

Most approaches to using webpack with Django work until the JavaScript app is tiny. What happens when the bundle grows? These days I'm seeing new tutorials on using webpack with Django popping up. Like: Using Webpack with Django: no plugins required!

Why are my tests not running in Django?

If you don’t have an optional dependency installed, the tests that require it will be skipped. Running the tests requires a Django settings module that defines the databases to use. To help you get started, Django provides and uses a sample settings module that uses the SQLite database.

What is the output directory of the Django test report?

When running coverage for the Django tests, the included .coveragerc settings file defines coverage_html as the output directory for the report and also excludes several directories not relevant to the results (test code or external code included in Django).


2 Answers

And if you want to do it at a higher level for all tests ran by pytest, you can add to your conftest.py:

@fixture(autouse=True)
def no_webpack_loaded(monkeypatch):
    def mockreturn(loader, bundle_name):
        return []
    monkeypatch.setattr(WebpackLoader, "get_bundle", mockreturn)
like image 134
Rmatt Avatar answered Oct 04 '22 20:10

Rmatt


You can patch the loader altogether to bypass anything that django-webpack-loader might do:

from mock import patch

...

@patch('webpack_loader.loader.WebpackLoader.get_bundle')
def test(self, mock_wpl, client):
    mock_wpl.return_value = []
    response = client.get("/")
    assert response.status_code == 200
like image 32
Susanne Peng Avatar answered Oct 04 '22 20:10

Susanne Peng