diff --git a/packages/gatsby-remark-images-contentful/src/__tests__/index.js b/packages/gatsby-remark-images-contentful/src/__tests__/index.js index f093ecc237bc2..2da3628d2395c 100644 --- a/packages/gatsby-remark-images-contentful/src/__tests__/index.js +++ b/packages/gatsby-remark-images-contentful/src/__tests__/index.js @@ -1,11 +1,3 @@ -const Remark = require(`remark`) -const plugin = require(`../`) -const remark = new Remark().data(`settings`, { - commonmark: true, - footnotes: true, - pedantic: true, -}) - jest.mock(`../utils/`, () => { return { getBase64Img: jest.fn().mockReturnValue(`data:image;`), @@ -23,6 +15,19 @@ jest.mock(`../utils/`, () => { } }) +jest.mock(`axios`) +jest.mock(`sharp`, () => () => { + return { + metadata: jest.fn(() => { + return { + width: 200, + height: 200, + density: 75, + } + }), + } +}) + const createNode = content => { const node = { id: 1234, @@ -47,7 +52,7 @@ const createNode = content => { return markdownNode } -const createPluginOptions = (content, imagePaths = `/`) => { +const createPluginOptions = (content, imagePaths = `/`, options = {}) => { const dirName = `not-a-real-dir` return { files: [].concat(imagePaths).map(imagePath => { @@ -64,28 +69,28 @@ const createPluginOptions = (content, imagePaths = `/`) => { } }, createContentDigest: jest.fn().mockReturnValue(`contentDigest`), + ...options, } } - -jest.mock(`axios`, () => () => - Promise.resolve({ - data: { - pipe: jest.fn(), - destroy: jest.fn(), - }, - }) -) - -jest.mock(`sharp`, () => () => { - return { - metadata: jest.fn(() => { - return { - width: 200, - height: 200, - density: 75, - } - }), - } +const Remark = require(`remark`) +const plugin = require(`../index`) +const remark = new Remark().data(`settings`, { + commonmark: true, + footnotes: true, + pedantic: true, +}) +const axios = require(`axios`) + +beforeEach(() => { + axios.mockClear() + axios.mockImplementation(() => + Promise.resolve({ + data: { + pipe: jest.fn(), + destroy: jest.fn(), + }, + }) + ) }) test(`it returns empty array when 0 images`, async () => { @@ -153,6 +158,24 @@ test(`it transforms images with a https scheme in markdown`, async () => { expect(node.value).not.toMatch(``) }) +test(`it throws specific error if the image is not found`, async () => { + axios.mockImplementationOnce(() => Promise.reject(new Error(`oh no`))) + const reporter = { + panic: jest.fn(), + } + const imagePath = `https://images.ctfassets.net/rocybtov1ozk/wtrHxeu3zEoEce2MokCSi/73dce36715f16e27cf5ff0d2d97d7dff/doesnotexist.jpg` + const content = ` +![image](${imagePath}) + `.trim() + + await plugin(createPluginOptions(content, imagePath, { reporter })) + expect(reporter.panic).toHaveBeenCalledTimes(1) + expect(reporter.panic).toHaveBeenCalledWith( + `Image downloading failed for ${imagePath}, please check if the image still exists on contentful`, + expect.any(Error) + ) +}) + test(`it transforms multiple images in markdown`, async () => { const imagePaths = [ `//images.ctfassets.net/rocybtov1ozk/wtrHxeu3zEoEce2MokCSi/73dce36715f16e27cf5ff0d2d97d7dff/quwowooybuqbl6ntboz3.jpg`, diff --git a/packages/gatsby-remark-images-contentful/src/index.js b/packages/gatsby-remark-images-contentful/src/index.js index 34b8b795b19f0..d56d95e61957a 100644 --- a/packages/gatsby-remark-images-contentful/src/index.js +++ b/packages/gatsby-remark-images-contentful/src/index.js @@ -70,11 +70,20 @@ module.exports = async ( } const metaReader = sharp() - const response = await axios({ - method: `GET`, - url: originalImg, // for some reason there is a './' prefix - responseType: `stream`, - }) + let response + try { + response = await axios({ + method: `GET`, + url: originalImg, // for some reason there is a './' prefix + responseType: `stream`, + }) + } catch (err) { + reporter.panic( + `Image downloading failed for ${originalImg}, please check if the image still exists on contentful`, + err + ) + return [] + } response.data.pipe(metaReader)