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 @@ -244,14 +244,14 @@ module.exports = class Page {
244
244
* @api private
245
245
*/
246
246
247
- enhance ( enhancers ) {
248
- return enhancers . reduce ( async ( accumulator , { pluginName, value } ) => {
249
- return accumulator
250
- . then ( ( ) => value ( this ) )
251
- . catch ( error => {
252
- console . log ( error )
253
- throw new Error ( `[${ pluginName } ] execute extendPageData failed.` )
254
- } )
255
- } , Promise . resolve ( ) )
247
+ async enhance ( enhancers ) {
248
+ for ( const { name : pluginName , value : enhancer } of enhancers ) {
249
+ try {
250
+ await enhancer ( this )
251
+ } catch ( error ) {
252
+ console . log ( error )
253
+ throw new Error ( `[${ pluginName } ] execute extendPageData failed.` )
254
+ }
255
+ }
256
256
}
257
257
}
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