upload-config.test.mjs
3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import assert from 'node:assert/strict'
import { readFile, stat } from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, it } from 'node:test'
const projectRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../..'
)
describe('小程序上传边界', () => {
it('项目运行时固定为 Node 24 LTS', async () => {
const packageJson = JSON.parse(
await readFile(path.join(projectRoot, 'package.json'), 'utf8')
)
assert.equal(packageJson.volta?.node, '24.18.0')
assert.equal(packageJson.engines?.node, '>=24 <25')
const guardSource = await readFile(
path.join(projectRoot, 'scripts/wx-upload-guard.js'),
'utf8'
)
assert.match(guardSource, /nodeMajor < 24/)
})
it('文档、测试和开发脚本不会进入小程序上传包', async () => {
const config = JSON.parse(
await readFile(path.join(projectRoot, 'project.config.json'), 'utf8')
)
const ignoredFolders = new Set(
config.packOptions.ignore
.filter(item => item.type === 'folder')
.map(item => item.value)
)
for (const folder of ['docs', 'tests', 'scripts']) {
assert.ok(ignoredFolders.has(folder), `未忽略开发目录:${folder}`)
}
})
it('开发编译只加载小程序依赖图中的文件', async () => {
const [projectConfig, privateConfig] = await Promise.all([
readFile(path.join(projectRoot, 'project.config.json'), 'utf8').then(
JSON.parse
),
readFile(
path.join(projectRoot, 'project.private.config.json'),
'utf8',
).then(JSON.parse)
])
assert.equal(projectConfig.setting?.ignoreDevUnusedFiles, true)
assert.equal(privateConfig.setting?.ignoreDevUnusedFiles, true)
})
it('上传会过滤未使用文件、源映射和重复 npm 产物', async () => {
const config = JSON.parse(
await readFile(path.join(projectRoot, 'project.config.json'), 'utf8')
)
const ignoredTypes = new Map(
(config.packOptions?.ignore || []).map(item => [item.value, item.type])
)
assert.equal(config.setting?.ignoreUploadUnusedFiles, true)
assert.equal(config.setting?.uploadWithSourceMap, false)
assert.equal(config.setting?.packNpmManually, true)
assert.deepEqual(config.setting?.packNpmRelationList, [
{
packageJsonPath: './package.json',
miniprogramNpmDistDir: './'
}
])
for (const entry of [
'miniprogram_npm/miniprogram_npm',
'outputs',
'typings',
'.superpowers',
'test'
]) {
assert.equal(ignoredTypes.get(entry), 'folder', `未以目录忽略上传内容:${entry}`)
}
for (const entry of [
'project.private.config.json',
'yarn.lock',
'prettier-plugin-wxml.mjs'
]) {
assert.equal(ignoredTypes.get(entry), 'file', `未以文件忽略上传内容:${entry}`)
}
})
it('上传前守卫脚本执行主包预算、跨包边界和静态闭包检查', async () => {
const guardPath = path.join(projectRoot, 'scripts/wx-upload-guard.js')
assert.ok((await stat(guardPath)).isFile())
const source = await readFile(guardPath, 'utf8')
assert.match(source, /main-package-size\.mjs/)
assert.match(source, /check-package-boundaries\.mjs/)
assert.match(source, /check-work-order-migration\.mjs/)
})
})