@@ -16,18 +16,33 @@ jest.mock(`../utils/`, () => {
1616} )
1717
1818jest . mock ( `axios` )
19- jest . mock ( `sharp` , ( ) => ( ) => {
20- return {
21- metadata : jest . fn ( ( ) => {
22- return {
23- width : 200 ,
24- height : 200 ,
25- density : 75 ,
26- }
27- } ) ,
19+
20+ jest . mock ( `sharp` , ( ) => {
21+ const metadataMock = jest . fn ( ( ) => {
22+ return {
23+ width : 200 ,
24+ height : 200 ,
25+ density : 75 ,
26+ }
27+ } )
28+
29+ const sharp = ( ) => {
30+ const pipeline = {
31+ metadata : metadataMock ,
32+ }
33+ return pipeline
2834 }
35+
36+ sharp . metadataMock = metadataMock
37+
38+ return sharp
2939} )
3040
41+ const sharp = require ( `sharp` )
42+ const mockSharpFailure = ( ) => {
43+ sharp . metadataMock . mockRejectedValueOnce ( new Error ( `invalid image` ) )
44+ }
45+
3146const createNode = content => {
3247 const node = {
3348 id : 1234 ,
@@ -131,7 +146,6 @@ test(`it transforms images in markdown`, async () => {
131146 const content = `
132147
133148 ` . trim ( )
134-
135149 const nodes = await plugin ( createPluginOptions ( content , imagePath ) )
136150
137151 expect ( nodes . length ) . toBe ( 1 )
@@ -237,3 +251,25 @@ test(`it transforms images in markdown with webp srcSets if option is enabled`,
237251 expect ( node . value ) . toMatchSnapshot ( )
238252 expect ( node . value ) . not . toMatch ( `<html>` )
239253} )
254+
255+ test ( `it shows an useful error message when the file is not a valid image` , async ( ) => {
256+ mockSharpFailure ( )
257+
258+ const imagePath = `//images.ctfassets.net/k8iqpp6u0ior/752jwCIe9dwtfi9mLbp9m2/bc588ee25cf8299bc33a56ca32f8677b/Gatsby-Logos.zip`
259+
260+ const content = `
261+ 
262+ ` . trim ( )
263+
264+ const reporter = {
265+ panic : jest . fn ( ) ,
266+ }
267+
268+ await plugin ( createPluginOptions ( content , imagePath , { reporter } ) )
269+
270+ expect ( reporter . panic ) . toHaveBeenCalledTimes ( 1 )
271+ expect ( reporter . panic ) . toHaveBeenCalledWith (
272+ `The image "${ imagePath } " (with alt text: "image") doesn't appear to be a supported image format.` ,
273+ expect . any ( Error )
274+ )
275+ } )
0 commit comments