Open nopeless opened 3 months ago
Hey, @nopeless! I'm here to help you with bugs, answer questions, and guide you toward becoming a contributor. While you're waiting for a human maintainer, I'm available to assist you.
The issue with ctx.nuxt
not existing when build: false
is likely due to the context not being properly initialized in the startServer
function. To resolve this, ensure ctx.nuxt
is initialized before it is accessed. Here is a potential fix:
ctx.nuxt
is initialized in the useTestContext
function or before it is used in the startServer
function.ctx.nuxt
is defined before accessing its properties.Here is a modified version of the startServer
function with added checks:
export async function startServer(options: StartServerOptions = {}) {
const ctx = useTestContext();
await stopServer();
const host = '127.0.0.1';
const port = ctx.options.port || await getRandomPort(host);
ctx.url = `http://${host}:${port}/`;
if (!ctx.nuxt) {
throw new Error('ctx.nuxt is not defined. Ensure that the context is properly initialized.');
}
if (ctx.options.dev) {
const nuxiCLI = await kit.resolvePath('nuxi/cli');
ctx.serverProcess = execa(nuxiCLI, ['_dev'], {
cwd: ctx.nuxt.options.rootDir,
stdio: 'inherit',
env: {
...process.env,
_PORT: String(port), // Used by internal _dev command
PORT: String(port),
HOST: host,
NODE_ENV: 'development',
...options.env,
...ctx.options.env,
},
});
await waitForPort(port, { retries: 32, host }).catch(() => {});
let lastError;
for (let i = 0; i < 150; i++) {
await new Promise(resolve => setTimeout(resolve, 100));
try {
const res = await $fetch<string>(ctx.nuxt.options.app.baseURL, { responseType: 'text' });
if (!res.includes('__NUXT_LOADING__')) {
return;
}
} catch (e) {
lastError = e;
}
}
ctx.serverProcess.kill();
throw lastError || new Error('Timeout waiting for dev server!');
} else {
ctx.serverProcess = execa('node', [
resolve(ctx.nuxt.options.nitro.output!.dir!, 'server/index.mjs'),
], {
stdio: 'inherit',
env: {
...process.env,
PORT: String(port),
HOST: host,
NODE_ENV: 'test',
...options.env,
...ctx.options.env,
},
});
await waitForPort(port, { retries: 20, host });
}
}
This modification ensures ctx.nuxt
is checked and an error is thrown if it is not defined, helping to identify the root cause of the issue [1].
To continue talking to Dosu, mention @dosu.
I am willing to provide stackblitz repro if required
A StackBlitz reproduction would be very helpful for diagnosing the issue further. Please go ahead and provide it. This will allow us to better understand the context and pinpoint the exact cause of the problem. Thank you!
To continue talking to Dosu, mention @dosu.
Environment
N/A
Reproduction
N/A
Describe the bug
setup fails
Additional context
cc: @tobiasdiez
Here are the lines
https://github.com/nuxt/test-utils/blob/86d09da23a2d7ba6b0f3dcf2e9ca8c21655a5218/src/core/server.ts#L17-L71
offending line for me was 57, but could easily be any other lines
https://github.com/nuxt/test-utils/commit/c4df41db447afe6101df10fbe763b198b12da86d
is the offending commit
specifically this change
https://github.com/nuxt/test-utils/commit/c4df41db447afe6101df10fbe763b198b12da86d#diff-84e9c1e393b43cee97ce99909a2db3a09bf9a2435daa2066b01a838e206edec3
Logs