Skip to content

Commit 7e9a3f4

Browse files
committed
test: create reporters tests
1 parent 3e912e7 commit 7e9a3f4

File tree

4 files changed

+90
-2
lines changed

4 files changed

+90
-2
lines changed

src/linter/reporters/console.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ export default issue => {
2121
? ` (${issue.location.position.start.line}:${issue.location.position.end.line})`
2222
: '';
2323

24-
console.log(
24+
process.stdout.write(
2525
styleText(
2626
levelToColorMap[issue.level],
2727
`${issue.message} at ${issue.location.path}${position}`
28-
)
28+
) + '\n'
2929
);
3030
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// @ts-check
2+
3+
/**
4+
* @type {import('../../types').LintIssue}
5+
*/
6+
export const infoIssue = {
7+
level: 'info',
8+
location: {
9+
path: 'doc/api/test.md',
10+
},
11+
message: 'This is a INFO issue',
12+
};
13+
14+
/**
15+
* @type {import('../../types').LintIssue}
16+
*/
17+
export const warnIssue = {
18+
level: 'warn',
19+
location: {
20+
path: 'doc/api/test.md',
21+
position: { start: { line: 1, column: 1 }, end: { line: 1, column: 2 } },
22+
},
23+
message: 'This is a WARN issue',
24+
};
25+
26+
/**
27+
* @type {import('../../types').LintIssue}
28+
*/
29+
export const errorIssue = {
30+
level: 'error',
31+
location: {
32+
path: 'doc/api/test.md',
33+
position: { start: { line: 1, column: 1 }, end: { line: 1, column: 2 } },
34+
},
35+
message: 'This is a ERROR issue',
36+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, it } from 'node:test';
2+
import console from '../../reporters/console.mjs';
3+
import assert from 'node:assert';
4+
import { errorIssue, infoIssue, warnIssue } from '../fixtures/issues.mjs';
5+
6+
describe('console', () => {
7+
it('should write to stdout with the correct colors based on the issue level', t => {
8+
t.mock.method(process.stdout, 'write');
9+
10+
console(infoIssue);
11+
console(warnIssue);
12+
console(errorIssue);
13+
14+
assert.strictEqual(process.stdout.write.mock.callCount(), 3);
15+
16+
const callsArgs = process.stdout.write.mock.calls.map(
17+
call => call.arguments[0]
18+
);
19+
20+
assert.deepStrictEqual(callsArgs, [
21+
'\x1B[90mThis is a INFO issue at doc/api/test.md\x1B[39m\n',
22+
'\x1B[33mThis is a WARN issue at doc/api/test.md (1:1)\x1B[39m\n',
23+
'\x1B[31mThis is a ERROR issue at doc/api/test.md (1:1)\x1B[39m\n',
24+
]);
25+
});
26+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, it } from 'node:test';
2+
import github from '../../reporters/github.mjs';
3+
import assert from 'node:assert';
4+
import { errorIssue, infoIssue, warnIssue } from '../fixtures/issues.mjs';
5+
6+
describe('github', () => {
7+
it('should write to stdout with the correct fn based on the issue level', t => {
8+
t.mock.method(process.stdout, 'write');
9+
10+
github(infoIssue);
11+
github(warnIssue);
12+
github(errorIssue);
13+
14+
assert.strictEqual(process.stdout.write.mock.callCount(), 3);
15+
16+
const callsArgs = process.stdout.write.mock.calls.map(
17+
call => call.arguments[0]
18+
);
19+
20+
assert.deepStrictEqual(callsArgs, [
21+
'::notice file=doc/api/test.md::This is a INFO issue\n',
22+
'::warning file=doc/api/test.md,line=1,endLine=1::This is a WARN issue\n',
23+
'::error file=doc/api/test.md,line=1,endLine=1::This is a ERROR issue\n',
24+
]);
25+
});
26+
});

0 commit comments

Comments
 (0)