Skip to content

Commit

Permalink
Improved testing process.exit calls 🚪
Browse files Browse the repository at this point in the history
  • Loading branch information
01taylop committed Jul 18, 2024
1 parent 5b1dc08 commit e4535d5
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 31 deletions.
4 changes: 4 additions & 0 deletions jest-config/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ jest.mock('log-symbols', () => ({
error: 'X',
}))

jest.spyOn(process, 'exit').mockImplementation(code => {
throw new Error(`process.exit(${code})`)
})

beforeEach(() => {
global.debug = false
})
Expand Down
13 changes: 8 additions & 5 deletions src/__tests__/sourceFiles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('sourceFiles', () => {

jest.spyOn(colourLog, 'configDebug').mockImplementation(() => {})
jest.spyOn(colourLog, 'error').mockImplementation(() => {})
jest.spyOn(process, 'exit').mockImplementation(() => null as never)

const commonArgs = {
filePattern: '*.ts',
Expand Down Expand Up @@ -54,14 +53,18 @@ describe('sourceFiles', () => {
})

it('catches any errors and exists the process', async () => {
expect.assertions(2)

const error = new Error('Test error')

jest.mocked(glob).mockRejectedValue(error)

await sourceFiles(commonArgs)

expect(colourLog.error).toHaveBeenCalledWith('An error occurred while trying to source files matching *.ts', error)
expect(process.exit).toHaveBeenCalledWith(1)
try {
await sourceFiles(commonArgs)
} catch {
expect(colourLog.error).toHaveBeenCalledWith('An error occurred while trying to source files matching *.ts', error)
expect(process.exit).toHaveBeenCalledWith(1)
}
})

})
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,8 @@ program
}
})

process.on('exit', () => {
console.log()
})

program.parse(process.argv)
7 changes: 3 additions & 4 deletions src/linters/eslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ const lintFiles = async (files: Array<string>): Promise<LintReport> => {
fix: false,
})

const results: Array<ESLint.LintResult> = await eslint.lintFiles(files)
const lintResults: Array<ESLint.LintResult> = await eslint.lintFiles(files)

const reportResults: ReportResults = {}

const reportSummary: ReportSummary = {
deprecatedRules: [],
errorCount: 0,
fileCount: results.length,
fileCount: lintResults.length,
fixableErrorCount: 0,
fixableWarningCount: 0,
linter: Linter.ESLint,
warningCount: 0,
}

results.forEach(({ errorCount, filePath, fixableErrorCount, fixableWarningCount, messages, usedDeprecatedRules, warningCount }) => {
lintResults.forEach(({ errorCount, filePath, fixableErrorCount, fixableWarningCount, messages, usedDeprecatedRules, warningCount }) => {
const file = filePath.replace(`${process.cwd()}/`, '')

reportSummary.deprecatedRules = [...new Set([...reportSummary.deprecatedRules, ...usedDeprecatedRules.map(({ ruleId }) => ruleId)])]
Expand Down Expand Up @@ -57,7 +57,6 @@ const lintFiles = async (files: Array<string>): Promise<LintReport> => {
}
} catch (error: any) {
colourLog.error('An error occurred while running eslint', error)
console.log()
process.exit(1)
}
}
Expand Down
30 changes: 22 additions & 8 deletions src/linters/markdownlint/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,40 @@ describe('markdownlint', () => {
}, expect.any(Function))
})

it('rejects when markdownlint returns an error', async () => {
it('exists the process when markdownlint returns an error', async () => {
expect.assertions(2)

const error = new Error('Test error')

jest.mocked(markdownlint).mockImplementationOnce((_options, callback) => {
callback(error, undefined)
})

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

expect(colourLog.error).toHaveBeenCalledOnceWith('An error occurred while running markdownlint', error)
try {
await markdownlintLib.lintFiles(testFiles)
} catch {
expect(colourLog.error).toHaveBeenCalledOnceWith('An error occurred while running markdownlint', error)
expect(process.exit).toHaveBeenCalledWith(1)
}
})

it('rejects when markdownlint returns no results', async () => {
it('resolves with results and a summary when markdownlint successfully lints (no files)', async () => {
jest.mocked(markdownlint).mockImplementationOnce((_options, callback) => {
callback(null, undefined)
})

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

expect(colourLog.error).toHaveBeenCalledOnceWith('An error occurred while running markdownlint: no results')
expect(await markdownlintLib.lintFiles([])).toStrictEqual({
results: {},
summary: {
deprecatedRules: [],
errorCount: 0,
fileCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
linter: 'MarkdownLint',
warningCount: 0,
},
})
})

it('resolves with results and a summary when markdownlint successfully lints (no errors)', async () => {
Expand Down
13 changes: 8 additions & 5 deletions src/linters/markdownlint/__tests__/loadConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ describe('loadConfig', () => {

jest.spyOn(colourLog, 'error').mockImplementation(() => {})
jest.spyOn(markdownlint, 'readConfigSync').mockImplementation(() => ({ default: true }))
jest.spyOn(process, 'exit').mockImplementation(() => null as never)

it('returns the custom config if it exists', () => {
jest.mocked(fs.existsSync).mockReturnValueOnce(true)
Expand Down Expand Up @@ -46,16 +45,20 @@ describe('loadConfig', () => {
})

it('catches and logs any errors', () => {
expect.assertions(2)

const error = new Error('Test error')

jest.mocked(fs.existsSync).mockImplementationOnce(() => {
throw error
})

loadConfig()

expect(colourLog.error).toHaveBeenCalledWith('An error occurred while loading the markdownlint config', error)
expect(process.exit).toHaveBeenCalledWith(1)
try {
loadConfig()
} catch {
expect(colourLog.error).toHaveBeenCalledWith('An error occurred while loading the markdownlint config', error)
expect(process.exit).toHaveBeenCalledWith(1)
}
})

})
13 changes: 4 additions & 9 deletions src/linters/markdownlint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,25 @@ const lintFiles = (files: Array<string>): Promise<LintReport> => new Promise((re
markdownlint({
config,
files,
}, (error: any, results: LintResults | undefined) => {
}, (error: any, lintResults: LintResults | undefined = {}) => {
if (error) {
colourLog.error('An error occurred while running markdownlint', error)
return reject(error)
}

if (!results) {
colourLog.error('An error occurred while running markdownlint: no results')
return reject(new Error('No results'))
process.exit(1)
}

const reportResults: ReportResults = {}

const reportSummary: ReportSummary = {
deprecatedRules: [],
errorCount: 0,
fileCount: Object.keys(results).length,
fileCount: Object.keys(lintResults).length,
fixableErrorCount: 0,
fixableWarningCount: 0,
linter: Linter.Markdownlint,
warningCount: 0,
}

Object.entries(results).forEach(([file, errors]) => {
Object.entries(lintResults).forEach(([file, errors]) => {
if (!errors.length) {
return
}
Expand Down

0 comments on commit e4535d5

Please sign in to comment.