Repository: okonet/lint-staged
Pull request #935 (2020-12-04 14:12:36)

better logging for js config errors
Added tests for the original PR https://github.com/okonet/lint-staged/pull/915

iiroj: Ping @okonet I added tests based on the original PR https://github.com/okonet/lint-staged/pull/915, so that we can get it merged.
codecov[bot]: # [Codecov](https://codecov.io/gh/okonet/lint-staged/pull/935?src=pr&el=h1) Report
> Merging [#935](https://codecov.io/gh/okonet/lint-staged/pull/935?src=pr&el=desc) (4a5dc99) into [master](https://codecov.io/gh/okonet/lint-staged/commit/0ff29178c8771ab10e43a395e252c6a118306942?el=desc) (0ff2917) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/okonet/lint-staged/pull/935/graphs/tree.svg?width=650&height=150&src=pr&token=LSoEgsjk4O)](https://codecov.io/gh/okonet/lint-staged/pull/935?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##            master      #935   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           18        18           
  Lines          609       611    +2     
  Branches       143       144    +1     
=========================================
+ Hits           609       611    +2     
```


| [Impacted Files](https://codecov.io/gh/okonet/lint-staged/pull/935?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [lib/index.js](https://codecov.io/gh/okonet/lint-staged/pull/935/diff?src=pr&el=tree#diff-bGliL2luZGV4Lmpz) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/okonet/lint-staged/pull/935?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/okonet/lint-staged/pull/935?src=pr&el=footer). Last update [0ff2917...4a5dc99](https://codecov.io/gh/okonet/lint-staged/pull/935?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/lib/index.js b/lib/index.js
index 0c1fb27..f1dd0dd 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) {


Test patch:
diff --git a/test/__snapshots__/index2.spec.js.snap b/test/__snapshots__/index2.spec.js.snap
new file mode 100644
index 0000000..0a25f13
--- /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 7dda8c6..bdeafcb 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()
+  })
 })