Skip to content

Commit

Permalink
chore: add error logging to loadConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Nov 21, 2021
1 parent e7f9fa0 commit fddcef7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
9 changes: 8 additions & 1 deletion lib/loadConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ const searchPlaces = [
'lint-staged.config.cjs',
]

const dynamicImport = (path) => import(path).then((module) => module.default)
/** exported for tests */
export const dynamicImport = (path) =>
import(path)
.then((module) => module.default)
.catch((error) => {
console.error(error)
throw error
})

const jsonParse = (path, content) => JSON.parse(content)

Expand Down
17 changes: 0 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"fs-extra": "^10.0.0",
"husky": "^7.0.4",
"jest": "^27.3.1",
"jest-mock-console": "^1.2.3",
"jest-snapshot-serializer-ansi": "^1.0.0",
"prettier": "^2.4.1"
},
Expand Down
21 changes: 21 additions & 0 deletions test/loadConfig.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import makeConsoleMock from 'consolemock'

import { dynamicImport } from '../lib/loadConfig.js'

describe('dynamicImport', () => {
const globalConsoleTemp = console

beforeEach(() => {
console = makeConsoleMock()
})

afterAll(() => {
console = globalConsoleTemp
})

it('should log errors into console', () => {
expect(() => dynamicImport('not-found.js')).rejects.toMatchInlineSnapshot(
`[Error: Cannot find module 'not-found.js' from 'lib/loadConfig.js']`
)
})
})

0 comments on commit fddcef7

Please sign in to comment.