Skip to content
This repository was archived by the owner on Jul 21, 2025. It is now read-only.

Commit 6afe3e2

Browse files
authored
fix: simplify tests (#96)
1 parent a77d78c commit 6afe3e2

File tree

3 files changed

+4430
-230
lines changed

3 files changed

+4430
-230
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"depcheck-package-name": "^2.0.6",
5151
"execa": "^5",
5252
"fs-extra": "^10.1.0",
53+
"nuxt": "^2.15.8",
5354
"output-files": "^2.0.0",
5455
"tree-kill-promise": "^2.0.8"
5556
},

src/index.spec.js

Lines changed: 71 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,61 @@
1-
import { delay, endent, identity, mapValues } from '@dword-design/functions'
1+
import { delay, endent } from '@dword-design/functions'
22
import tester from '@dword-design/tester'
33
import testerPluginPuppeteer from '@dword-design/tester-plugin-puppeteer'
44
import testerPluginTmpDir from '@dword-design/tester-plugin-tmp-dir'
55
import packageName from 'depcheck-package-name'
66
import execa from 'execa'
7-
import { outputFile, remove, symlink } from 'fs-extra'
7+
import { outputFile } from 'fs-extra'
8+
import { Builder, Nuxt } from 'nuxt'
89
import outputFiles from 'output-files'
910
import P from 'path'
1011
import kill from 'tree-kill-promise'
1112

1213
export default tester(
1314
{
15+
nuxt3: {
16+
files: {
17+
modules: {
18+
bar: {
19+
'index.js': endent`
20+
import self from '../../../src'
21+
22+
export default function () {
23+
self(this, require.resolve('./plugin'))
24+
}
25+
26+
`,
27+
'plugin.js':
28+
"export default (context, inject) => inject('bar', context.$foo)",
29+
},
30+
foo: {
31+
'index.js': endent`
32+
import self from '../../../src'
33+
34+
export default function () {
35+
self(this, require.resolve('./plugin'))
36+
}
37+
38+
`,
39+
'plugin.js':
40+
"export default (context, inject) => inject('foo', 'bar')",
41+
},
42+
},
43+
'pages/index.vue': endent`
44+
<template>
45+
<div :class="$bar" />
46+
</template>
47+
48+
`,
49+
},
50+
nuxtConfig: {
51+
modules: ['./modules/foo', './modules/bar'],
52+
},
53+
nuxtVersion: 3,
54+
async test() {
55+
await this.page.goto('http://localhost:3000')
56+
await this.page.waitForSelector('.bar')
57+
},
58+
},
1459
works: {
1560
files: {
1661
modules: {
@@ -51,65 +96,28 @@ export default tester(
5196
},
5297
async test() {
5398
await this.page.goto('http://localhost:3000')
54-
console.log(await this.page.content())
5599
await this.page.waitForSelector('.bar')
56100
},
57101
},
58102
},
59103
[
60104
testerPluginTmpDir(),
61105
{
62-
after: () => Promise.all([remove('nuxt2'), remove('nuxt3')]),
63-
before: async () => {
64-
await outputFile(P.join('nuxt2', 'package.json'), JSON.stringify({}))
65-
await execa.command('yarn add --dev nuxt', { cwd: 'nuxt2' })
66-
await outputFile(P.join('nuxt3', 'package.json'), JSON.stringify({}))
67-
await execa.command('yarn add --dev nuxt@3.0.0-rc.12', {
68-
cwd: 'nuxt3',
69-
})
70-
},
71106
transform: config => {
72-
config = { test: () => {}, ...config }
107+
config = { nuxtConfig: {}, nuxtVersion: 2, test: () => {}, ...config }
73108

74109
return async function () {
75110
await outputFiles({
76111
'package.json': JSON.stringify({ type: 'module' }),
77112
...config.files,
78113
})
79-
let versions = {
80-
2: {},
81-
3: {
82-
transformNuxtConfig: nuxtConfig =>
83-
nuxtConfig
84-
|> mapValues((value, key) =>
85-
key === 'modules'
86-
? value.map(mod =>
87-
mod.startsWith('~') ? `.${mod.slice(1)}` : mod
88-
)
89-
: value
90-
),
91-
},
92-
}
93-
versions =
94-
versions
95-
|> mapValues(versionConfig => ({
96-
transformNuxtConfig: identity,
97-
...versionConfig,
98-
}))
99-
for (const version of Object.keys(versions)) {
100-
console.log(version)
101-
await remove('node_modules')
102-
await symlink(
103-
P.join('..', `nuxt${version}`, 'node_modules'),
104-
'node_modules'
105-
)
114+
if (config.nuxtVersion === 3) {
115+
await execa.command('yarn add --dev nuxt@3.0.0-rc.12')
106116
await outputFile(
107117
'nuxt.config.js',
108118
`export default ${JSON.stringify({
109-
build: { quiet: false },
110-
rootDir: process.cwd(),
111119
telemetry: false,
112-
...versions[version].transformNuxtConfig(config.nuxtConfig),
120+
...config.nuxtConfig,
113121
})}`
114122
)
115123

@@ -118,17 +126,33 @@ export default tester(
118126
'.bin',
119127
packageName`nuxt`
120128
)
121-
await execa(nuxtPath, ['build'], { stdio: 'inherit' })
129+
await execa(nuxtPath, ['build'])
122130

123-
const childProcess = execa(nuxtPath, ['start'], {
124-
stdio: 'inherit',
125-
})
131+
const childProcess = execa(nuxtPath, ['start'])
126132
await delay(5000)
127133
try {
128134
await config.test.call(this)
129135
} finally {
130136
await kill(childProcess.pid)
131137
}
138+
} else {
139+
const nuxt = new Nuxt({
140+
dev: false,
141+
...config.nuxtConfig,
142+
})
143+
if (config.error) {
144+
await expect(new Builder(nuxt).build()).rejects.toThrow(
145+
config.error
146+
)
147+
} else {
148+
await new Builder(nuxt).build()
149+
await nuxt.listen()
150+
try {
151+
await config.test.call(this)
152+
} finally {
153+
await nuxt.close()
154+
}
155+
}
132156
}
133157
}
134158
},

0 commit comments

Comments
 (0)