diff --git a/demo/src/pages/manual-tests/varid-data-no-logs.js b/demo/src/pages/manual-tests/varid-data-no-logs.js new file mode 100644 index 0000000..09c920b --- /dev/null +++ b/demo/src/pages/manual-tests/varid-data-no-logs.js @@ -0,0 +1,41 @@ +import React from 'react'; +import { graphql, useStaticQuery } from 'gatsby'; +import { GatsbyImage, getImage } from 'gatsby-plugin-image'; + +const VariedDataPage = () => { + const data = useStaticQuery(graphql` + query { + allVariedData { + nodes { + name + expected + gatsbyImageData( + height: 200 + backgroundColor: "#BADA55" + logLevel: "error" + ) + } + } + } + `); + + return data.allVariedData.nodes.map((node, index) => { + const gatsbyImage = getImage(node); + + return ( + <> +

{node.name}

+
+ Expected: {node.expected} +
+ {gatsbyImage ? ( + + ) : ( +
No image for node with name: {node.name}
+ )} + + ); + }); +}; + +export default VariedDataPage; diff --git a/plugin/gatsby-plugin-image/resolve-asset.js b/plugin/gatsby-plugin-image/resolve-asset.js index 78d0d2b..487268d 100644 --- a/plugin/gatsby-plugin-image/resolve-asset.js +++ b/plugin/gatsby-plugin-image/resolve-asset.js @@ -11,6 +11,8 @@ const { } = require('./asset-data'); const { Joi } = require('gatsby-plugin-utils/joi'); +const { resolverReporter } = require('./resolver-reporter'); + const generateCloudinaryAssetSource = ( filename, width, @@ -94,7 +96,8 @@ exports._generateCloudinaryAssetSource = generateCloudinaryAssetSource; exports.createResolveCloudinaryAssetData = (gatsbyUtils) => async (source, args, _context, info) => { - const { reporter } = gatsbyUtils; + let { reporter } = gatsbyUtils; + reporter = resolverReporter({ reporter, logLevel: args.logLevel }); const transformType = info.parentType || 'UnknownTransformType'; const schema = Joi.object({ diff --git a/plugin/gatsby-plugin-image/resolve-asset.test.js b/plugin/gatsby-plugin-image/resolve-asset.test.js index 909d818..5bfc88e 100644 --- a/plugin/gatsby-plugin-image/resolve-asset.test.js +++ b/plugin/gatsby-plugin-image/resolve-asset.test.js @@ -218,21 +218,61 @@ describe('resolveCloudinaryAssetData', () => { expect(result).toBe(null); }); - it('calls reporter.verbose and returns null for weird source', async () => { - const source = { - one: 'thing', - another: 'thang', - }; - const args = {}; - const result = await resolveCloudinaryAssetData( - source, - args, - context, - info - ); - expect(generateImageData).toBeCalledTimes(0); - expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledTimes(1); - expect(result).toBe(null); + describe('returns null for weird source', () => { + it('calls reporter.verbose when undefined log level', async () => { + const source = { + one: 'thing', + another: 'thang', + }; + const args = {}; + const result = await resolveCloudinaryAssetData( + source, + args, + context, + info + ); + expect(generateImageData).toBeCalledTimes(0); + expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledTimes(1); + expect(result).toBe(null); + }); + + it('calls reporter.verbose when log level = "verbose"', async () => { + const source = { + one: 'thing', + another: 'thang', + }; + const args = { + logLevel: 'verbose', + }; + const result = await resolveCloudinaryAssetData( + source, + args, + context, + info + ); + expect(generateImageData).toBeCalledTimes(0); + expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledTimes(1); + expect(result).toBe(null); + }); + + it('does not call reporter.verbose when log level = "warn"', async () => { + const source = { + one: 'thing', + another: 'thang', + }; + const args = { + logLevel: 'warn', + }; + const result = await resolveCloudinaryAssetData( + source, + args, + context, + info + ); + expect(generateImageData).toBeCalledTimes(0); + expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledTimes(0); + expect(result).toBe(null); + }); }); it('calls reporter.verbose and returns null for null source', async () => { @@ -263,21 +303,42 @@ describe('resolveCloudinaryAssetData', () => { expect(result).toBe(null); }); - it('calls reporter.warn and returns null for missing data', async () => { - const source = { - publicId: 'publicId', - one: 'thing', - }; - const args = {}; - const result = await resolveCloudinaryAssetData( - source, - args, - context, - info - ); - expect(generateImageData).toBeCalledTimes(0); - expect(gatsbyUtilsMocks.reporter.warn).toBeCalledTimes(1); - expect(result).toBe(null); + describe('returns null for missing data', () => { + it('returns null for missing data when log level is undefined', async () => { + const source = { + publicId: 'publicId', + one: 'thing', + }; + const args = {}; + const result = await resolveCloudinaryAssetData( + source, + args, + context, + info + ); + expect(generateImageData).toBeCalledTimes(0); + expect(gatsbyUtilsMocks.reporter.warn).toBeCalledTimes(1); + expect(result).toBe(null); + }); + + it('does not call reporter.warn when log level = "error"', async () => { + const source = { + publicId: 'publicId', + one: 'thing', + }; + const args = { + logLevel: 'error', + }; + const result = await resolveCloudinaryAssetData( + source, + args, + context, + info + ); + expect(generateImageData).toBeCalledTimes(0); + expect(gatsbyUtilsMocks.reporter.warn).toBeCalledTimes(0); + expect(result).toBe(null); + }); }); }); diff --git a/plugin/gatsby-plugin-image/resolver-reporter.js b/plugin/gatsby-plugin-image/resolver-reporter.js new file mode 100644 index 0000000..d17999d --- /dev/null +++ b/plugin/gatsby-plugin-image/resolver-reporter.js @@ -0,0 +1,25 @@ +const LEVEL = { + verbose: 0, + info: 1, + warn: 2, + error: 3, + panic: 4, + panicOnBuild: 4, +}; + +exports.resolverReporter = ({ reporter, logLevel }) => { + const log = (level, message, ...rest) => { + if (!logLevel || LEVEL[level] >= LEVEL[logLevel]) { + reporter[level](message, ...rest); + } + }; + + return { + verbose: (...args) => log('verbose', ...args), + info: (...args) => log('info', ...args), + warn: (...args) => log('warn', ...args), + error: (...args) => log('error', ...args), + panic: (...args) => log('panic', ...args), + panicOnBuild: (...args) => log('panicOnBuild', ...args), + }; +}; diff --git a/plugin/gatsby-plugin-image/resolver-reporter.test.js b/plugin/gatsby-plugin-image/resolver-reporter.test.js new file mode 100644 index 0000000..6ab36e0 --- /dev/null +++ b/plugin/gatsby-plugin-image/resolver-reporter.test.js @@ -0,0 +1,143 @@ +const { resolverReporter } = require('./resolver-reporter'); + +const gatsbyUtilsMocks = { + reporter: { + panicOnBuild: jest.fn(), + panic: jest.fn(), + error: jest.fn(), + warn: jest.fn(), + info: jest.fn(), + verbose: jest.fn(), + }, +}; + +describe('resolverReporter', () => { + it('reports only panic', () => { + const reporter = resolverReporter({ + reporter: gatsbyUtilsMocks.reporter, + logLevel: 'panic', + }); + + reporter.panicOnBuild('message'); + reporter.panic('message'); + reporter.error('message'); + reporter.warn('message'); + reporter.info('message'); + reporter.verbose('message'); + + expect(gatsbyUtilsMocks.reporter.panic).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.panicOnBuild).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.error).toBeCalledTimes(0); + expect(gatsbyUtilsMocks.reporter.warn).toBeCalledTimes(0); + expect(gatsbyUtilsMocks.reporter.info).toBeCalledTimes(0); + expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledTimes(0); + }); + + it('reports only errors and above', () => { + const reporter = resolverReporter({ + reporter: gatsbyUtilsMocks.reporter, + logLevel: 'error', + }); + + reporter.panicOnBuild('message'); + reporter.panic('message'); + reporter.error('message'); + reporter.warn('message'); + reporter.info('message'); + reporter.verbose('message'); + + expect(gatsbyUtilsMocks.reporter.panic).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.panicOnBuild).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.error).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.warn).toBeCalledTimes(0); + expect(gatsbyUtilsMocks.reporter.info).toBeCalledTimes(0); + expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledTimes(0); + }); + + it('reports only warnings and above', () => { + const reporter = resolverReporter({ + reporter: gatsbyUtilsMocks.reporter, + logLevel: 'warn', + }); + + reporter.panicOnBuild('message'); + reporter.panic('message'); + reporter.error('message'); + reporter.warn('message'); + reporter.info('message'); + reporter.verbose('message'); + + expect(gatsbyUtilsMocks.reporter.panic).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.panicOnBuild).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.error).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.warn).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.info).toBeCalledTimes(0); + expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledTimes(0); + }); + + it('reports only info and above', () => { + const reporter = resolverReporter({ + reporter: gatsbyUtilsMocks.reporter, + logLevel: 'info', + }); + + reporter.panicOnBuild('message'); + reporter.panic('message'); + reporter.error('message'); + reporter.warn('message'); + reporter.info('message'); + reporter.verbose('message'); + + expect(gatsbyUtilsMocks.reporter.panic).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.panicOnBuild).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.error).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.warn).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.info).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledTimes(0); + }); + + describe('reports all', () => { + it('when log level is verbose', () => { + const reporter = resolverReporter({ + reporter: gatsbyUtilsMocks.reporter, + logLevel: 'verbose', + }); + + reporter.panicOnBuild('message'); + reporter.panic('message'); + reporter.error('message'); + reporter.warn('message'); + reporter.info('message'); + reporter.verbose('message'); + + expect(gatsbyUtilsMocks.reporter.panic).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.panic).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.panicOnBuild).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.error).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.warn).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.info).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledWith('message'); + }); + + it('when log level is undefined', () => { + const reporter = resolverReporter({ + reporter: gatsbyUtilsMocks.reporter, + }); + + reporter.panicOnBuild('message'); + reporter.panic('message'); + reporter.error('message'); + reporter.warn('message'); + reporter.info('message'); + reporter.verbose('message'); + + expect(gatsbyUtilsMocks.reporter.panic).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.panic).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.panicOnBuild).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.error).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.warn).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.info).toBeCalledWith('message'); + expect(gatsbyUtilsMocks.reporter.verbose).toBeCalledWith('message'); + }); + }); +}); diff --git a/plugin/gatsby-plugin-image/resolvers.js b/plugin/gatsby-plugin-image/resolvers.js index 052b832..c9503fd 100644 --- a/plugin/gatsby-plugin-image/resolvers.js +++ b/plugin/gatsby-plugin-image/resolvers.js @@ -22,6 +22,9 @@ exports.createGatsbyPluginImageResolver = (gatsbyUtils, pluginOptions) => { type: 'Boolean', defaultValue: true, }, + logLevel: { + type: 'String', + }, } ); } catch (error) {