2024-07-16 14:12:31 +00:00
|
|
|
import { describe, expect, it, vi } from 'vitest'
|
2024-05-02 13:24:31 +00:00
|
|
|
import { applyDefaults } from 'untyped'
|
|
|
|
|
2024-06-16 14:26:47 +00:00
|
|
|
import { normalize } from 'pathe'
|
2024-05-02 13:24:31 +00:00
|
|
|
import { NuxtConfigSchema } from '../src'
|
|
|
|
import type { NuxtOptions } from '../src'
|
|
|
|
|
2024-07-16 14:12:31 +00:00
|
|
|
vi.mock('node:fs', () => ({
|
|
|
|
existsSync: (id: string) => id.endsWith('app'),
|
|
|
|
}))
|
|
|
|
|
2024-05-02 13:24:31 +00:00
|
|
|
describe('nuxt folder structure', () => {
|
|
|
|
it('should resolve directories for v3 setup correctly', async () => {
|
|
|
|
const result = await applyDefaults(NuxtConfigSchema, {})
|
|
|
|
expect(getDirs(result as unknown as NuxtOptions)).toMatchInlineSnapshot(`
|
|
|
|
{
|
|
|
|
"dir": {
|
2024-06-19 15:02:35 +00:00
|
|
|
"app": "<cwd>/app",
|
|
|
|
"modules": "<cwd>/modules",
|
|
|
|
"public": "<cwd>/public",
|
2024-05-02 13:24:31 +00:00
|
|
|
},
|
|
|
|
"rootDir": "<cwd>",
|
|
|
|
"serverDir": "<cwd>/server",
|
2024-06-19 15:02:35 +00:00
|
|
|
"srcDir": "<cwd>/app",
|
2024-05-02 13:24:31 +00:00
|
|
|
"workspaceDir": "<cwd>",
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should resolve directories with a custom `srcDir` and `rootDir`', async () => {
|
|
|
|
const result = await applyDefaults(NuxtConfigSchema, { srcDir: 'src/', rootDir: '/test' })
|
|
|
|
expect(getDirs(result as unknown as NuxtOptions)).toMatchInlineSnapshot(`
|
|
|
|
{
|
|
|
|
"dir": {
|
2024-06-19 15:02:35 +00:00
|
|
|
"app": "/test/src",
|
|
|
|
"modules": "/test/modules",
|
|
|
|
"public": "/test/public",
|
2024-05-02 13:24:31 +00:00
|
|
|
},
|
|
|
|
"rootDir": "/test",
|
2024-06-19 15:02:35 +00:00
|
|
|
"serverDir": "/test/server",
|
2024-05-02 13:24:31 +00:00
|
|
|
"srcDir": "/test/src",
|
|
|
|
"workspaceDir": "/test",
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should resolve directories when opting-in to v4 schema', async () => {
|
|
|
|
const result = await applyDefaults(NuxtConfigSchema, { future: { compatibilityVersion: 4 } })
|
|
|
|
expect(getDirs(result as unknown as NuxtOptions)).toMatchInlineSnapshot(`
|
|
|
|
{
|
|
|
|
"dir": {
|
|
|
|
"app": "<cwd>/app",
|
|
|
|
"modules": "<cwd>/modules",
|
|
|
|
"public": "<cwd>/public",
|
|
|
|
},
|
|
|
|
"rootDir": "<cwd>",
|
|
|
|
"serverDir": "<cwd>/server",
|
|
|
|
"srcDir": "<cwd>/app",
|
|
|
|
"workspaceDir": "<cwd>",
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should resolve directories when opting-in to v4 schema with a custom `srcDir` and `rootDir`', async () => {
|
|
|
|
const result = await applyDefaults(NuxtConfigSchema, { future: { compatibilityVersion: 4 }, srcDir: 'customApp/', rootDir: '/test' })
|
|
|
|
expect(getDirs(result as unknown as NuxtOptions)).toMatchInlineSnapshot(`
|
|
|
|
{
|
|
|
|
"dir": {
|
|
|
|
"app": "/test/customApp",
|
|
|
|
"modules": "/test/modules",
|
|
|
|
"public": "/test/public",
|
|
|
|
},
|
|
|
|
"rootDir": "/test",
|
|
|
|
"serverDir": "/test/server",
|
|
|
|
"srcDir": "/test/customApp",
|
|
|
|
"workspaceDir": "/test",
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
})
|
2024-07-23 08:31:39 +00:00
|
|
|
|
|
|
|
it('should not override value from user for serverDir', async () => {
|
|
|
|
const result = await applyDefaults(NuxtConfigSchema, { future: { compatibilityVersion: 4 }, serverDir: '/myServer' })
|
|
|
|
expect(getDirs(result as unknown as NuxtOptions)).toMatchInlineSnapshot(`
|
|
|
|
{
|
|
|
|
"dir": {
|
|
|
|
"app": "<cwd>/app",
|
|
|
|
"modules": "<cwd>/modules",
|
|
|
|
"public": "<cwd>/public",
|
|
|
|
},
|
|
|
|
"rootDir": "<cwd>",
|
|
|
|
"serverDir": "/myServer",
|
|
|
|
"srcDir": "<cwd>/app",
|
|
|
|
"workspaceDir": "<cwd>",
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
})
|
2024-05-02 13:24:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
function getDirs (options: NuxtOptions) {
|
2024-06-16 14:26:47 +00:00
|
|
|
const stripRoot = (dir: string) => {
|
|
|
|
return normalize(dir).replace(normalize(process.cwd()), '<cwd>')
|
|
|
|
}
|
2024-05-02 13:24:31 +00:00
|
|
|
return {
|
|
|
|
rootDir: stripRoot(options.rootDir),
|
|
|
|
serverDir: stripRoot(options.serverDir),
|
|
|
|
srcDir: stripRoot(options.srcDir),
|
|
|
|
dir: {
|
|
|
|
app: stripRoot(options.dir.app),
|
|
|
|
modules: stripRoot(options.dir.modules),
|
|
|
|
public: stripRoot(options.dir.public),
|
|
|
|
},
|
|
|
|
workspaceDir: stripRoot(options.workspaceDir!),
|
|
|
|
}
|
|
|
|
}
|