diff --git a/packages/gatsby-plugin-sharp/src/__tests__/index.js b/packages/gatsby-plugin-sharp/src/__tests__/index.js index 9d4800e5879bf..ac0af9d4c42e4 100644 --- a/packages/gatsby-plugin-sharp/src/__tests__/index.js +++ b/packages/gatsby-plugin-sharp/src/__tests__/index.js @@ -1,6 +1,6 @@ const path = require(`path`) -const { base64, responsiveSizes } = require(`../`) +const { base64, responsiveSizes, resolutions } = require(`../`) describe(`gatsby-plugin-sharp`, () => { const args = { @@ -73,6 +73,42 @@ describe(`gatsby-plugin-sharp`, () => { }) }) + describe(`resolutions`, () => { + console.warn = jest.fn() + + beforeEach(() => { + console.warn.mockClear() + }) + + afterAll(() => { + console.warn.mockClear() + }) + + it(`does not warn when the requested width is equal to the image width`, async () => { + const args = { width: 1 } + + const result = await resolutions({ + file, + args, + }) + + expect(result.width).toEqual(1) + expect(console.warn).toHaveBeenCalledTimes(0) + }) + + it(`warns when the requested width is greater than the image width`, async () => { + const args = { width: 2 } + + const result = await resolutions({ + file, + args, + }) + + expect(result.width).toEqual(1) + expect(console.warn).toHaveBeenCalledTimes(1) + }) + }) + describe(`base64`, () => { it(`converts image to base64`, async () => { const result = await base64({ diff --git a/packages/gatsby-plugin-sharp/src/index.js b/packages/gatsby-plugin-sharp/src/index.js index ebe27c7bef9a8..953e3cda8abdb 100644 --- a/packages/gatsby-plugin-sharp/src/index.js +++ b/packages/gatsby-plugin-sharp/src/index.js @@ -517,7 +517,7 @@ async function resolutions({ file, args = {} }) { sizes.push(options.width * 3) const dimensions = imageSize(file.absolutePath) - const filteredSizes = sizes.filter(size => size < dimensions.width) + const filteredSizes = sizes.filter(size => size <= dimensions.width) // If there's no sizes after filtering (e.g. image is smaller than what's // requested, add back the original so there's at least something)