-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathgraphql.test.ts
77 lines (65 loc) · 2.02 KB
/
graphql.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
/* eslint-disable @typescript-eslint/unbound-method */
import { Hub, Scope } from '@sentry/core';
import { logger } from '@sentry/utils';
import { Span } from '../../src';
import { GraphQL } from '../../src/node/integrations/graphql';
import { getTestClient } from '../testutils';
const GQLExecute = {
execute() {
return Promise.resolve();
},
};
// Jest mocks get hoisted. vars starting with `mock` are hoisted before imports.
/* eslint-disable no-var */
var mockClient = GQLExecute;
// mock for 'graphql/execution/execution.js' package
jest.mock('@sentry/utils', () => {
const actual = jest.requireActual('@sentry/utils');
return {
...actual,
loadModule() {
return mockClient;
},
};
});
describe('setupOnce', () => {
let scope = new Scope();
let parentSpan: Span;
let childSpan: Span;
beforeAll(() => {
new GraphQL().setupOnce(
() => undefined,
() => new Hub(undefined, scope),
);
});
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 execute method', async () => {
await GQLExecute.execute();
expect(scope.getSpan).toBeCalled();
expect(parentSpan.startChild).toBeCalledWith({
description: 'execute',
op: 'graphql.execute',
});
expect(childSpan.finish).toBeCalled();
expect(scope.setSpan).toHaveBeenCalledTimes(2);
});
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 GraphQL();
integration.setupOnce(
() => {},
() => hub,
);
expect(loggerLogSpy).toBeCalledWith('GraphQL Integration is skipped because of instrumenter configuration.');
});
});