-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathapollo-nestjs.test.ts
124 lines (106 loc) · 3.1 KB
/
apollo-nestjs.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* eslint-disable @typescript-eslint/unbound-method */
import { Hub, Scope } from '@sentry/core';
import { logger } from '@sentry/utils';
import { Span } from '../../src';
import { Apollo } from '../../src/node/integrations/apollo';
import { getTestClient } from '../testutils';
type ApolloResolverGroup = {
[key: string]: () => unknown;
};
type ApolloModelResolvers = {
[key: string]: ApolloResolverGroup;
};
class GraphQLFactory {
_resolvers: ApolloModelResolvers[];
resolversExplorerService = {
explore: () => this._resolvers,
};
constructor() {
this._resolvers = [
{
Query: {
res_1(..._args: unknown[]) {
return 'foo';
},
},
Mutation: {
res_2(..._args: unknown[]) {
return 'bar';
},
},
},
];
this.mergeWithSchema();
}
public mergeWithSchema(..._args: unknown[]) {
return this.resolversExplorerService.explore();
}
}
// Jest mocks get hoisted. vars starting with `mock` are hoisted before imports.
/* eslint-disable no-var */
var mockFactory = GraphQLFactory;
// mock for @nestjs/graphql package
jest.mock('@sentry/utils', () => {
const actual = jest.requireActual('@sentry/utils');
return {
...actual,
loadModule() {
return {
GraphQLFactory: mockFactory,
};
},
};
});
describe('setupOnce', () => {
let scope = new Scope();
let parentSpan: Span;
let childSpan: Span;
let GraphQLFactoryInstance: GraphQLFactory;
beforeAll(() => {
new Apollo({
useNestjs: true,
}).setupOnce(
() => undefined,
() => new Hub(undefined, scope),
);
GraphQLFactoryInstance = new GraphQLFactory();
});
beforeEach(() => {
scope = new Scope();
parentSpan = new Span();
childSpan = parentSpan.startChild();
jest.spyOn(scope, 'getSpan').mockReturnValueOnce(parentSpan);
jest.spyOn(scope, 'setSpan');
jest.spyOn(parentSpan, 'startChild').mockReturnValueOnce(childSpan);
jest.spyOn(childSpan, 'finish');
});
it('should wrap a simple resolver', () => {
GraphQLFactoryInstance._resolvers[0]?.['Query']?.['res_1']?.();
expect(scope.getSpan).toBeCalled();
expect(parentSpan.startChild).toBeCalledWith({
description: 'Query.res_1',
op: 'graphql.resolve',
});
expect(childSpan.finish).toBeCalled();
});
it('should wrap another simple resolver', () => {
GraphQLFactoryInstance._resolvers[0]?.['Mutation']?.['res_2']?.();
expect(scope.getSpan).toBeCalled();
expect(parentSpan.startChild).toBeCalledWith({
description: 'Mutation.res_2',
op: 'graphql.resolve',
});
expect(childSpan.finish).toBeCalled();
});
it("doesn't attach when using otel instrumenter", () => {
const loggerLogSpy = jest.spyOn(logger, 'log');
const client = getTestClient({ instrumenter: 'otel' });
const hub = new Hub(client);
const integration = new Apollo({ useNestjs: true });
integration.setupOnce(
() => {},
() => hub,
);
expect(loggerLogSpy).toBeCalledWith('Apollo Integration is skipped because of instrumenter configuration.');
});
});