diff --git a/packages/cli/src/bin/cli.test.ts b/packages/cli/src/bin/cli.test.ts index 556a0a1d..f26afcaa 100644 --- a/packages/cli/src/bin/cli.test.ts +++ b/packages/cli/src/bin/cli.test.ts @@ -9,6 +9,12 @@ import { version } from '../../package.json'; import runCLI from '../testutils/'; const SIMPLE_HTML_FILE = path.join(__dirname, '..', 'testutils', 'simple.html'); +const SIMPLE_CLEAN_HTML_FILE = path.join( + __dirname, + '..', + 'testutils', + 'simple-clean.html' +); const SIMPLE_HTML_SOURCE = fs.readFileSync(SIMPLE_HTML_FILE, 'utf8'); const PATH_TO_AXE_250 = path.resolve( __dirname, @@ -147,6 +153,18 @@ describe('cli', () => { ); } }); + + it('should exit zero if violations are found', async () => { + try { + await runCLI(`file://${SIMPLE_CLEAN_HTML_FILE}`, '--exit'); + } catch (error) { + assert.equal(error.exitCode, 0); + assert.include( + error.stdout, + 'Violation of "marquee" with 1 occurrences!' + ); + } + }); }); describe('--dir', () => { diff --git a/packages/cli/src/bin/index.ts b/packages/cli/src/bin/index.ts index 856d269a..541a2d9e 100644 --- a/packages/cli/src/bin/index.ts +++ b/packages/cli/src/bin/index.ts @@ -133,6 +133,24 @@ const cli = async ( } } + if (exit) { + let exitErr = false; + /* istanbul ignore if */ + if (Array.isArray(outcome)) { + for (const res of outcome) { + if (res.violations.length > 0) { + exitErr = true; + break; + } + } + } else { + exitErr = outcome.violations.length > 0; + } + if (exitErr) { + process.exit(1); + } + } + /* istanbul ignore if */ if (silentMode) { return; diff --git a/packages/cli/src/lib/events.ts b/packages/cli/src/lib/events.ts index 9be3e202..4b651cdc 100644 --- a/packages/cli/src/lib/events.ts +++ b/packages/cli/src/lib/events.ts @@ -69,10 +69,6 @@ export default ({ }; cliReporter(`\n${JSON.stringify(metadata, null, 2)}`); } - - if (exit) { - process.exit(1); - } } }; }; diff --git a/packages/cli/src/testutils/simple-clean.html b/packages/cli/src/testutils/simple-clean.html new file mode 100644 index 00000000..7e8ac2ab --- /dev/null +++ b/packages/cli/src/testutils/simple-clean.html @@ -0,0 +1,11 @@ + + + + + + Document + + +

This is a document.

+ +