Skip to content

Commit 1e7aed9

Browse files
authored
Add basic tests.
1 parent f9615e1 commit 1e7aed9

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* eslint-disable @typescript-eslint/unbound-method */
2+
import { Hub, Scope } from '@sentry/hub';
3+
4+
import { GraphQL } from '../../src/integrations/graphql';
5+
import { Span } from '../../src/span';
6+
7+
const GQLExecute = {
8+
execute() {
9+
return Promise.resolve();
10+
},
11+
};
12+
13+
// mock for 'graphql/execution/execution.js' package
14+
jest.mock('@sentry/utils', () => {
15+
const actual = jest.requireActual('@sentry/utils');
16+
return {
17+
...actual,
18+
loadModule() {
19+
return GQLExecute;
20+
},
21+
};
22+
});
23+
24+
describe('setupOnce', () => {
25+
let scope = new Scope();
26+
let parentSpan: Span;
27+
let childSpan: Span;
28+
29+
beforeAll(() => {
30+
new GraphQL().setupOnce(
31+
() => undefined,
32+
() => new Hub(undefined, scope),
33+
);
34+
});
35+
36+
beforeEach(() => {
37+
scope = new Scope();
38+
parentSpan = new Span();
39+
childSpan = parentSpan.startChild();
40+
jest.spyOn(scope, 'getSpan').mockReturnValueOnce(parentSpan);
41+
jest.spyOn(scope, 'setSpan');
42+
jest.spyOn(parentSpan, 'startChild').mockReturnValueOnce(childSpan);
43+
jest.spyOn(childSpan, 'finish');
44+
});
45+
46+
it(`should wrap execute method`, async () => {
47+
await GQLExecute.execute();
48+
expect(scope.getSpan).toBeCalled();
49+
expect(parentSpan.startChild).toBeCalledWith({
50+
description: 'execute',
51+
op: 'graphql',
52+
});
53+
expect(childSpan.finish).toBeCalled();
54+
expect(scope.setSpan).toHaveBeenCalledTimes(2);
55+
});
56+
});

0 commit comments

Comments
 (0)