-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
test.js
115 lines (90 loc) 路 3.1 KB
/
test.js
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
103
104
105
106
107
108
109
110
111
112
113
114
115
const fs = require('fs')
const { join, resolve } = require('path')
const { execSync } = require('child_process')
const { version: packageVersion } = require('../package.json')
const [agent, version, type = 'commonjs'] = process.argv.slice(2)
const ROOT = resolve(__dirname, '..')
const DIR = resolve(ROOT, `../vue-demi-test-${type}`)
const isVue2 = version.startsWith('2')
const isVue27 = version.startsWith('2.7')
const isCjs = type === 'commonjs'
function pack() {
execSync('npm pack', { cwd: ROOT, stdio: 'inherit' })
return join(ROOT, `vue-demi-${packageVersion}.tgz`)
}
function installDeps() {
const tarball = pack()
let installCmd = agent === 'yarn' ? `${agent} add` : `${agent} i`
const packages = isVue27 ? 'vue@v2-alpha' : isVue2 ? `vue@2.6 @vue/composition-api` : 'vue@3'
execSync(`${installCmd} ${packages}`, { cwd: DIR, stdio: 'inherit' })
execSync(`${installCmd} ${agent === 'yarn' ? `file:${tarball}` : tarball} --force`, { cwd: DIR, stdio: 'inherit' })
}
function prepareTestPackage(type = 'commonjs') {
if (fs.existsSync(DIR)) fs.rmSync(DIR, { recursive: true })
fs.mkdirSync(DIR)
fs.writeFileSync(
join(DIR, 'package.json'),
JSON.stringify({
name: `vue-demi-test-${type}`,
version: packageVersion,
type,
}),
'utf-8'
)
installDeps()
}
prepareTestPackage(type)
const indexFile = isCjs ? 'index.cjs' : 'index.mjs'
const mod = fs.readFileSync(resolve(DIR, `node_modules/vue-demi/lib/${indexFile}`), 'utf-8')
let failed = false
if (isCjs && !mod.includes(`exports.isVue2 = ${isVue2}`)) {
console.log('CJS:', mod)
failed = true
}
if (!isCjs && !/export\s\{\n\s\sVue,\n\s\sVue2,\n\s\sisVue2/gm.test(mod)) {
console.log('ESM:', mod)
failed = true
}
const outputVersion = execSync(`node -e "console.log(require('vue-demi').version)"`, { cwd: DIR }).toString().trim()
console.log('version: ' + outputVersion)
// isVue2
const is2 = execSync(`node -e "console.log(require('vue-demi').isVue2)"`, { cwd: DIR }).toString().trim()
if (is2 !== `${isVue2}`) {
console.log(`isVue2: ${is2} !== ${isVue2}`)
failed = true
}
const hasVue2 = execSync(`node -e "console.log(require('vue-demi').Vue2 !== undefined)"`, { cwd: DIR }).toString().trim()
if (hasVue2 !== `${isVue2}`) {
console.log(`hasVue2: ${hasVue2} !== ${isVue2}`)
failed = true
}
const importCJS = `const { ref, computed } = require('vue-demi');`
const importESM = `const { ref, computed } = await import('vue-demi');`
const snippet = `
let a = ref(12)
let b = computed(() => a.value * 2)
console.log(b.value)
a.value += 1
console.log(b.value)
`
.replace(/\n/g, ';')
.trim()
// ref
const refCJS = execSync(`node -e "${importCJS}${snippet}"`, { cwd: DIR }).toString().trim()
if (refCJS !== `24\n26`) {
console.log(`ref(cjs): ${refCJS} !== 24\n26`)
failed = true
}
// TODO: 2.7's ESM can't runs in Node currently
if (!isVue27) {
const refESM = execSync(`node -e "(async ()=>{${importESM}${snippet}})()"`, { cwd: DIR }).toString().trim()
if (refESM !== `24\n26`) {
console.log(`ref(esm): ${refESM} !== 24\n26`)
failed = true
}
}
if (failed) {
setTimeout(() => {
process.exit(1)
}, 0)
}