Repository: nuxt/nuxt
File: packages/nuxt/test/load-nuxt.test.ts (TypeScript)

Code before:
    expect(tryUseNuxt()?.__name).not.toBe(nuxt.__name)

    // second nuxt context
    const second = await loadNuxt({
      cwd: repoRoot,
    })

    expect(second.__name).not.toBe(nuxt.__name)
    expect(tryUseNuxt()?.__name).not.toBe(nuxt.__name)

    // @ts-expect-error - random hook
    await nuxt.callHook('test')

    expect(loggerWarn).not.toHaveBeenCalled()
  })

  it('ensures layer modules remains in order', async () => {
    const layerFixtureDir = withoutTrailingSlash(normalize(fileURLToPath(new URL('./layers-fixture', import.meta.url))))
    const nuxt = await loadNuxt({ cwd: layerFixtureDir })
    await nuxt.close()

    const modules = nuxt.options.modules.map(item => typeof item === 'function' ? item.name : item)

    expect(modules).toMatchInlineSnapshot(`
      [
        "layer",
        "${repoRoot}/packages/nuxt/test/layers-fixture/custom/module",
        "project",
        "css",
      ]
    `)
  })
})

const pagesDetectionTests: [test: string, overrides: NuxtConfig, result: NuxtConfig['pages']][] = [
  ['pages dir', {}, { enabled: true }],
  ['pages dir empty', { dir: { pages: 'empty-dir' } }, { enabled: false }],
  ['user config', { pages: false }, { enabled: false }],
  ['user config', { pages: { enabled: false } }, { enabled: false }],
  ['user config', { pages: { enabled: true, pattern: '**/*{.vue}' } }, { enabled: true, pattern: '**/*{.vue}' }],
]

const pagesFixtureDir = withoutTrailingSlash(normalize(fileURLToPath(new URL('./pages-fixture', import.meta.url))))
describe('pages detection', () => {
  it.each(pagesDetectionTests)('%s `%s`', async (_, overrides, result) => {
    const nuxt = await loadNuxt({ cwd: pagesFixtureDir, overrides, ready: true })
    // @ts-expect-error should resolve to object?
    expect(nuxt.options.pages).toMatchObject(result)
    await nuxt.close()
  })
})

Review comment (suggestion):
I think auto-registered layers should be last because they are in the user's project.

so while we are reordering, we should probably ensure they are 'after' layers in `extends`, etc.

Code after:
    expect(tryUseNuxt()?.__name).not.toBe(nuxt.__name)

    // second nuxt context
    const second = await loadNuxt({
      cwd: repoRoot,
    })

    expect(second.__name).not.toBe(nuxt.__name)
    expect(tryUseNuxt()?.__name).not.toBe(nuxt.__name)

    // @ts-expect-error - random hook
    await nuxt.callHook('test')

    expect(loggerWarn).not.toHaveBeenCalled()
  })

  it('ensures layer modules remain in order', async () => {
    const layerFixtureDir = withoutTrailingSlash(normalize(fileURLToPath(new URL('./layers-fixture', import.meta.url))))
    const nuxt = await loadNuxt({ cwd: layerFixtureDir })
    await nuxt.close()

    const modules = nuxt.options._installedModules.map(item => item.meta.name ?? item.module.name)

    expect(modules).toMatchInlineSnapshot(`
      [
        "customLayerInlineModule",
        "customLayerModule",
        "customLayerAutoModule",
        "autoLayerInlineModule",
        "autoLayerModule",
        "autoLayerAutoModule",
        "projectModule",
        "projectInlineModule",
        "css",
        "projectAutoModule",
        "@nuxt/devtools",
        "nuxt:pages",
        "nuxt:meta",
        "nuxt:components",
        "nuxt:imports",
        "nuxt:nuxt-config-schema",
        "@nuxt/telemetry",
      ]
    `)
  })
})

const pagesDetectionTests: [test: string, overrides: NuxtConfig, result: NuxtConfig['pages']][] = [
  ['pages dir', {}, { enabled: true }],
  ['pages dir empty', { dir: { pages: 'empty-dir' } }, { enabled: false }],
  ['user config', { pages: false }, { enabled: false }],