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

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