Skip to content

Commit 6a03794

Browse files
committed
Add testcase
1 parent 1117f9d commit 6a03794

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

packages/next-plugin/src/__tests__/preload.test.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { join } from 'node:path'
66
import { codeExtract, getCss } from '@devup-ui/wasm'
77
import { globSync } from 'glob'
88

9+
import { findTopPackageRoot } from '../find-top-package-root'
10+
import { getPackageName } from '../get-package-name'
11+
import { hasLocalPackage } from '../has-localpackage'
912
import { preload } from '../preload'
1013

1114
// Mock dependencies
@@ -33,6 +36,18 @@ vi.mock('@devup-ui/wasm', () => ({
3336
getCss: vi.fn(),
3437
}))
3538

39+
vi.mock('../find-top-package-root', () => ({
40+
findTopPackageRoot: vi.fn(),
41+
}))
42+
43+
vi.mock('../get-package-name', () => ({
44+
getPackageName: vi.fn(),
45+
}))
46+
47+
vi.mock('../has-localpackage', () => ({
48+
hasLocalPackage: vi.fn(),
49+
}))
50+
3651
describe('preload', () => {
3752
beforeEach(() => {
3853
vi.clearAllMocks()
@@ -233,4 +248,63 @@ describe('preload', () => {
233248
'utf-8',
234249
)
235250
})
251+
252+
it('should recurse into local workspaces when include is provided', () => {
253+
const files = ['src/App.tsx']
254+
vi.mocked(findTopPackageRoot).mockReturnValue('/repo')
255+
vi.mocked(hasLocalPackage)
256+
.mockReturnValueOnce(true)
257+
.mockReturnValueOnce(false)
258+
vi.mocked(globSync)
259+
.mockReturnValueOnce([
260+
'/repo/packages/pkg-a/package.json',
261+
'/repo/packages/pkg-b/package.json',
262+
])
263+
.mockReturnValueOnce(files)
264+
vi.mocked(getPackageName)
265+
.mockReturnValueOnce('pkg-a')
266+
.mockReturnValueOnce('pkg-b')
267+
vi.mocked(realpathSync).mockReturnValueOnce('src/App.tsx')
268+
269+
preload(/node_modules/, '@devup-ui/react', false, '/output/css', ['pkg-a'])
270+
271+
expect(findTopPackageRoot).toHaveBeenCalled()
272+
expect(globSync).toHaveBeenCalledWith(
273+
['package.json', '!**/node_modules/**'],
274+
{
275+
follow: true,
276+
absolute: true,
277+
cwd: '/repo',
278+
},
279+
)
280+
expect(codeExtract).toHaveBeenCalledTimes(1)
281+
expect(realpathSync).toHaveBeenCalledWith('src/App.tsx')
282+
})
283+
284+
it('should skip test and build outputs based on filters', () => {
285+
vi.mocked(globSync).mockReturnValue([
286+
'src/App.test.tsx',
287+
'.next/page.tsx',
288+
'out/index.js',
289+
'src/keep.ts',
290+
])
291+
vi.mocked(realpathSync)
292+
.mockReturnValueOnce('src/App.test.tsx')
293+
.mockReturnValueOnce('.next/page.tsx')
294+
.mockReturnValueOnce('out/index.js')
295+
.mockReturnValueOnce('src/keep.ts')
296+
297+
preload(/exclude/, '@devup-ui/react', false, '/output/css', [])
298+
299+
expect(codeExtract).toHaveBeenCalledTimes(1)
300+
expect(codeExtract).toHaveBeenCalledWith(
301+
expect.stringMatching(/keep\.ts$/),
302+
'const Button = () => <div>Hello</div>',
303+
'@devup-ui/react',
304+
'/output/css',
305+
false,
306+
false,
307+
true,
308+
)
309+
})
236310
})
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import type { PathLike } from 'node:fs'
2+
import { join } from 'node:path'
3+
4+
import { describe, expect, it, vi } from 'vitest'
5+
6+
import { findTopPackageRoot } from '../find-top-package-root'
7+
import { getPackageName } from '../get-package-name'
8+
import { hasLocalPackage } from '../has-localpackage'
9+
10+
vi.mock('node:fs', () => ({
11+
existsSync: vi.fn(),
12+
readFileSync: vi.fn(),
13+
}))
14+
15+
const { existsSync, readFileSync } = await import('node:fs')
16+
17+
describe('findTopPackageRoot', () => {
18+
it('returns highest directory containing package.json', () => {
19+
const root = join('/', 'repo')
20+
const child = join(root, 'packages', 'pkg')
21+
vi.mocked(existsSync).mockImplementation((path: PathLike) => {
22+
if (path === join(root, 'package.json')) return true
23+
return false
24+
})
25+
26+
const result = findTopPackageRoot(child)
27+
28+
expect(result).toBe(root)
29+
})
30+
31+
it('falls back to cwd when no package.json found', () => {
32+
const cwd = join('/', 'repo', 'packages', 'pkg')
33+
vi.mocked(existsSync).mockReturnValue(false)
34+
35+
const result = findTopPackageRoot(cwd)
36+
37+
expect(result).toBe(cwd)
38+
})
39+
})
40+
41+
describe('hasLocalPackage', () => {
42+
it('detects workspace dependency', () => {
43+
vi.mocked(readFileSync).mockReturnValue(
44+
JSON.stringify({
45+
dependencies: {
46+
foo: 'workspace:*',
47+
bar: '^1.0.0',
48+
},
49+
}),
50+
)
51+
52+
expect(hasLocalPackage()).toBe(true)
53+
})
54+
55+
it('returns false when no workspace dependency', () => {
56+
vi.mocked(readFileSync).mockReturnValue(
57+
JSON.stringify({
58+
dependencies: {
59+
foo: '^1.0.0',
60+
},
61+
}),
62+
)
63+
64+
expect(hasLocalPackage()).toBe(false)
65+
})
66+
67+
it('returns false when dependencies field is missing', () => {
68+
vi.mocked(readFileSync).mockReturnValue('{}')
69+
70+
expect(hasLocalPackage()).toBe(false)
71+
})
72+
})
73+
74+
describe('getPackageName', () => {
75+
it('reads and returns package name', () => {
76+
vi.mocked(readFileSync).mockReturnValue(
77+
JSON.stringify({ name: '@scope/pkg' }),
78+
)
79+
80+
expect(getPackageName('/path/package.json')).toBe('@scope/pkg')
81+
expect(readFileSync).toHaveBeenCalledWith('/path/package.json', 'utf-8')
82+
})
83+
})

0 commit comments

Comments
 (0)