-
Notifications
You must be signed in to change notification settings - Fork 84
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
Vitest failing when baseURL is defined at nuxt.config.ts #865
Comments
Hello there, @leopradac! I'm here to help you with any bugs, questions, or contributions. Let's work together to solve the issue you're facing. To resolve the issue where Vitest fails when the In the import { joinURL } from 'ufo';
export default <Environment>{
name: 'nuxt',
transformMode: 'web',
async setup(global, environmentOptions) {
const url = joinURL('http://localhost:3000', environmentOptions?.nuxtRuntimeConfig.app?.baseURL || '/');
const environmentName = environmentOptions.nuxt.domEnvironment as NuxtBuiltinEnvironment;
const environment = environmentMap[environmentName] || environmentMap['happy-dom'];
const { window: win, teardown } = await environment(global, defu(environmentOptions, {
happyDom: { url },
jsdom: { url },
}));
win.__NUXT_VITEST_ENVIRONMENT__ = true;
win.__NUXT__ = {
serverRendered: false,
config: {
public: {},
app: { baseURL: '/' },
...environmentOptions?.nuxtRuntimeConfig,
},
data: {},
state: {},
};
const app = win.document.createElement('div');
app.id = environmentOptions.nuxt.rootId;
win.document.body.appendChild(app);
if (environmentOptions?.nuxt?.mock?.intersectionObserver) {
win.IntersectionObserver = win.IntersectionObserver || class IntersectionObserver {
observe() {}
unobserve() {}
disconnect() {}
};
}
if (environmentOptions?.nuxt?.mock?.indexedDb) {
win.indexedDB = indexedDB;
}
const h3App = createApp();
if (!win.fetch) {
await import('node-fetch-native/polyfill');
win.URLSearchParams = globalThis.URLSearchParams;
}
const localCall = createCall(toNodeListener(h3App));
const localFetch = createLocalFetch(localCall, win.fetch);
const registry = new Set<string>();
win.fetch = (init, options) => {
if (typeof init === 'string') {
const base = init.split('?')[0];
if (registry.has(base) || registry.has(init)) {
init = '/_' + init;
}
}
return localFetch(init.toString(), {
...options,
headers: Array.isArray(options?.headers) ? new Headers(options?.headers) : options?.headers,
});
};
win.$fetch = createFetch({ fetch: win.fetch, Headers: win.Headers });
win.__registry = registry;
win.__app = h3App;
const { keys, originals } = populateGlobal(global, win, {
bindFunctions: true,
});
const timestamp = Date.now();
const routeRulesMatcher = toRouteMatcher(
createRadixRouter({ routes: environmentOptions.nuxtRouteRules || {} }),
);
const matcher = exportMatcher(routeRulesMatcher);
const manifestOutputPath = joinURL(
'/',
environmentOptions?.nuxtRuntimeConfig.app?.buildAssetsDir || '_nuxt',
'builds',
);
const manifestBaseRoutePath = joinURL('/_', manifestOutputPath);
h3App.use(
`${manifestBaseRoutePath}/latest.json`,
defineEventHandler(() => ({
id: 'test',
timestamp,
})),
);
h3App.use(
`${manifestBaseRoutePath}/meta/test.json`,
defineEventHandler(() => ({
id: 'test',
timestamp,
matcher,
prerendered: [],
})),
);
h3App.use(
`${manifestBaseRoutePath}/meta/dev.json`,
defineEventHandler(() => ({
id: 'test',
timestamp,
matcher,
prerendered: [],
})),
);
registry.add(`${manifestOutputPath}/latest.json`);
registry.add(`${manifestOutputPath}/meta/test.json`);
registry.add(`${manifestOutputPath}/meta/dev.json`);
return {
teardown() {
keys.forEach(key => delete global[key]);
originals.forEach((v, k) => (global[k] = v));
teardown();
},
};
},
}; This modification ensures that the |
I'm also having this problem and I can confirm that setting |
Environment
Reproduction
https://stackblitz.com/edit/github-ichjfp?file=nuxt.config.ts&view=editor
Describe the bug
Quite simple as the title describes, when
app > baseURL
is defined atnuxt.config.ts
vitest starts failing, at least at those tests using the nuxt environment with the// @vitest-environment nuxt
at the beginning of the file.Additional context
Logs shared below are for
"baseURL": "/admin/"
. Also, for those struggling with this, by the time being a workaround would be modifying the "test" script at package.json to:Logs
The text was updated successfully, but these errors were encountered: