MrBin99 / django-vite

Integration of ViteJS in a Django project.
Apache License 2.0
560 stars 72 forks source link

Not working with StaticLiveServerTestCase #95

Open ipeterov opened 12 months ago

ipeterov commented 12 months ago

I wrote Playwright tests for my web app that uses django-vite. Currently, I have to run manage.py collectstatic every time I want to run my tests (if the frontend changed).

Django has a special tool for this use case - StaticLiveServerTestCase. It serves the static files right from STATICFILES_DIRS, allowing me to just run the tests without doing manage.py collectstatic.

When I'm running the tests, they are failing with a django-vite error:

Cannot find src/main.jsx in Vite manifest at /code/static/manifest.json

It looks like the error is originating here. I checked with a debugger, and self._manifest was None.

It would be cool to be able to use this Django feature with django-vite. How can I help to resolve/investigate the issue?

Niicck commented 12 months ago

During your playwright test execution, you'll want to adjust your DJANGO_VITE_MANIFEST_PATH and DJANGO_VITE_STATIC_URL_PREFIX.

By default, with DEV_MODE=False, django-vite will expect to look for your manifest.json in your STATIC_ROOT, which I'm guessing you've set to /code/static/. But your manifest.json won't be there if haven't run collectstatic.

So you're going to need to set your DJANGO_VITE_MANIFEST_PATH to the location in STATICFILES_DIR where your compiled vite assets initially lived before then get collected into your STATIC_ROOT. See if that solves your self._manifest being set to None. That's step 1.

Step 2 will be probably be the problem of your individual vite_assets not being discovered since they're not in STATIC_ROOT either. manifest.json gives the related paths of each of your vite_asset entries, but since they aren't in your STATIC_ROOT, the vite_asset tag may not know where they live.

If your assets don't get loaded after you've fixed your manifest_path, then you need to adjust DJANGO_VITE_STATIC_URL_PREFIX to point to the same root directory as your DJANGO_VITE_MANIFEST_PATH -- the place they live in STATICFILES_DIR before they get collected.

Let me know how that works out.