diff --git a/lib/index.js b/lib/index.js index 0c1fb27da..f1dd0ddc7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -128,19 +128,24 @@ module.exports = async function lintStaged( printTaskOutput(ctx, logger) return true } catch (runAllError) { - const { ctx } = runAllError - if (ctx.errors.has(ApplyEmptyCommitError)) { - logger.warn(PREVENTED_EMPTY_COMMIT) - } else if (ctx.errors.has(GitError) && !ctx.errors.has(GetBackupStashError)) { - logger.error(GIT_ERROR) - if (ctx.shouldBackup) { - // No sense to show this if the backup stash itself is missing. - logger.error(RESTORE_STASH_EXAMPLE) + if (runAllError && runAllError.ctx && runAllError.ctx.errors) { + const { ctx } = runAllError + if (ctx.errors.has(ApplyEmptyCommitError)) { + logger.warn(PREVENTED_EMPTY_COMMIT) + } else if (ctx.errors.has(GitError) && !ctx.errors.has(GetBackupStashError)) { + logger.error(GIT_ERROR) + if (ctx.shouldBackup) { + // No sense to show this if the backup stash itself is missing. + logger.error(RESTORE_STASH_EXAMPLE) + } } + + printTaskOutput(ctx, logger) + return false } - printTaskOutput(ctx, logger) - return false + // Probably a compilation error in the config js file. Pass it up to the outer error handler for logging. + throw runAllError } } catch (lintStagedError) { if (lintStagedError === errConfigNotFound) { diff --git a/test/__snapshots__/index2.spec.js.snap b/test/__snapshots__/index2.spec.js.snap new file mode 100644 index 000000000..0a25f136a --- /dev/null +++ b/test/__snapshots__/index2.spec.js.snap @@ -0,0 +1,11 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`lintStaged should catch errors from js function config 2`] = ` +" +ERROR Could not parse lint-staged config. + +Error: failed config +ERROR +ERROR Please make sure you have created it correctly. +See https://github.com/okonet/lint-staged#configuration." +`; diff --git a/test/index2.spec.js b/test/index2.spec.js index 7dda8c6e3..bdeafcbfe 100644 --- a/test/index2.spec.js +++ b/test/index2.spec.js @@ -64,4 +64,19 @@ describe('lintStaged', () => { } `) }) + + it('should catch errors from js function config', async () => { + const logger = makeConsoleMock() + const config = { + '*': () => { + throw new Error('failed config') + }, + } + + expect.assertions(2) + await expect(lintStaged({ config }, logger)).rejects.toThrowErrorMatchingInlineSnapshot( + `"failed config"` + ) + expect(logger.printHistory()).toMatchSnapshot() + }) })