Skip to content

Commit c52d8ca

Browse files
browniebrokefjsj
authored andcommitted
Add a FakeWebpackLoader for running tests
1 parent d2f51b7 commit c52d8ca

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,11 @@ First we build the assets and, since we have `webpack-bundle-tracker` in our fro
180180
However, production usage for this package is **fairly flexible**. Other approaches may include keeping the production bundles in the version control and take that responsibility from the automatic pipeline. However, you must remember to always build the frontend and generate the bundle before pushing to remote.
181181

182182
## Usage in tests
183-
There are 2 approaches for when `render_bundle` shows up in tests, since we don't have `webpack-bundle-tracker` at that point to generate the stats file.
183+
To run tests where `render_bundle` shows up, since we don't have `webpack-bundle-tracker` at that point to generate the stats file, the calls to render the bundle will fail. The solution is to use the `FakeWebpackLoader` in your test settings:
184184

185-
1. The first approach is to have specific settings for them (which is how we approach on our [tests](https://github.com/django-webpack/django-webpack-loader/blob/master/tests/app/settings.py#L111-L125)), such as done [here](https://github.com/django-webpack/django-webpack-loader/issues/187#issuecomment-470055769). Please note that it's necessary to have a pre-made stats file for the tests (which in general can be empty, such as [here](https://github.com/django-webpack/django-webpack-loader/issues/187#issuecomment-464250721)).
186-
187-
2. The second approach is to leverage [`LOADER_CLASS` overriding](#extra-settings) for the test settings and customize the `get_bundle` method to return the url of a stats file. Note that, using this approach, the stats file doesn't have to [exist](https://github.com/django-webpack/django-webpack-loader/issues/187#issuecomment-901449290).
185+
```python
186+
WEBPACK_LOADER['DEFAULT']['LOADER_CLASS'] = 'webpack_loader.loader.FakeWebpackLoader'
187+
```
188188

189189
## Advanced Usage
190190
### Rendering by file extension

webpack_loader/loader.py

+16
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,19 @@ def get_bundle(self, bundle_name):
139139
"The stats file does not contain valid data. Make sure "
140140
"webpack-bundle-tracker plugin is enabled and try to run "
141141
"webpack again.")
142+
143+
144+
class FakeWebpackLoader(WebpackLoader):
145+
"""
146+
A fake loader to help run Django tests.
147+
148+
For running tests where `render_bundle` is used but assets aren't built.
149+
"""
150+
151+
def get_bundle(self, _bundle_name):
152+
return [
153+
{
154+
'name': 'test.bundle.js',
155+
'url': 'http://localhost/static/bundles/test.bundle.js',
156+
}
157+
]

0 commit comments

Comments
 (0)