formatTestNameByPattern.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = formatTestNameByPattern;
  6. function _chalk() {
  7. const data = _interopRequireDefault(require('chalk'));
  8. _chalk = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. var _colorize = _interopRequireDefault(require('./colorize'));
  14. function _interopRequireDefault(obj) {
  15. return obj && obj.__esModule ? obj : {default: obj};
  16. }
  17. /**
  18. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  19. *
  20. * This source code is licensed under the MIT license found in the
  21. * LICENSE file in the root directory of this source tree.
  22. */
  23. const DOTS = '...';
  24. const ENTER = '⏎';
  25. function formatTestNameByPattern(testName, pattern, width) {
  26. const inlineTestName = testName.replace(/(\r\n|\n|\r)/gm, ENTER);
  27. let regexp;
  28. try {
  29. regexp = new RegExp(pattern, 'i');
  30. } catch {
  31. return _chalk().default.dim(inlineTestName);
  32. }
  33. const match = inlineTestName.match(regexp);
  34. if (!match) {
  35. return _chalk().default.dim(inlineTestName);
  36. }
  37. const startPatternIndex = Math.max(match.index || 0, 0);
  38. const endPatternIndex = startPatternIndex + match[0].length;
  39. if (inlineTestName.length <= width) {
  40. return (0, _colorize.default)(
  41. inlineTestName,
  42. startPatternIndex,
  43. endPatternIndex
  44. );
  45. }
  46. const slicedTestName = inlineTestName.slice(0, width - DOTS.length);
  47. if (startPatternIndex < slicedTestName.length) {
  48. if (endPatternIndex > slicedTestName.length) {
  49. return (0, _colorize.default)(
  50. slicedTestName + DOTS,
  51. startPatternIndex,
  52. slicedTestName.length + DOTS.length
  53. );
  54. } else {
  55. return (0, _colorize.default)(
  56. slicedTestName + DOTS,
  57. Math.min(startPatternIndex, slicedTestName.length),
  58. endPatternIndex
  59. );
  60. }
  61. }
  62. return `${_chalk().default.dim(slicedTestName)}${_chalk().default.reset(
  63. DOTS
  64. )}`;
  65. }