|
| 1 | +/* eslint-disable @typescript-eslint/unbound-method */ |
| 2 | +import { Hub, Scope } from '@sentry/hub'; |
| 3 | + |
| 4 | +import { Apollo } from '../../src/integrations/apollo'; |
| 5 | +import { Span } from '../../src/span'; |
| 6 | + |
| 7 | +type ApolloResolverGroup = { |
| 8 | + [key: string]: () => any; |
| 9 | +}; |
| 10 | + |
| 11 | +type ApolloModelResolvers = { |
| 12 | + [key: string]: ApolloResolverGroup; |
| 13 | +}; |
| 14 | + |
| 15 | +class ApolloServerBase { |
| 16 | + config: { |
| 17 | + resolvers: ApolloModelResolvers[]; |
| 18 | + }; |
| 19 | + |
| 20 | + constructor() { |
| 21 | + this.config = { |
| 22 | + resolvers: [ |
| 23 | + { |
| 24 | + Query: { |
| 25 | + res_1(...args: unknown[]) { |
| 26 | + return 'foo'; |
| 27 | + }, |
| 28 | + }, |
| 29 | + Mutation: { |
| 30 | + res_2(...args: unknown[]) { |
| 31 | + return 'bar'; |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + ], |
| 36 | + }; |
| 37 | + |
| 38 | + this.constructSchema(); |
| 39 | + } |
| 40 | + |
| 41 | + public constructSchema(...args: unknown[]) { |
| 42 | + return null; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// mock for 'graphql/execution/execution.js' package |
| 47 | +jest.mock('@sentry/utils', () => { |
| 48 | + const actual = jest.requireActual('@sentry/utils'); |
| 49 | + return { |
| 50 | + ...actual, |
| 51 | + loadModule() { |
| 52 | + return { |
| 53 | + ApolloServerBase, |
| 54 | + }; |
| 55 | + }, |
| 56 | + }; |
| 57 | +}); |
| 58 | + |
| 59 | +describe('setupOnce', () => { |
| 60 | + let scope = new Scope(); |
| 61 | + let parentSpan: Span; |
| 62 | + let childSpan: Span; |
| 63 | + let ApolloServer: ApolloServerBase; |
| 64 | + |
| 65 | + beforeAll(() => { |
| 66 | + new Apollo().setupOnce( |
| 67 | + () => undefined, |
| 68 | + () => new Hub(undefined, scope), |
| 69 | + ); |
| 70 | + |
| 71 | + ApolloServer = new ApolloServerBase(); |
| 72 | + }); |
| 73 | + |
| 74 | + beforeEach(() => { |
| 75 | + scope = new Scope(); |
| 76 | + parentSpan = new Span(); |
| 77 | + childSpan = parentSpan.startChild(); |
| 78 | + jest.spyOn(scope, 'getSpan').mockReturnValueOnce(parentSpan); |
| 79 | + jest.spyOn(scope, 'setSpan'); |
| 80 | + jest.spyOn(parentSpan, 'startChild').mockReturnValueOnce(childSpan); |
| 81 | + jest.spyOn(childSpan, 'finish'); |
| 82 | + }); |
| 83 | + |
| 84 | + it(`should wrap a simple resolver`, () => { |
| 85 | + ApolloServer.config.resolvers[0]?.['Query']?.['res_1']?.(); |
| 86 | + expect(scope.getSpan).toBeCalled(); |
| 87 | + expect(parentSpan.startChild).toBeCalledWith({ |
| 88 | + description: 'Query.res_1', |
| 89 | + op: 'apollo', |
| 90 | + }); |
| 91 | + expect(childSpan.finish).toBeCalled(); |
| 92 | + expect(scope.setSpan).toHaveBeenCalledTimes(2); |
| 93 | + }); |
| 94 | + |
| 95 | + it(`should wrap another simple resolver`, () => { |
| 96 | + ApolloServer.config.resolvers[0]?.['Mutation']?.['res_2']?.(); |
| 97 | + expect(scope.getSpan).toBeCalled(); |
| 98 | + expect(parentSpan.startChild).toBeCalledWith({ |
| 99 | + description: 'Mutation.res_2', |
| 100 | + op: 'apollo', |
| 101 | + }); |
| 102 | + expect(childSpan.finish).toBeCalled(); |
| 103 | + expect(scope.setSpan).toHaveBeenCalledTimes(2); |
| 104 | + }); |
| 105 | +}); |
0 commit comments