File tree 2 files changed +58
-9
lines changed
packages/@vuepress/core/lib/node
2 files changed +58
-9
lines changed Original file line number Diff line number Diff line change @@ -282,14 +282,14 @@ module.exports = class Page {
282
282
* @api private
283
283
*/
284
284
285
- enhance ( enhancers ) {
286
- return enhancers . reduce ( async ( accumulator , { pluginName, value } ) => {
287
- return accumulator
288
- . then ( ( ) => value ( this ) )
289
- . catch ( error => {
290
- console . log ( error )
291
- throw new Error ( `[${ pluginName } ] execute extendPageData failed.` )
292
- } )
293
- } , Promise . resolve ( ) )
285
+ async enhance ( enhancers ) {
286
+ for ( const { name : pluginName , value : enhancer } of enhancers ) {
287
+ try {
288
+ await enhancer ( this )
289
+ } catch ( error ) {
290
+ console . log ( error )
291
+ throw new Error ( `[${ pluginName } ] execute extendPageData failed.` )
292
+ }
293
+ }
294
294
}
295
295
}
Original file line number Diff line number Diff line change @@ -102,5 +102,54 @@ describe('Page', () => {
102
102
expect ( page . _content . startsWith ( '---' ) ) . toBe ( true )
103
103
expect ( page . _strippedContent . startsWith ( '---' ) ) . toBe ( false )
104
104
} )
105
+
106
+ describe ( 'enhance - ' , ( ) => {
107
+ let page
108
+ let enhancers
109
+
110
+ beforeEach ( ( ) => {
111
+ page = new Page ( { path : '/' } , app )
112
+ enhancers = [
113
+ {
114
+ pluginName : 'foo' ,
115
+ value : jest . fn ( )
116
+ } ,
117
+ {
118
+ pluginName : 'foo' ,
119
+ value : jest . fn ( )
120
+ }
121
+ ]
122
+ global . console . log = jest . fn ( )
123
+ } )
124
+
125
+ test ( 'should loop over sync enhancers' , async ( ) => {
126
+ await page . enhance ( enhancers )
127
+
128
+ return enhancers . map ( enhancer => expect ( enhancer . value ) . toBeCalled ( ) )
129
+ } )
130
+
131
+ test ( 'should loop over sync and async enhancers' , async ( ) => {
132
+ const mixedEnhancers = [ ...enhancers , {
133
+ pluginName : 'blog' ,
134
+ value : jest . fn ( ) . mockResolvedValue ( { } )
135
+ } ]
136
+ await page . enhance ( mixedEnhancers )
137
+
138
+ return mixedEnhancers . map ( enhancer => expect ( enhancer . value ) . toBeCalled ( ) )
139
+ } )
140
+
141
+ test ( 'should log when enhancing when failing' , async ( ) => {
142
+ const error = { errorMessage : 'this is an error message' }
143
+ expect . assertions ( 1 )
144
+ try {
145
+ await page . enhance ( [ {
146
+ pluginName : 'error-plugin' ,
147
+ value : jest . fn ( ) . mockRejectedValue ( error )
148
+ } ] )
149
+ } catch ( e ) {
150
+ expect ( console . log ) . toBeCalledWith ( error )
151
+ }
152
+ } )
153
+ } )
105
154
} )
106
155
You can’t perform that action at this time.
0 commit comments