index.js 3.3 KB

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