vue.jsurlvitesnapshotvitest

Vue / Vitest : issues in snapshots with dynamic import assets URL


I am migrating my project from Vue-CLI & Jest to Vite & Vitest. Everything worked fine until I tried to make my tests pass through Jenkins. I discovered that some snapshots does not match because of some assets paths.

These assets are imported in my .vue files using return new URL("/src/...", import.meta.url).href.

Of course everything works perfectly locally.

Do you have any idea why, and how I can fix it?

Thanks!


Solution

  • Edit: I found why. I was running my local tests on Windows, but the Jenkins ones on Linux. I solved it by using process.env.VITEST env variable:

    export function setDynamicUrl(media: string) {
        return process.env.VITEST
            ? media
            : new URL(`/src/assets/${media}`, import.meta.url).href;
    }