Validating Pages.json Configuration and Subpackages in uni-app
This test suite validates the pages.json configuration in uni-app, focusing on page routing and subpackage structure verification. The tests ensure proper JSON formatting and error handling for both main pages and subpackages configurations.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
dcloudio/uni-app
packages/uni-cli-shared/__tests__/pagesJson.spec.ts
import path from 'path'
import { checkPagesJson } from '../src/json/uni-x'
import { generateCodeFrame } from '../src/vite/plugins/vitejs/utils'
describe('pages.json', () => {
const inputDir = path.resolve(__dirname, './examples/check-pages-json')
test(`pages check`, () => {
const source = JSON.stringify(
{
pages: [
{
path: 'pages/index/index',
},
{
path: 'pages/index/test',
},
],
},
null,
2
)
try {
checkPagesJson(source, inputDir)
} catch (error: any) {
expect(error).toMatchSnapshot()
expect(
generateCodeFrame(
source,
error.offsetStart as number,
error.offsetEnd as number
).replace(/\t/g, ' ')
).toMatchSnapshot()
}
})
test(`subPackages check`, () => {
const source = JSON.stringify(
{
subPackages: [
{
root: 'pages/API',
pages: [
{
path: 'index/index',
},
],
},
],
},
null,
2
)
try {
checkPagesJson(source, inputDir)
} catch (error: any) {
expect(error).toMatchSnapshot()
expect(
generateCodeFrame(
source,
error.offsetStart as number,
error.offsetEnd as number
).replace(/\t/g, ' ')
).toMatchSnapshot()
}
})
test(`subpackages check`, () => {
const source = JSON.stringify({
subpackages: [
{
root: 'pages/API',
pages: [
{
path: 'index/index',
},
],
},
],
})
try {
checkPagesJson(source, inputDir)
} catch (error: any) {
expect(error).toMatchSnapshot()
expect(
generateCodeFrame(
source,
error.offsetStart as number,
error.offsetEnd as number
).replace(/\t/g, ' ')
).toMatchSnapshot()
}
})
})