Skip to content

Commit

Permalink
Code improvements 🙌
Browse files Browse the repository at this point in the history
  • Loading branch information
01taylop committed Jul 18, 2024
1 parent a2bf52c commit 5b1dc08
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
14 changes: 7 additions & 7 deletions src/linters/markdownlint/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import markdownlint from 'markdownlint'
import colourLog from '@Utils/colourLog'

import loadConfig from '../loadConfig'
import markdownLib from '..'
import markdownlintLib from '..'

jest.mock('markdownlint')
jest.mock('@Utils/colourLog')
Expand All @@ -26,7 +26,7 @@ describe('markdownlint', () => {
callback(null, {})
})

await markdownLib.lintFiles(testFiles)
await markdownlintLib.lintFiles(testFiles)

expect(loadConfig).toHaveBeenCalledTimes(1)
expect(colourLog.configDebug).toHaveBeenCalledWith('Using default markdownlint config:', mockedConfig)
Expand All @@ -37,7 +37,7 @@ describe('markdownlint', () => {
callback(null, {})
})

await markdownLib.lintFiles(testFiles)
await markdownlintLib.lintFiles(testFiles)

expect(markdownlint).toHaveBeenCalledOnceWith({
config: mockedConfig,
Expand All @@ -52,7 +52,7 @@ describe('markdownlint', () => {
callback(error, undefined)
})

await expect(markdownLib.lintFiles(testFiles)).rejects.toThrow(error)
await expect(markdownlintLib.lintFiles(testFiles)).rejects.toThrow(error)

expect(colourLog.error).toHaveBeenCalledOnceWith('An error occurred while running markdownlint', error)
})
Expand All @@ -62,7 +62,7 @@ describe('markdownlint', () => {
callback(null, undefined)
})

await expect(markdownLib.lintFiles(testFiles)).rejects.toThrow('No results')
await expect(markdownlintLib.lintFiles(testFiles)).rejects.toThrow('No results')

expect(colourLog.error).toHaveBeenCalledOnceWith('An error occurred while running markdownlint: no results')
})
Expand All @@ -74,7 +74,7 @@ describe('markdownlint', () => {
})
})

expect(await markdownLib.lintFiles(testFiles)).toStrictEqual({
expect(await markdownlintLib.lintFiles(testFiles)).toStrictEqual({
results: {},
summary: {
deprecatedRules: [],
Expand Down Expand Up @@ -149,7 +149,7 @@ describe('markdownlint', () => {
callback(null, markdownlintResult)
})

expect(await markdownLib.lintFiles(testFiles)).toStrictEqual({
expect(await markdownlintLib.lintFiles(testFiles)).toStrictEqual({
results: {
'CHANGELOG.md': [{
...commonResult,
Expand Down
4 changes: 2 additions & 2 deletions src/linters/markdownlint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ const lintFiles = (files: Array<string>): Promise<LintReport> => new Promise((re
})
})

const markdownLib = {
const markdownlintLib = {
lintFiles,
}

export default markdownLib
export default markdownlintLib
6 changes: 3 additions & 3 deletions src/utils/__tests__/colourLog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('colourLog', () => {
expect(mockedConsoleLog).toHaveBeenCalledOnceWith('\nAn error occurred.')
})

it('logs additional debug information if there is an error', () => {
it('logs additional debug information if there is an error and global.debug is false', () => {
colourLog.error('An error occurred', error)

expect(chalk.red).toHaveBeenCalledOnceWith('\nAn error occurred. Run with --debug for more information.')
Expand All @@ -119,9 +119,9 @@ describe('colourLog', () => {

colourLog.error('An error occurred', error)

expect(chalk.red).toHaveBeenCalledOnceWith('\nAn error occurred. Run with --debug for more information.')
expect(chalk.red).toHaveBeenCalledOnceWith('\nAn error occurred.')
expect(mockedConsoleLog).toHaveBeenCalledTimes(3)
expect(mockedConsoleLog).toHaveBeenNthCalledWith(1, '\nAn error occurred. Run with --debug for more information.')
expect(mockedConsoleLog).toHaveBeenNthCalledWith(1, '\nAn error occurred.')
expect(mockedConsoleLog).toHaveBeenNthCalledWith(2)
expect(mockedConsoleLog).toHaveBeenNthCalledWith(3, error)
})
Expand Down
17 changes: 9 additions & 8 deletions src/utils/colourLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ const colourLog = {
},

error: (text: string, error?: Error | unknown) => {
if (error) {
console.log(chalk.red(`\n${text}. Run with --debug for more information.`))
if (global.debug === true) {
console.log()
console.log(error)
}
} else {
console.log(chalk.red(`\n${text}.`))
let errorMessage = `\n${text}.`
if (error && global.debug === false) {
errorMessage += ' Run with --debug for more information.'
}

console.log(chalk.red(errorMessage))
if (error && global.debug === true) {
console.log()
console.log(error)
}
},

Expand Down

0 comments on commit 5b1dc08

Please sign in to comment.