Testing Component Tree Shaking Implementation in dcloudio/uni-app
This test suite evaluates tree shaking functionality in uni-app components for both Android and iOS builds. It verifies the proper component optimization and manifest generation across different build configurations, ensuring efficient bundle size management.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
dcloudio/uni-app
packages/playground/__tests__/app-components-tree-shaking.spec.ts
import fs from 'fs-extra'
import path from 'path'
import execa from 'execa'
const projectDir = path.resolve(__dirname, '../app-components-tree-shaking')
describe('app-components-tree-shaking playground', () => {
jest.setTimeout(50 * 1000)
const types = {
'uni-app-x': ['build:app-android', 'build:app-ios'],
}
const distDir = path.resolve(projectDir, 'dist')
if (fs.existsSync(distDir)) {
fs.emptyDirSync(distDir)
}
Object.keys(types).forEach((type) => {
const scripts = types[type]
scripts.forEach((script) => {
const mode = script.split(':')[0]
const platform = script.split(':')[1]
test(`${type} ${script}`, async () => {
const outDir = path.resolve(distDir, mode, type, platform)
console.log(`${type} npm run ${script} start`)
await execa('npm', ['run', script], {
cwd: projectDir,
env: {
...process.env,
UNI_OUTPUT_DIR: outDir,
UNI_APP_X: type === 'uni-app-x' ? 'true' : 'false',
},
})
console.log(`${type} npm run ${script} end`)
const manifest = JSON.parse(
fs.readFileSync(path.resolve(outDir, 'manifest.json'), 'utf-8')
)
// 里边的版本号会变更,移除影响
delete manifest['uni-app-x']
expect(JSON.stringify(manifest, null, 2)).toMatchSnapshot()
})
})
})
})