Skip to content

Commit 59b20bb

Browse files
committed
fix: Update Page.js tests
1 parent cbe1af0 commit 59b20bb

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

packages/@vuepress/core/lib/node/Page.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,19 @@ module.exports = class Page {
289289
const enhancerPromises = []
290290

291291
for (const { name: pluginName, value: enhancer } of enhancers) {
292-
try {
293-
enhancerPromises.push(enhancer(this))
294-
} catch (error) {
295-
console.log(error)
296-
throw new Error(`[${pluginName}] execute extendPageData failed.`)
297-
}
292+
const enhancerResult = enhancer(this)
293+
const promise = enhancerResult instanceof Promise ? enhancerResult : Promise.resolve(enhancerResult)
294+
295+
enhancerPromises.push(promise.catch(e => {
296+
console.log(e)
297+
return new Error(`[${pluginName}] execute extendPageData failed.`)
298+
}))
298299
}
299300

300-
return Promise.all(enhancerPromises)
301+
const results = await Promise.all(enhancerPromises)
302+
const enhancerError = results.find(result => result instanceof Error)
303+
if (enhancerError) throw enhancerError
304+
305+
return results
301306
}
302307
}

packages/@vuepress/core/lib/node/__tests__/prepare/Page.spec.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ describe('Page', () => {
111111
page = new Page({ path: '/' }, app)
112112
enhancers = [
113113
{
114-
pluginName: 'foo',
114+
name: 'foo',
115115
value: jest.fn()
116116
},
117117
{
118-
pluginName: 'foo',
118+
name: 'bar',
119119
value: jest.fn()
120120
}
121121
]
@@ -130,21 +130,22 @@ describe('Page', () => {
130130

131131
test('should loop over sync and async enhancers', async () => {
132132
const mixedEnhancers = [...enhancers, {
133-
pluginName: 'blog',
133+
name: 'blog',
134134
value: jest.fn().mockResolvedValue({})
135135
}]
136136
await page.enhance(mixedEnhancers)
137137

138138
return mixedEnhancers.map(enhancer => expect(enhancer.value).toHaveBeenCalled())
139139
})
140140

141-
test('should log when enhancing when failing', async () => {
141+
test('should log and throw an error when enhancing fails', async () => {
142142
const error = { errorMessage: 'this is an error message' }
143+
const pluginName = 'error-plugin'
143144

144145
await expect(page.enhance([{
145-
pluginName: 'error-plugin',
146+
name: pluginName,
146147
value: jest.fn().mockRejectedValue(error)
147-
}])).rejects.toThrow()
148+
}])).rejects.toThrowError(`[${pluginName}] execute extendPageData failed.`)
148149

149150
expect(console.log).toHaveBeenCalledWith(error)
150151
})

0 commit comments

Comments
 (0)