with-all-lifecycle-hooks.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { INestApplication, INestMicroservice } from '@nestjs/common';
  2. import {
  3. OnVendureBootstrap,
  4. OnVendureClose,
  5. OnVendureWorkerBootstrap,
  6. OnVendureWorkerClose,
  7. } from '@vendure/core';
  8. export class TestPluginWithAllLifecycleHooks
  9. implements OnVendureBootstrap, OnVendureWorkerBootstrap, OnVendureClose, OnVendureWorkerClose {
  10. private static onConstructorFn: any;
  11. private static onBeforeBootstrapFn: any;
  12. private static onBeforeWorkerBootstrapFn: any;
  13. private static onBootstrapFn: any;
  14. private static onWorkerBootstrapFn: any;
  15. private static onCloseFn: any;
  16. private static onWorkerCloseFn: any;
  17. static init(
  18. constructorFn: any,
  19. beforeBootstrapFn: any,
  20. beforeWorkerBootstrapFn: any,
  21. bootstrapFn: any,
  22. workerBootstrapFn: any,
  23. closeFn: any,
  24. workerCloseFn: any,
  25. ) {
  26. this.onConstructorFn = constructorFn;
  27. this.onBeforeBootstrapFn = beforeBootstrapFn;
  28. this.onBeforeWorkerBootstrapFn = beforeWorkerBootstrapFn;
  29. this.onBootstrapFn = bootstrapFn;
  30. this.onWorkerBootstrapFn = workerBootstrapFn;
  31. this.onCloseFn = closeFn;
  32. this.onWorkerCloseFn = workerCloseFn;
  33. return this;
  34. }
  35. constructor() {
  36. TestPluginWithAllLifecycleHooks.onConstructorFn();
  37. }
  38. static beforeVendureBootstrap(app: INestApplication): void | Promise<void> {
  39. TestPluginWithAllLifecycleHooks.onBeforeBootstrapFn(app);
  40. }
  41. static beforeVendureWorkerBootstrap(app: INestMicroservice): void | Promise<void> {
  42. TestPluginWithAllLifecycleHooks.onBeforeWorkerBootstrapFn(app);
  43. }
  44. onVendureBootstrap(): void | Promise<void> {
  45. TestPluginWithAllLifecycleHooks.onBootstrapFn();
  46. }
  47. onVendureWorkerBootstrap(): void | Promise<void> {
  48. TestPluginWithAllLifecycleHooks.onWorkerBootstrapFn();
  49. }
  50. onVendureClose(): void | Promise<void> {
  51. TestPluginWithAllLifecycleHooks.onCloseFn();
  52. this.resetSpies();
  53. }
  54. onVendureWorkerClose(): void | Promise<void> {
  55. TestPluginWithAllLifecycleHooks.onWorkerCloseFn();
  56. this.resetSpies();
  57. }
  58. /**
  59. * This is required because on the first run, the Vendure server will be bootstrapped twice -
  60. * once to populate the database and the second time for the actual tests. Thus the call counts
  61. * for the plugin lifecycles will be doubled. This method resets them after the initial
  62. * (population) run.
  63. */
  64. private resetSpies() {
  65. TestPluginWithAllLifecycleHooks.onConstructorFn.mockClear();
  66. TestPluginWithAllLifecycleHooks.onBeforeBootstrapFn.mockClear();
  67. TestPluginWithAllLifecycleHooks.onBeforeWorkerBootstrapFn.mockClear();
  68. TestPluginWithAllLifecycleHooks.onBootstrapFn.mockClear();
  69. TestPluginWithAllLifecycleHooks.onWorkerBootstrapFn.mockClear();
  70. }
  71. }