stepLogsModifier.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const { event } = require("codeceptjs");
  2. const REPLACE_ARGS_RULE = "replaceArgs";
  3. const HIDE_FUNCTION_RULE = "hideFunction";
  4. const RULES = {
  5. REPLACE_ARGS_RULE,
  6. HIDE_FUNCTION_RULE,
  7. };
  8. /**
  9. * @typedef stepNameMatcher
  10. *
  11. */
  12. /**
  13. * @typedef {object} modifyStepConfig
  14. * @property {boolean|string|string[]|RegExp|RegExp[]} [stepNameMatcher=true] - affected steps matcher
  15. * @property {Function|'replaceArgs'|'hideFunction'} [rule=REPLACE_RULE] - replacing rule
  16. * @property {any[]} [params]
  17. */
  18. /**
  19. * @typedef {Object} stepLogsModifierConfig
  20. * @property {modifyStepConfig[]} [modifyStepLogs] - List of step modifiers
  21. */
  22. /**
  23. * @type {stepLogsModifierConfig}
  24. */
  25. const defaultConfig = {
  26. modifyStepLogs: [
  27. {
  28. stepNameMatcher: true,
  29. rule: REPLACE_ARGS_RULE,
  30. params: ["[args]"],
  31. },
  32. ],
  33. };
  34. /**
  35. * This plugin can modify step logs.
  36. * The main goal is making step logs more compact.
  37. * @param {stepLogsModifierConfig} config
  38. */
  39. module.exports = (config) => {
  40. const options = Object.assign({}, defaultConfig, config);
  41. const RULES = {
  42. [REPLACE_ARGS_RULE](step, replacer) {
  43. step.toString = replaceArgsStepToString.bind(step, replacer);
  44. step.toCode = replaceArgsStepToCode.bind(step, replacer);
  45. },
  46. [HIDE_FUNCTION_RULE](step) {
  47. const functionName = step.args?.[0]?.name || "<anonymous>";
  48. step.toString = replaceArgsStepToString.bind(step, functionName);
  49. step.toCode = replaceArgsStepToCode.bind(step, functionName);
  50. },
  51. };
  52. function replaceArgsStepToString(replacer) {
  53. return `${this.prefix}${this.actor} ${this.humanize()} ${replacer}${this.suffix}`;
  54. }
  55. function replaceArgsStepToCode(replacer) {
  56. return `${this.prefix}${this.actor}.${this.name}(${replacer})${this.suffix}`;
  57. }
  58. function testStep(matcher, stepName) {
  59. if (typeof matcher === "boolean") return matcher;
  60. if (typeof matcher === "string") return matcher === stepName;
  61. if (matcher instanceof RegExp) return matcher.test(stepName);
  62. if (Array.isArray(matcher)) return matcher.some((f) => testStep(f, stepName));
  63. return false;
  64. }
  65. function modifyStep(step) {
  66. if (step.metaStep) {
  67. modifyStep(step.metaStep);
  68. }
  69. for (const modifierParams of options.modifyStepLogs) {
  70. if (testStep(modifierParams.stepNameMatcher, step.name)) {
  71. if (typeof modifierParams.rule === "function") {
  72. modifierParams.rule(step);
  73. } else {
  74. if (RULES[modifierParams.rule]) {
  75. RULES[modifierParams.rule](step, ...(modifierParams.params || []));
  76. } else {
  77. console.error("There is no step modifier rule called `", modifierParams.rule, "`");
  78. }
  79. }
  80. }
  81. }
  82. }
  83. event.dispatcher.on(event.step.before, modifyStep);
  84. };
  85. module.exports.defaultConfig = defaultConfig;
  86. module.exports.RULES = RULES;