Skip to content

Commit c729954

Browse files
committed
Add integration test.
1 parent a37edee commit c729954

File tree

4 files changed

+344
-13
lines changed

4 files changed

+344
-13
lines changed

packages/node-integration-tests/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
"@types/mongodb": "^3.6.20",
1919
"@types/mysql": "^2.15.21",
2020
"@types/pg": "^8.6.5",
21+
"apollo-server": "^3.6.7",
2122
"cors": "^2.8.5",
2223
"express": "^4.17.3",
24+
"graphql": "^16.3.0",
2325
"mongodb": "^3.7.3",
2426
"mongodb-memory-server-global": "^7.6.3",
2527
"mysql": "^2.18.1",
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as Sentry from '@sentry/node';
2+
import * as Tracing from '@sentry/tracing';
3+
import { ApolloServer, gql } from 'apollo-server';
4+
5+
Sentry.init({
6+
dsn: 'https://[email protected]/1337',
7+
release: '1.0',
8+
tracesSampleRate: 1.0,
9+
integrations: [new Tracing.Integrations.GraphQL(), new Tracing.Integrations.Apollo()],
10+
});
11+
12+
const typeDefs = gql`
13+
type Query {
14+
hello: String
15+
}
16+
`;
17+
18+
const resolvers = {
19+
Query: {
20+
hello: () => {
21+
return 'Hello world!';
22+
},
23+
},
24+
};
25+
26+
const server = new ApolloServer({
27+
typeDefs,
28+
resolvers,
29+
});
30+
31+
const transaction = Sentry.startTransaction({ name: 'test_transaction', op: 'transaction' });
32+
33+
Sentry.configureScope(scope => {
34+
scope.setSpan(transaction);
35+
});
36+
37+
void (async () => {
38+
// Ref: https://www.apollographql.com/docs/apollo-server/testing/testing/#testing-using-executeoperation
39+
await server.executeOperation({
40+
query: '{hello}',
41+
});
42+
43+
transaction.finish();
44+
})();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { assertSentryTransaction, getEnvelopeRequest, runServer } from '../../../utils';
2+
3+
test('should instrument GraphQL and Apollo Server.', async () => {
4+
const url = await runServer(__dirname);
5+
const envelope = await getEnvelopeRequest(url);
6+
7+
expect(envelope).toHaveLength(3);
8+
9+
const transaction = envelope[2];
10+
const parentSpanId = (transaction as any)?.contexts?.trace?.span_id;
11+
const graphqlSpanId = (transaction as any)?.spans?.[0].span_id;
12+
13+
expect(parentSpanId).toBeDefined();
14+
expect(graphqlSpanId).toBeDefined();
15+
16+
assertSentryTransaction(transaction, {
17+
transaction: 'test_transaction',
18+
spans: [
19+
{
20+
description: 'execute',
21+
op: 'db.graphql',
22+
parent_span_id: parentSpanId,
23+
},
24+
{
25+
description: 'Query.hello',
26+
op: 'db.graphql.apollo',
27+
parent_span_id: graphqlSpanId,
28+
},
29+
],
30+
});
31+
});

0 commit comments

Comments
 (0)