-
Notifications
You must be signed in to change notification settings - Fork 667
/
tests.ts
115 lines (79 loc) · 3.63 KB
/
tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import anyTest, { TestInterface, ExecutionContext } from 'ava';
import chalk from 'chalk';
import * as logSymbols from 'log-symbols';
import * as proxyquire from 'proxyquire';
import * as sinon from 'sinon';
import * as table from 'text-table';
import * as problems from './fixtures/list-of-problems';
type Logging = {
log: () => void;
};
type SummaryContext = {
logging: Logging;
loggingLogSpy: sinon.SinonSpy<any, void>;
};
const test = anyTest as TestInterface<SummaryContext>;
const initContext = (t: ExecutionContext<SummaryContext>) => {
t.context.logging = { log() { } };
t.context.loggingLogSpy = sinon.spy(t.context.logging, 'log');
};
const loadScript = (context: SummaryContext) => {
const script = proxyquire('../src/formatter', { 'hint/dist/src/lib/utils/logging': context.logging });
return script.default;
};
test.beforeEach(initContext);
test.afterEach.always((t) => {
t.context.loggingLogSpy.restore();
});
test(`Summary formatter doesn't print anything if no values`, (t) => {
const SummaryFormatter = loadScript(t.context);
const formatter = new SummaryFormatter();
formatter.format(problems.noproblems);
t.is(t.context.loggingLogSpy.callCount, 0);
});
test(`Summary formatter prints in yellow if only warnings found`, (t) => {
const log = t.context.loggingLogSpy;
const tableData = [];
const SummaryFormatter = loadScript(t.context);
const formatter = new SummaryFormatter();
formatter.format(problems.summaryWarnings);
tableData.push([chalk.cyan('random-hint'), chalk.yellow(`2 warnings`)]);
const tableString = table(tableData);
t.is(log.args[0][0], tableString);
t.is(log.args[1][0], chalk.yellow.bold(`${logSymbols.error.trim()} Found a total of 0 errors and 2 warnings`));
});
test(`Summary formatter prints a table and a summary for all resources combined`, (t) => {
const log = t.context.loggingLogSpy;
const tableData = [];
const SummaryFormatter = loadScript(t.context);
const formatter = new SummaryFormatter();
formatter.format(problems.summaryProblems);
tableData.push([chalk.cyan('random-hint2'), chalk.red(`1 error`)]);
tableData.push([chalk.cyan('random-hint'), chalk.yellow(`4 warnings`)]);
const tableString = table(tableData);
t.is(log.args[0][0], tableString);
t.is(log.args[1][0], chalk.red.bold(`${logSymbols.error.trim()} Found a total of 1 error and 4 warnings`));
});
test(`Summary formatter sorts by name if same number of errors`, (t) => {
const log = t.context.loggingLogSpy;
const tableData = [];
const SummaryFormatter = loadScript(t.context);
const formatter = new SummaryFormatter();
formatter.format(problems.summarySameNumberOfErrors);
tableData.push([chalk.cyan('random-hint'), chalk.red(`1 error`)]);
tableData.push([chalk.cyan('random-hint2'), chalk.red(`1 error`)]);
const tableString = table(tableData);
t.is(log.args[0][0], tableString);
t.is(log.args[1][0], chalk.red.bold(`${logSymbols.error.trim()} Found a total of 2 errors and 0 warnings`));
});
test(`Summary formatter prints errors and warnings for a hint that reports both`, (t) => {
const log = t.context.loggingLogSpy;
const tableData = [];
const SummaryFormatter = loadScript(t.context);
const formatter = new SummaryFormatter();
formatter.format(problems.summaryErrorWarnings);
tableData.push([chalk.cyan('random-hint'), chalk.red(`1 error`), chalk.yellow(`1 warning`)]);
const tableString = table(tableData);
t.is(log.args[0][0], tableString);
t.is(log.args[1][0], chalk.red.bold(`${logSymbols.error.trim()} Found a total of 1 error and 1 warning`));
});