Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 51e1b14

Browse files
author
Peter Bengtsson
authored
improve test debugging and loading of cached redirects json file (#23584)
* improve test debugging and loading of cached redirects json file * exception for testing 500 page itself
1 parent 3e6eb01 commit 51e1b14

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

lib/redirects/precompile.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ function diskMemoize(filePath, asyncFn, maxAgeSeconds = 60 * 60) {
2727
}
2828
log(`Redirects disk-cache ${filePath} too old`)
2929
} catch (err) {
30-
if (err.code !== 'ENOENT') throw err
30+
if (err instanceof SyntaxError) {
31+
console.warn(`Syntax error when trying to JSON parse the ${filePath}`, err)
32+
} else if (err.code !== 'ENOENT') throw err
3133
}
3234
log(`Redirects disk-cache MISS on ${filePath}`)
3335
const promise = asyncFn(...args)

middleware/handle-errors.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ async function logException(error, req) {
2727
}
2828

2929
export default async function handleError(error, req, res, next) {
30+
// When you run tests that use things doing get() requests in
31+
// our supertest handler, if something goes wrong anywhere in the app
32+
// and its middlewares, you get a 500 but the error is never displayed
33+
// anywhere. So this is why we log it additionally.
34+
// Note, not using console.error() because it's arguably handled.
35+
// Some tests might actually expect a 500 error.
36+
if (process.env.NODE_ENV === 'test') {
37+
console.warn('An error occurrred in some middleware handler', error)
38+
}
39+
3040
try {
3141
// If the headers have already been sent or the request was aborted...
3242
if (res.headersSent || req.aborted) {

tests/helpers/supertest.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ export const head = (helpers.head = async function (route, opts = { followRedire
3434

3535
export const post = (helpers.post = (route) => request('post', route))
3636

37-
export const getDOM = (helpers.getDOM = async function (route, headers) {
37+
export const getDOM = (helpers.getDOM = async function (route, headers, allow500s = false) {
3838
const res = await helpers.get(route, { followRedirects: true, headers })
39+
if (!allow500s && res.status >= 500) {
40+
throw new Error(`Server error (${res.status}) on ${route}`)
41+
}
3942
const $ = cheerio.load(res.text || '', { xmlMode: true })
4043
$.res = Object.assign({}, res)
4144
return $
@@ -45,5 +48,8 @@ export const getDOM = (helpers.getDOM = async function (route, headers) {
4548
// e.g. await getJSON('/en?json=breadcrumbs')
4649
export const getJSON = (helpers.getJSON = async function (route) {
4750
const res = await helpers.get(route, { followRedirects: true })
51+
if (res.status >= 500) {
52+
throw new Error(`Server error (${res.status}) on ${route}`)
53+
}
4854
return JSON.parse(res.text)
4955
})

tests/rendering/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ describe('server', () => {
183183
})
184184

185185
test('renders a 500 page when errors are thrown', async () => {
186-
const $ = await getDOM('/_500')
186+
const $ = await getDOM('/_500', undefined, true)
187187
expect($('h1').text()).toBe('Ooops!')
188188
expect($.text().includes('It looks like something went wrong.')).toBe(true)
189189
expect(

0 commit comments

Comments
 (0)