apollo-server-plugin.e2e-spec.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { ApolloServerPlugin, GraphQLRequestListener, GraphQLServerContext } from '@apollo/server';
  2. import { mergeConfig } from '@vendure/core';
  3. import path from 'path';
  4. import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
  5. import { initialData } from '../../../e2e-common/e2e-initial-data';
  6. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  7. import { createTestEnvironment } from '../../testing/lib/create-test-environment';
  8. import { getProductIdNameDocument } from './graphql/shared-definitions';
  9. class MyApolloServerPlugin implements ApolloServerPlugin {
  10. static serverWillStartFn = vi.fn();
  11. static requestDidStartFn = vi.fn();
  12. static willSendResponseFn = vi.fn();
  13. static reset() {
  14. this.serverWillStartFn = vi.fn();
  15. this.requestDidStartFn = vi.fn();
  16. this.willSendResponseFn = vi.fn();
  17. }
  18. async serverWillStart(service: GraphQLServerContext): Promise<void> {
  19. MyApolloServerPlugin.serverWillStartFn(service);
  20. }
  21. async requestDidStart(): Promise<GraphQLRequestListener<any>> {
  22. MyApolloServerPlugin.requestDidStartFn();
  23. return {
  24. async willSendResponse(requestContext): Promise<void> {
  25. const { body } = requestContext.response;
  26. if (body.kind === 'single') {
  27. MyApolloServerPlugin.willSendResponseFn(body.singleResult.data);
  28. }
  29. },
  30. };
  31. }
  32. }
  33. describe('custom apolloServerPlugins', () => {
  34. const { server, adminClient, shopClient } = createTestEnvironment(
  35. mergeConfig(testConfig(), {
  36. apiOptions: {
  37. apolloServerPlugins: [new MyApolloServerPlugin()],
  38. },
  39. }),
  40. );
  41. beforeAll(async () => {
  42. await server.init({
  43. initialData,
  44. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  45. customerCount: 1,
  46. });
  47. await adminClient.asSuperAdmin();
  48. }, TEST_SETUP_TIMEOUT_MS);
  49. afterAll(async () => {
  50. await server.destroy();
  51. });
  52. it('calls serverWillStart()', () => {
  53. expect(MyApolloServerPlugin.serverWillStartFn).toHaveBeenCalled();
  54. });
  55. it('runs plugin on shop api query', async () => {
  56. MyApolloServerPlugin.reset();
  57. await shopClient.query(getProductIdNameDocument, { id: 'T_1' });
  58. expect(MyApolloServerPlugin.requestDidStartFn).toHaveBeenCalledTimes(1);
  59. expect(MyApolloServerPlugin.willSendResponseFn).toHaveBeenCalledTimes(1);
  60. expect(MyApolloServerPlugin.willSendResponseFn.mock.calls[0][0]).toEqual({
  61. product: {
  62. id: 'T_1',
  63. name: 'Laptop',
  64. },
  65. });
  66. });
  67. it('runs plugin on admin api query', async () => {
  68. MyApolloServerPlugin.reset();
  69. await adminClient.query(getProductIdNameDocument, { id: 'T_1' });
  70. expect(MyApolloServerPlugin.requestDidStartFn).toHaveBeenCalledTimes(1);
  71. expect(MyApolloServerPlugin.willSendResponseFn).toHaveBeenCalledTimes(1);
  72. expect(MyApolloServerPlugin.willSendResponseFn.mock.calls[0][0]).toEqual({
  73. product: {
  74. id: 'T_1',
  75. name: 'Laptop',
  76. },
  77. });
  78. });
  79. });