-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
index.js
115 lines (97 loc) 路 3.39 KB
/
index.js
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 chalk from "chalk";
import stripAnsi from "strip-ansi";
import highlight, { shouldHighlight, getChalk } from "../lib/index.js";
describe("@babel/highlight", function () {
function stubColorSupport(supported) {
let originalSupportsColor;
beforeEach(function () {
originalSupportsColor = chalk.supportsColor;
chalk.supportsColor = supported;
});
afterEach(function () {
chalk.supportsColor = originalSupportsColor;
});
}
describe("highlight", function () {
describe("when colors are supported", function () {
stubColorSupport(true);
it("highlights code", function () {
const code = "console.log('hi')";
const result = highlight(code);
const stripped = stripAnsi(result);
expect(result.length).toBeGreaterThan(stripped.length);
expect(stripped).toBe(code);
});
});
describe("when colors are not supported", function () {
stubColorSupport(false);
it("does not attempt to highlight code", function () {
const code = "console.log('hi')";
const result = highlight(code);
const stripped = stripAnsi(result);
expect(result.length).toBe(stripped.length);
expect(result).toBe(code);
});
describe("and the forceColor option is passed", function () {
it("highlights the code anyway", function () {
const code = "console.log('hi')";
const result = highlight(code, { forceColor: true });
const stripped = stripAnsi(result);
expect(result.length).toBeGreaterThan(stripped.length);
expect(stripped).toBe(code);
});
});
});
});
describe("shouldHighlight", function () {
describe("when colors are supported", function () {
stubColorSupport(true);
it("returns true", function () {
expect(shouldHighlight({})).toBeTruthy();
});
});
describe("when colors are not supported", function () {
stubColorSupport(false);
it("returns false", function () {
expect(shouldHighlight({})).toBeFalsy();
});
describe("and the forceColor option is passed", function () {
it("returns true", function () {
expect(shouldHighlight({ forceColor: true })).toBeTruthy();
});
});
});
});
describe("getChalk", function () {
describe("when colors are supported", function () {
stubColorSupport(true);
describe("when forceColor is not passed", function () {
it("returns a Chalk instance", function () {
expect(getChalk({}).constructor).toBe(chalk.constructor);
});
});
describe("when forceColor is passed", function () {
it("returns a Chalk instance", function () {
expect(getChalk({ forceColor: true }).constructor).toBe(
chalk.constructor,
);
});
});
});
describe("when colors are not supported", function () {
stubColorSupport(false);
describe("when forceColor is not passed", function () {
it("returns a Chalk instance", function () {
expect(getChalk({}).constructor).toBe(chalk.constructor);
});
});
describe("when forceColor is passed", function () {
it("returns a Chalk instance", function () {
expect(getChalk({ forceColor: true }).constructor).toBe(
chalk.constructor,
);
});
});
});
});
});