forked from babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
126 lines (106 loc) 路 3.77 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
116
117
118
119
120
121
122
123
124
125
126
import stripAnsi from "strip-ansi";
import _highlight, { shouldHighlight, getChalk } from "../lib/index.js";
const highlight = _highlight.default || _highlight;
const chalk = getChalk({});
describe("@babel/highlight", function () {
function stubColorSupport(supported) {
let originalChalkLevel;
let originalChalkSupportsColor;
let originalChalkEnabled;
beforeEach(function () {
originalChalkSupportsColor = chalk.supportsColor;
originalChalkLevel = chalk.level;
originalChalkEnabled = chalk.enabled;
chalk.supportsColor = supported ? { level: 1 } : false;
chalk.level = supported ? 1 : 0;
chalk.enabled = supported;
});
afterEach(function () {
chalk.supportsColor = originalChalkSupportsColor;
chalk.level = originalChalkLevel;
chalk.enabled = originalChalkEnabled;
});
}
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,
);
});
});
});
});
});