create-test-environment.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { VendureConfig } from '@vendure/core';
  2. import { SimpleGraphQLClient } from './simple-graphql-client';
  3. import { TestServer } from './test-server';
  4. /**
  5. * @description
  6. * The return value of {@link createTestEnvironment}, containing the test server
  7. * and clients for the Shop API and Admin API.
  8. *
  9. * @docsCategory testing
  10. */
  11. export interface TestEnvironment {
  12. /**
  13. * @description
  14. * A Vendure server instance against which GraphQL requests can be made.
  15. */
  16. server: TestServer;
  17. /**
  18. * @description
  19. * A GraphQL client configured for the Admin API.
  20. */
  21. adminClient: SimpleGraphQLClient;
  22. /**
  23. * @description
  24. * A GraphQL client configured for the Shop API.
  25. */
  26. shopClient: SimpleGraphQLClient;
  27. }
  28. /**
  29. * @description
  30. * Configures a {@link TestServer} and a {@link SimpleGraphQLClient} for each of the GraphQL APIs
  31. * for use in end-to-end tests. Returns a {@link TestEnvironment} object.
  32. *
  33. * @example
  34. * ```ts
  35. * import { createTestEnvironment, testConfig } from '\@vendure/testing';
  36. *
  37. * describe('some feature to test', () => {
  38. *
  39. * const { server, adminClient, shopClient } = createTestEnvironment(testConfig);
  40. *
  41. * beforeAll(async () => {
  42. * await server.init({
  43. * // ... server options
  44. * });
  45. * await adminClient.asSuperAdmin();
  46. * });
  47. *
  48. * afterAll(async () => {
  49. * await server.destroy();
  50. * });
  51. *
  52. * // ... end-to-end tests here
  53. * });
  54. * ```
  55. * @docsCategory testing
  56. */
  57. export function createTestEnvironment(config: Required<VendureConfig>): TestEnvironment {
  58. const server = new TestServer(config);
  59. const { port, adminApiPath, shopApiPath } = config.apiOptions;
  60. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  61. const adminClient = new SimpleGraphQLClient(config, `http://localhost:${port}/${adminApiPath!}`);
  62. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  63. const shopClient = new SimpleGraphQLClient(config, `http://localhost:${port}/${shopApiPath!}`);
  64. return {
  65. server,
  66. adminClient,
  67. shopClient,
  68. };
  69. }