Skip to content
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

ctx.nuxt does not exist when build: false in setup #918

Open
nopeless opened this issue Aug 14, 2024 · 3 comments · May be fixed by #936
Open

ctx.nuxt does not exist when build: false in setup #918

nopeless opened this issue Aug 14, 2024 · 3 comments · May be fixed by #936
Labels
bug Something isn't working pending triage

Comments

@nopeless
Copy link

nopeless commented Aug 14, 2024

Environment

N/A

Reproduction

N/A

Describe the bug

setup fails

Additional context

cc: @tobiasdiez

Here are the lines

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

offending line for me was 57, but could easily be any other lines

c4df41d

is the offending commit

specifically this change

c4df41d#diff-84e9c1e393b43cee97ce99909a2db3a09bf9a2435daa2066b01a838e206edec3

Logs

N/A
Copy link

dosubot bot commented Aug 14, 2024

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:

  1. Ensure ctx.nuxt is initialized in the useTestContext function or before it is used in the startServer function.
  2. Add a check to ensure 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.

@dosubot dosubot bot added the bug Something isn't working label Aug 14, 2024
@nopeless
Copy link
Author

I am willing to provide stackblitz repro if required

Copy link

dosubot bot commented Aug 14, 2024

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.

@Al-Rozhkov Al-Rozhkov linked a pull request Sep 9, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant