Skip to content

Unit tests fail because webpack-stats.json don't exists #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
srtab opened this issue Dec 27, 2018 · 9 comments · Fixed by #337
Closed

Unit tests fail because webpack-stats.json don't exists #187

srtab opened this issue Dec 27, 2018 · 9 comments · Fixed by #337

Comments

@srtab
Copy link

srtab commented Dec 27, 2018

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

@srtab srtab changed the title Error running unit tests Unit tests fail because webpack-stats.json don't exists Dec 27, 2018
@jwerardi
Copy link

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.

@srtab
Copy link
Author

srtab commented Feb 15, 2019

@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": []}}

@macecchi
Copy link

macecchi commented Mar 5, 2019

@srtab Would you mind sharing how you did to change the STATS_FILE setting to webpack-stats-test.json only during the tests?

@srtab
Copy link
Author

srtab commented Mar 6, 2019

@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')
    }
}
...

@macecchi
Copy link

macecchi commented Mar 6, 2019

Thanks @srtab! I was trying with override_settings but with no success. I'll try setting up a new file :)

@joaopslins
Copy link
Contributor

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.

@hyshka
Copy link
Contributor

hyshka commented Aug 18, 2021

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'}]

@browniebroke
Copy link
Contributor

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?

@fjsj
Copy link
Member

fjsj commented Feb 4, 2023

Thanks for the report @browniebroke. Yes, please feel free to open a PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants