@@ -16,18 +16,33 @@ jest.mock(`../utils/`, () => {
16
16
} )
17
17
18
18
jest . 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
28
34
}
35
+
36
+ sharp . metadataMock = metadataMock
37
+
38
+ return sharp
29
39
} )
30
40
41
+ const sharp = require ( `sharp` )
42
+ const mockSharpFailure = ( ) => {
43
+ sharp . metadataMock . mockRejectedValueOnce ( new Error ( `invalid image` ) )
44
+ }
45
+
31
46
const createNode = content => {
32
47
const node = {
33
48
id : 1234 ,
@@ -131,7 +146,6 @@ test(`it transforms images in markdown`, async () => {
131
146
const content = `
132
147

133
148
` . trim ( )
134
-
135
149
const nodes = await plugin ( createPluginOptions ( content , imagePath ) )
136
150
137
151
expect ( nodes . length ) . toBe ( 1 )
@@ -237,3 +251,25 @@ test(`it transforms images in markdown with webp srcSets if option is enabled`,
237
251
expect ( node . value ) . toMatchSnapshot ( )
238
252
expect ( node . value ) . not . toMatch ( `<html>` )
239
253
} )
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