1
- import { delay , endent , identity , mapValues } from '@dword-design/functions'
1
+ import { delay , endent } from '@dword-design/functions'
2
2
import tester from '@dword-design/tester'
3
3
import testerPluginPuppeteer from '@dword-design/tester-plugin-puppeteer'
4
4
import testerPluginTmpDir from '@dword-design/tester-plugin-tmp-dir'
5
5
import packageName from 'depcheck-package-name'
6
6
import execa from 'execa'
7
- import { outputFile , remove , symlink } from 'fs-extra'
7
+ import { outputFile } from 'fs-extra'
8
+ import { Builder , Nuxt } from 'nuxt'
8
9
import outputFiles from 'output-files'
9
10
import P from 'path'
10
11
import kill from 'tree-kill-promise'
11
12
12
13
export default tester (
13
14
{
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
+ } ,
14
59
works : {
15
60
files : {
16
61
modules : {
@@ -51,65 +96,28 @@ export default tester(
51
96
} ,
52
97
async test ( ) {
53
98
await this . page . goto ( 'http://localhost:3000' )
54
- console . log ( await this . page . content ( ) )
55
99
await this . page . waitForSelector ( '.bar' )
56
100
} ,
57
101
} ,
58
102
} ,
59
103
[
60
104
testerPluginTmpDir ( ) ,
61
105
{
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
- } ,
71
106
transform : config => {
72
- config = { test : ( ) => { } , ...config }
107
+ config = { nuxtConfig : { } , nuxtVersion : 2 , test : ( ) => { } , ...config }
73
108
74
109
return async function ( ) {
75
110
await outputFiles ( {
76
111
'package.json' : JSON . stringify ( { type : 'module' } ) ,
77
112
...config . files ,
78
113
} )
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' )
106
116
await outputFile (
107
117
'nuxt.config.js' ,
108
118
`export default ${ JSON . stringify ( {
109
- build : { quiet : false } ,
110
- rootDir : process . cwd ( ) ,
111
119
telemetry : false ,
112
- ...versions [ version ] . transformNuxtConfig ( config . nuxtConfig ) ,
120
+ ...config . nuxtConfig ,
113
121
} ) } `
114
122
)
115
123
@@ -118,17 +126,33 @@ export default tester(
118
126
'.bin' ,
119
127
packageName `nuxt`
120
128
)
121
- await execa ( nuxtPath , [ 'build' ] , { stdio : 'inherit' } )
129
+ await execa ( nuxtPath , [ 'build' ] )
122
130
123
- const childProcess = execa ( nuxtPath , [ 'start' ] , {
124
- stdio : 'inherit' ,
125
- } )
131
+ const childProcess = execa ( nuxtPath , [ 'start' ] )
126
132
await delay ( 5000 )
127
133
try {
128
134
await config . test . call ( this )
129
135
} finally {
130
136
await kill ( childProcess . pid )
131
137
}
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
+ }
132
156
}
133
157
}
134
158
} ,
0 commit comments