Skip to content

Commit bfa2328

Browse files
authored
fix(cli): respect --exit regardless of other flags (#750)
1 parent 8a9a92d commit bfa2328

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

packages/cli/src/bin/cli.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import { version } from '../../package.json';
99
import runCLI from '../testutils/';
1010

1111
const SIMPLE_HTML_FILE = path.join(__dirname, '..', 'testutils', 'simple.html');
12+
const SIMPLE_CLEAN_HTML_FILE = path.join(
13+
__dirname,
14+
'..',
15+
'testutils',
16+
'simple-clean.html'
17+
);
1218
const SIMPLE_HTML_SOURCE = fs.readFileSync(SIMPLE_HTML_FILE, 'utf8');
1319
const PATH_TO_AXE_250 = path.resolve(
1420
__dirname,
@@ -147,6 +153,18 @@ describe('cli', () => {
147153
);
148154
}
149155
});
156+
157+
it('should exit zero if violations are found', async () => {
158+
try {
159+
await runCLI(`file://${SIMPLE_CLEAN_HTML_FILE}`, '--exit');
160+
} catch (error) {
161+
assert.equal(error.exitCode, 0);
162+
assert.include(
163+
error.stdout,
164+
'Violation of "marquee" with 1 occurrences!'
165+
);
166+
}
167+
});
150168
});
151169

152170
describe('--dir', () => {

packages/cli/src/bin/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,24 @@ const cli = async (
133133
}
134134
}
135135

136+
if (exit) {
137+
let exitErr = false;
138+
/* istanbul ignore if */
139+
if (Array.isArray(outcome)) {
140+
for (const res of outcome) {
141+
if (res.violations.length > 0) {
142+
exitErr = true;
143+
break;
144+
}
145+
}
146+
} else {
147+
exitErr = outcome.violations.length > 0;
148+
}
149+
if (exitErr) {
150+
process.exit(1);
151+
}
152+
}
153+
136154
/* istanbul ignore if */
137155
if (silentMode) {
138156
return;

packages/cli/src/lib/events.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ export default ({
6969
};
7070
cliReporter(`\n${JSON.stringify(metadata, null, 2)}`);
7171
}
72-
73-
if (exit) {
74-
process.exit(1);
75-
}
7672
}
7773
};
7874
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<h1>This is a document.</h1>
10+
</body>
11+
</html>

0 commit comments

Comments
 (0)