Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a6dec5b

Browse files
committed
test: use findWorkspaceDir rather than relative paths to repo root
1 parent d6ee30e commit a6dec5b

13 files changed

+79
-49
lines changed

packages/kit/test/generate-types.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { fileURLToPath } from 'node:url'
21
import { describe, expect, it } from 'vitest'
32
import type { Nuxt, NuxtConfig } from '@nuxt/schema'
43
import { defu } from 'defu'
5-
import { withoutTrailingSlash } from 'ufo'
6-
import { normalize } from 'pathe'
4+
import { findWorkspaceDir } from 'pkg-types'
75

86
import { loadNuxtConfig } from '../src/loader/config'
97
import { _generateTypes, resolveLayerPaths } from '../src/template'
@@ -101,8 +99,8 @@ describe('tsConfig generation', () => {
10199
})
102100
})
103101

104-
describe('resolveLayerPaths', () => {
105-
const repoRoot = withoutTrailingSlash(normalize(fileURLToPath(new URL('../../../', import.meta.url))))
102+
describe('resolveLayerPaths', async () => {
103+
const repoRoot = await findWorkspaceDir()
106104

107105
it('should respect custom nuxt options', async () => {
108106
const nuxtOptions = await loadNuxtConfig({

packages/kit/test/load-nuxt-config.bench.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { fileURLToPath } from 'node:url'
21
import { bench, describe } from 'vitest'
3-
import { join, normalize } from 'pathe'
4-
import { withoutTrailingSlash } from 'ufo'
2+
import { join } from 'pathe'
53
import { loadNuxtConfig } from '@nuxt/kit'
4+
import { findWorkspaceDir } from 'pkg-types'
65

76
const fixtures = {
87
'empty directory': 'node_modules/fixture',
@@ -12,10 +11,10 @@ const fixtures = {
1211
'minimal test fixture (types)': 'test/fixtures/minimal-types',
1312
}
1413

15-
describe('loadNuxtConfig', () => {
14+
describe('loadNuxtConfig', async () => {
15+
const repoRoot = await findWorkspaceDir()
1616
for (const fixture in fixtures) {
17-
const relativeDir = join('../../..', fixtures[fixture as keyof typeof fixtures])
18-
const path = withoutTrailingSlash(normalize(fileURLToPath(new URL(relativeDir, import.meta.url))))
17+
const path = join(repoRoot, fixtures[fixture as keyof typeof fixtures])
1918
bench(`loadNuxtConfig in the ${fixture}`, async () => {
2019
await loadNuxtConfig({ cwd: path })
2120
})

packages/kit/test/load-nuxt.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { fileURLToPath } from 'node:url'
21
import { mkdir, rm, writeFile } from 'node:fs/promises'
32
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
4-
import { join, normalize } from 'pathe'
5-
import { withoutTrailingSlash } from 'ufo'
3+
import { join } from 'pathe'
64
import { x } from 'tinyexec'
75

86
import { loadNuxt } from '../src'
7+
import { findWorkspaceDir } from 'pkg-types'
98

10-
const repoRoot = withoutTrailingSlash(normalize(fileURLToPath(new URL('../../../', import.meta.url))))
9+
const repoRoot = await findWorkspaceDir()
1110

1211
describe('loadNuxt', () => {
1312
const tempDir = join(repoRoot, 'temp')
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { mkdir, rm, writeFile } from 'node:fs/promises'
2+
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
3+
import { join } from 'pathe'
4+
import { findWorkspaceDir } from 'pkg-types'
5+
import { x } from 'tinyexec'
6+
7+
import { loadNuxt } from '../src'
8+
9+
const repoRoot = await findWorkspaceDir()
10+
11+
describe('loadNuxt', () => {
12+
const tempDir = join(repoRoot, 'temp')
13+
14+
beforeAll(async () => {
15+
await mkdir(join(tempDir, 'nuxt'), { recursive: true })
16+
await writeFile(join(tempDir, 'package.json'), '{"dependencies":{"nuxt":"file:./nuxt"}}')
17+
await writeFile(join(tempDir, 'nuxt', 'package.json'), '{"name":"nuxt","type":"module","exports":{".":"./index.js"}}')
18+
await writeFile(join(tempDir, 'nuxt', 'index.js'), 'export const loadNuxt = (opts) => ({ name: "it me" })')
19+
await x('npm', ['install'], { nodeOptions: { cwd: tempDir } })
20+
})
21+
22+
afterAll(async () => {
23+
await rm(tempDir, { recursive: true, force: true })
24+
})
25+
26+
it('respects correct directory', async () => {
27+
const nuxt = await loadNuxt({ cwd: tempDir })
28+
expect(nuxt).toStrictEqual({
29+
name: 'it me',
30+
})
31+
})
32+
})

packages/kit/test/write-types.bench.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import { fileURLToPath } from 'node:url'
21
import { rm } from 'node:fs/promises'
32
import { afterAll, beforeAll, bench, describe } from 'vitest'
4-
import { join, normalize, resolve } from 'pathe'
5-
import { withoutTrailingSlash } from 'ufo'
3+
import { join, resolve } from 'pathe'
64
import type { Nuxt } from 'nuxt/schema'
75
import { loadNuxt, writeTypes } from '@nuxt/kit'
6+
import { findWorkspaceDir } from 'pkg-types'
87

9-
describe('writeTypes', () => {
10-
const relativeDir = join('../../..', 'test/fixtures/basic-types')
11-
const path = withoutTrailingSlash(normalize(fileURLToPath(new URL(relativeDir, import.meta.url))))
8+
describe('writeTypes', async () => {
9+
const repoRoot = await findWorkspaceDir()
10+
const path = join(repoRoot, 'test/fixtures/basic-types')
1211

1312
let nuxt: Nuxt
1413

packages/nuxt/test/app.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { fileURLToPath } from 'node:url'
21
import { mkdir, rm, writeFile } from 'node:fs/promises'
32
import { randomUUID } from 'node:crypto'
43
import { afterAll, describe, expect, it } from 'vitest'
5-
import { dirname, join, normalize, resolve } from 'pathe'
6-
import { withoutTrailingSlash } from 'ufo'
4+
import { dirname, join, resolve } from 'pathe'
5+
import { findWorkspaceDir } from 'pkg-types'
76
import { createApp, resolveApp } from '../src/core/app'
87
import { loadNuxt } from '../src'
98

10-
const repoRoot = withoutTrailingSlash(normalize(fileURLToPath(new URL('../../../', import.meta.url))))
9+
const repoRoot = await findWorkspaceDir()
1110

1211
describe('resolveApp', () => {
1312
afterAll(async () => {

packages/nuxt/test/build-plugins.bench.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { fileURLToPath } from 'node:url'
21
import { rm } from 'node:fs/promises'
32
import { afterAll, beforeAll, bench, describe } from 'vitest'
4-
import { join, normalize } from 'pathe'
5-
import { withoutTrailingSlash } from 'ufo'
3+
import { join } from 'pathe'
64
import type { Nuxt } from '@nuxt/schema'
75
import { build, loadNuxt } from 'nuxt'
6+
import { findWorkspaceDir } from 'pkg-types'
87

9-
const basicTestFixtureDir = withoutTrailingSlash(normalize(fileURLToPath(new URL('../../../test/fixtures/minimal', import.meta.url))))
8+
const repoRoot = await findWorkspaceDir()
9+
const basicTestFixtureDir = join(repoRoot, 'test/fixtures/minimal')
1010

1111
describe('build', () => {
1212
let nuxt: Nuxt

packages/nuxt/test/build.bench.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { fileURLToPath } from 'node:url'
21
import { rm } from 'node:fs/promises'
32
import { beforeAll, bench, describe } from 'vitest'
4-
import { join, normalize } from 'pathe'
5-
import { withoutTrailingSlash } from 'ufo'
3+
import { join } from 'pathe'
64
import { build, loadNuxt } from 'nuxt'
5+
import { findWorkspaceDir } from 'pkg-types'
76

8-
const basicTestFixtureDir = withoutTrailingSlash(normalize(fileURLToPath(new URL('../../../test/fixtures/basic', import.meta.url))))
7+
const repoRoot = await findWorkspaceDir()
8+
const basicTestFixtureDir = join(repoRoot, 'test/fixtures/basic')
99

1010
describe('build', () => {
1111
beforeAll(async () => {

packages/nuxt/test/components-transform.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { fileURLToPath } from 'node:url'
21
import { describe, expect, it } from 'vitest'
32
import type { Component, Nuxt } from '@nuxt/schema'
43
import { kebabCase } from 'scule'
54
import { normalize } from 'pathe'
5+
import { findWorkspaceDir } from 'pkg-types'
66

77
import { TransformPlugin } from '../src/components/plugins/transform'
88

@@ -80,7 +80,7 @@ describe('components:transform', () => {
8080
})
8181
})
8282

83-
const rootDir = fileURLToPath(new URL('../..', import.meta.url))
83+
const repoRoot = await findWorkspaceDir()
8484

8585
function createTransformer (components: Component[], mode: 'client' | 'server' | 'all' = 'all') {
8686
const stubNuxt = {
@@ -100,7 +100,7 @@ function createTransformer (components: Component[], mode: 'client' | 'server' |
100100

101101
return async (code: string, id: string) => {
102102
const result = await (plugin as any).transform!(code, id)
103-
return (typeof result === 'string' ? result : result?.code)?.replaceAll(normalize(rootDir), '<repo>/')
103+
return (typeof result === 'string' ? result : result?.code)?.replaceAll(normalize(repoRoot), '<repo>/')
104104
}
105105
}
106106

packages/nuxt/test/load-nuxt.bench.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { fileURLToPath } from 'node:url'
21
import { rm } from 'node:fs/promises'
32
import { beforeAll, bench, describe } from 'vitest'
4-
import { join, normalize } from 'pathe'
5-
import { withoutTrailingSlash } from 'ufo'
3+
import { join } from 'pathe'
64
import { loadNuxt } from 'nuxt'
5+
import { findWorkspaceDir } from 'pkg-types'
76

8-
const emptyDir = withoutTrailingSlash(normalize(fileURLToPath(new URL('../../../node_modules/fixture', import.meta.url))))
9-
const basicTestFixtureDir = withoutTrailingSlash(normalize(fileURLToPath(new URL('../../../test/fixtures/basic', import.meta.url))))
7+
const repoRoot = await findWorkspaceDir()
8+
9+
const emptyDir = join(repoRoot, 'node_modules/fixture')
10+
const basicTestFixtureDir = join(repoRoot, 'test/fixtures/basic')
1011

1112
describe('loadNuxt', () => {
1213
beforeAll(async () => {

0 commit comments

Comments
 (0)