github.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports.GitHubReporter = void 0;
  6. var _utilsBundle = require("playwright-core/lib/utilsBundle");
  7. var _path = _interopRequireDefault(require("path"));
  8. var _base = require("./base");
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. /**
  11. * Copyright (c) Microsoft Corporation.
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License");
  14. * you may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */
  25. class GitHubLogger {
  26. _log(message, type = 'notice', options = {}) {
  27. message = message.replace(/\n/g, '%0A');
  28. const configs = Object.entries(options).map(([key, option]) => `${key}=${option}`).join(',');
  29. console.log((0, _base.stripAnsiEscapes)(`::${type} ${configs}::${message}`));
  30. }
  31. debug(message, options) {
  32. this._log(message, 'debug', options);
  33. }
  34. error(message, options) {
  35. this._log(message, 'error', options);
  36. }
  37. notice(message, options) {
  38. this._log(message, 'notice', options);
  39. }
  40. warning(message, options) {
  41. this._log(message, 'warning', options);
  42. }
  43. }
  44. class GitHubReporter extends _base.BaseReporter {
  45. constructor(...args) {
  46. super(...args);
  47. this.githubLogger = new GitHubLogger();
  48. }
  49. printsToStdio() {
  50. return false;
  51. }
  52. async onEnd(result) {
  53. await super.onEnd(result);
  54. this._printAnnotations();
  55. }
  56. onError(error) {
  57. const errorMessage = (0, _base.formatError)(error, false).message;
  58. this.githubLogger.error(errorMessage);
  59. }
  60. _printAnnotations() {
  61. const summary = this.generateSummary();
  62. const summaryMessage = this.generateSummaryMessage(summary);
  63. if (summary.failuresToPrint.length) this._printFailureAnnotations(summary.failuresToPrint);
  64. this._printSlowTestAnnotations();
  65. this._printSummaryAnnotation(summaryMessage);
  66. }
  67. _printSlowTestAnnotations() {
  68. this.getSlowTests().forEach(([file, duration]) => {
  69. const filePath = workspaceRelativePath(_path.default.join(process.cwd(), file));
  70. this.githubLogger.warning(`${filePath} took ${(0, _utilsBundle.ms)(duration)}`, {
  71. title: 'Slow Test',
  72. file: filePath
  73. });
  74. });
  75. }
  76. _printSummaryAnnotation(summary) {
  77. this.githubLogger.notice(summary, {
  78. title: '🎭 Playwright Run Summary'
  79. });
  80. }
  81. _printFailureAnnotations(failures) {
  82. failures.forEach((test, index) => {
  83. const {
  84. annotations
  85. } = (0, _base.formatFailure)(this.config, test, {
  86. index: index + 1,
  87. includeStdio: true,
  88. includeAttachments: false
  89. });
  90. annotations.forEach(({
  91. location,
  92. title,
  93. message
  94. }) => {
  95. const options = {
  96. file: workspaceRelativePath((location === null || location === void 0 ? void 0 : location.file) || test.location.file),
  97. title
  98. };
  99. if (location) {
  100. options.line = location.line;
  101. options.col = location.column;
  102. }
  103. this.githubLogger.error(message, options);
  104. });
  105. });
  106. }
  107. }
  108. exports.GitHubReporter = GitHubReporter;
  109. function workspaceRelativePath(filePath) {
  110. var _process$env$GITHUB_W;
  111. return _path.default.relative((_process$env$GITHUB_W = process.env['GITHUB_WORKSPACE']) !== null && _process$env$GITHUB_W !== void 0 ? _process$env$GITHUB_W : '', filePath);
  112. }
  113. var _default = exports.default = GitHubReporter;