test-db-initializer.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { DataSourceOptions } from 'typeorm';
  2. import { BaseConnectionOptions } from 'typeorm/connection/BaseConnectionOptions';
  3. /**
  4. * @description
  5. * Defines how the e2e TestService sets up a particular DB to run a single test suite.
  6. * The `\@vendure/testing` package ships with initializers for sql.js, MySQL & Postgres.
  7. *
  8. * Custom initializers can be created by implementing this interface and registering
  9. * it with the {@link registerInitializer} function:
  10. *
  11. * @example
  12. * ```ts
  13. * export class CockroachDbInitializer implements TestDbInitializer<CockroachConnectionOptions> {
  14. * // database-specific implementation goes here
  15. * }
  16. *
  17. * registerInitializer('cockroachdb', new CockroachDbInitializer());
  18. * ```
  19. *
  20. * @docsCategory testing
  21. */
  22. export interface TestDbInitializer<T extends BaseConnectionOptions> {
  23. /**
  24. * @description
  25. * Responsible for creating a database for the current test suite.
  26. * Typically, this method will:
  27. *
  28. * * use the testFileName parameter to derive a database name
  29. * * create the database
  30. * * mutate the `connetionOptions` object to point to that new database
  31. */
  32. init(testFileName: string, connectionOptions: T): Promise<T>;
  33. /**
  34. * @description
  35. * Execute the populateFn to populate your database.
  36. */
  37. populate(populateFn: () => Promise<void>): Promise<void>;
  38. /**
  39. * @description
  40. * Clean up any resources used during the init() phase (i.e. close open DB connections)
  41. */
  42. destroy(): void | Promise<void>;
  43. }