Reputation: 9922
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?
Upvotes: 5
Views: 604
Reputation: 887
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
Upvotes: 4
Reputation: 1347
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)
Upvotes: 5