JestHooks.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. /**
  7. * Copyright (c) Meta Platforms, Inc. and affiliates.
  8. *
  9. * This source code is licensed under the MIT license found in the
  10. * LICENSE file in the root directory of this source tree.
  11. */
  12. class JestHooks {
  13. _listeners;
  14. _subscriber;
  15. _emitter;
  16. constructor() {
  17. this._listeners = {
  18. onFileChange: [],
  19. onTestRunComplete: [],
  20. shouldRunTestSuite: []
  21. };
  22. this._subscriber = {
  23. onFileChange: fn => {
  24. this._listeners.onFileChange.push(fn);
  25. },
  26. onTestRunComplete: fn => {
  27. this._listeners.onTestRunComplete.push(fn);
  28. },
  29. shouldRunTestSuite: fn => {
  30. this._listeners.shouldRunTestSuite.push(fn);
  31. }
  32. };
  33. this._emitter = {
  34. onFileChange: fs =>
  35. this._listeners.onFileChange.forEach(listener => listener(fs)),
  36. onTestRunComplete: results =>
  37. this._listeners.onTestRunComplete.forEach(listener =>
  38. listener(results)
  39. ),
  40. shouldRunTestSuite: async testSuiteInfo => {
  41. const result = await Promise.all(
  42. this._listeners.shouldRunTestSuite.map(listener =>
  43. listener(testSuiteInfo)
  44. )
  45. );
  46. return result.every(Boolean);
  47. }
  48. };
  49. }
  50. isUsed(hook) {
  51. return this._listeners[hook]?.length > 0;
  52. }
  53. getSubscriber() {
  54. return this._subscriber;
  55. }
  56. getEmitter() {
  57. return this._emitter;
  58. }
  59. }
  60. var _default = JestHooks;
  61. exports.default = _default;