diff --git a/jest-config/setup.ts b/jest-config/setup.ts index a2092e1..d4ead73 100644 --- a/jest-config/setup.ts +++ b/jest-config/setup.ts @@ -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 }) diff --git a/src/__tests__/sourceFiles.spec.ts b/src/__tests__/sourceFiles.spec.ts index 51d4ee1..f89aa84 100644 --- a/src/__tests__/sourceFiles.spec.ts +++ b/src/__tests__/sourceFiles.spec.ts @@ -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', @@ -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) + } }) }) diff --git a/src/index.ts b/src/index.ts index 7d8217f..2ca12af 100755 --- a/src/index.ts +++ b/src/index.ts @@ -111,4 +111,8 @@ program } }) +process.on('exit', () => { + console.log() +}) + program.parse(process.argv) diff --git a/src/linters/eslint.ts b/src/linters/eslint.ts index 5a9b15f..e73ce74 100644 --- a/src/linters/eslint.ts +++ b/src/linters/eslint.ts @@ -13,21 +13,21 @@ const lintFiles = async (files: Array): Promise => { fix: false, }) - const results: Array = await eslint.lintFiles(files) + const lintResults: Array = 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)])] @@ -57,7 +57,6 @@ const lintFiles = async (files: Array): Promise => { } } catch (error: any) { colourLog.error('An error occurred while running eslint', error) - console.log() process.exit(1) } } diff --git a/src/linters/markdownlint/__tests__/index.spec.ts b/src/linters/markdownlint/__tests__/index.spec.ts index 4b4c7e3..d91dab4 100644 --- a/src/linters/markdownlint/__tests__/index.spec.ts +++ b/src/linters/markdownlint/__tests__/index.spec.ts @@ -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 () => { diff --git a/src/linters/markdownlint/__tests__/loadConfig.spec.ts b/src/linters/markdownlint/__tests__/loadConfig.spec.ts index 49ff9f6..a7d2ac1 100644 --- a/src/linters/markdownlint/__tests__/loadConfig.spec.ts +++ b/src/linters/markdownlint/__tests__/loadConfig.spec.ts @@ -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) @@ -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) + } }) }) diff --git a/src/linters/markdownlint/index.ts b/src/linters/markdownlint/index.ts index e227f44..31aa8d9 100644 --- a/src/linters/markdownlint/index.ts +++ b/src/linters/markdownlint/index.ts @@ -16,15 +16,10 @@ const lintFiles = (files: Array): Promise => 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 = {} @@ -32,14 +27,14 @@ const lintFiles = (files: Array): Promise => new Promise((re 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 }