Debian Codegraph @

packages/babel-highlight/test/index.js

raw symbols SCIP
1 import chalk from "chalk";
2 import stripAnsi from "strip-ansi";
3
4 import _highlight, { shouldHighlight, getChalk } from "../lib/index.js";
5 const highlight = _highlight.default || _highlight;
6
7 describe("@babel/highlight", function () {
8 function stubColorSupport(supported) {
9 let originalSupportsColor;
10 beforeEach(function () {
11 originalSupportsColor = chalk.supportsColor;
12 chalk.supportsColor = supported;
13 });
14
15 afterEach(function () {
16 chalk.supportsColor = originalSupportsColor;
17 });
18 }
19
20 describe("highlight", function () {
21 describe("when colors are supported", function () {
22 stubColorSupport(true);
23
24 it("highlights code", function () {
25 const code = "console.log('hi')";
26 const result = highlight(code);
27 const stripped = stripAnsi(result);
28 expect(result.length).toBeGreaterThan(stripped.length);
29 expect(stripped).toBe(code);
30 });
31 });
32
33 describe("when colors are not supported", function () {
34 stubColorSupport(false);
35
36 it("does not attempt to highlight code", function () {
37 const code = "console.log('hi')";
38 const result = highlight(code);
39 const stripped = stripAnsi(result);
40 expect(result.length).toBe(stripped.length);
41 expect(result).toBe(code);
42 });
43
44 describe("and the forceColor option is passed", function () {
45 it("highlights the code anyway", function () {
46 const code = "console.log('hi')";
47 const result = highlight(code, { forceColor: true });
48 const stripped = stripAnsi(result);
49 expect(result.length).toBeGreaterThan(stripped.length);
50 expect(stripped).toBe(code);
51 });
52 });
53 });
54 });
55
56 describe("shouldHighlight", function () {
57 describe("when colors are supported", function () {
58 stubColorSupport(true);
59
60 it("returns true", function () {
61 expect(shouldHighlight({})).toBeTruthy();
62 });
63 });
64
65 describe("when colors are not supported", function () {
66 stubColorSupport(false);
67
68 it("returns false", function () {
69 expect(shouldHighlight({})).toBeFalsy();
70 });
71
72 describe("and the forceColor option is passed", function () {
73 it("returns true", function () {
74 expect(shouldHighlight({ forceColor: true })).toBeTruthy();
75 });
76 });
77 });
78 });
79
80 describe("getChalk", function () {
81 describe("when colors are supported", function () {
82 stubColorSupport(true);
83
84 describe("when forceColor is not passed", function () {
85 it("returns a Chalk instance", function () {
86 expect(getChalk({}).constructor).toBe(chalk.constructor);
87 });
88 });
89
90 describe("when forceColor is passed", function () {
91 it("returns a Chalk instance", function () {
92 expect(getChalk({ forceColor: true }).constructor).toBe(
93 chalk.constructor,
94 );
95 });
96 });
97 });
98
99 describe("when colors are not supported", function () {
100 stubColorSupport(false);
101
102 describe("when forceColor is not passed", function () {
103 it("returns a Chalk instance", function () {
104 expect(getChalk({}).constructor).toBe(chalk.constructor);
105 });
106 });
107
108 describe("when forceColor is passed", function () {
109 it("returns a Chalk instance", function () {
110 expect(getChalk({ forceColor: true }).constructor).toBe(
111 chalk.constructor,
112 );
113 });
114 });
115 });
116 });
117 });