django-webpack-loader
django-webpack-loader copied to clipboard
Unit tests fail because webpack-stats.json don't exists
Hi,
I'm having problems running unit tests because webpack-stats.json don't exists in my CI, the file is not under version control along with the code. Do i need to add it? Is there a Test mode to avoid adding this file under VCS?
The feature requested in #137 could be a good solution, this makes possible creating a specific loader for testing purpose.
Tks, Sandro Rodrigues
I'm in the same boat as you, and I've found a 'fix' where I just make sure to run npm build before I run my unit tests. not great, but it works. I'm interested to see if someones come up with a better solution.
@jwerardi My work around was creating an empty webpack-stats-test.json with the essential just to make run the tests:
{"status":"done","publicPath":"https://127.0.0.1:4200/static/bundles/","chunks": {"main": []}}
@srtab Would you mind sharing how you did to change the STATS_FILE setting to webpack-stats-test.json only during the tests?
@macecchi I have specific settings for tests that are only loaded when running my tests.
Example of settings/test.py:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_DIR = os.path.abspath(os.path.join(BASE_DIR, '..'))
WEBPACK_LOADER = {
'DEFAULT': {
'STATS_FILE': os.path.join(PROJECT_DIR, 'static', 'webpack-stats-test.json')
}
}
...
Thanks @srtab! I was trying with override_settings but with no success. I'll try setting up a new file :)
Thanks @srtab, went through the same issue, glad I've found your workaround! I think it would be useful to have this in the README.
For others that may have a lot of different entry points in webpack which change frequently and just want their tests to not blowup when encountering the render_bundle template tag, I overrode the LOADER_CLASS as per the readme with this:
from webpack_loader.loader import WebpackLoader
class TestingWebpackLoader(WebpackLoader):
def get_bundle(self):
"""
Return a non-existent JS bundle for each one requested.
Django webpack loader expects a bundle to exist for each one
that is requested via the 'render_bundle' template tag. Since we
don't want to store generated bundles in our repo, we need to
override this so that the template tags will still resolve to
something when we're running tests.
The name and URL here don't matter, this file doesn't need to exist.
"""
return [{'name': 'test.bundle.js', 'url': 'http://localhost:8000/static/bundles/test.bundle.js'}]
Creating a fake stat file for the tests didn't work for me, the default WebpackLoader raises an exception:
if assets.get('status') == 'done':
chunks = assets['chunks'].get(bundle_name, None)
if chunks is None:
> raise WebpackBundleLookupError('Cannot resolve bundle {0}.'.format(bundle_name))
E webpack_loader.exceptions.WebpackBundleLookupError: Cannot resolve bundle project.
webpack_loader/loader.py:114: WebpackBundleLookupError
I created a TestingWebpackLoader as per the example above and it worked like a charm, thanks for the solution 👍🏻
Would it make sense to include such a loader as part of this library to help users with testing?
Thanks for the report @browniebroke. Yes, please feel free to open a PR!