virtual-console.js 760 B

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. const { EventEmitter } = require("events");
  3. module.exports = class VirtualConsole extends EventEmitter {
  4. constructor() {
  5. super();
  6. this.on("error", () => {
  7. // If "error" event has no listeners,
  8. // EventEmitter throws an exception
  9. });
  10. }
  11. sendTo(anyConsole, options) {
  12. if (options === undefined) {
  13. options = {};
  14. }
  15. for (const method of Object.keys(anyConsole)) {
  16. if (typeof anyConsole[method] === "function") {
  17. function onMethodCall(...args) {
  18. anyConsole[method](...args);
  19. }
  20. this.on(method, onMethodCall);
  21. }
  22. }
  23. if (!options.omitJSDOMErrors) {
  24. this.on("jsdomError", e => anyConsole.error(e.stack, e.detail));
  25. }
  26. return this;
  27. }
  28. };