Skip to content

Commit 18366e7

Browse files
committed
add tests
1 parent cadeabb commit 18366e7

File tree

5 files changed

+110
-19
lines changed

5 files changed

+110
-19
lines changed

packages/node/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"eslint": "7.6.0",
3636
"express": "^4.17.1",
3737
"jest": "^24.7.1",
38+
"nock": "^13.0.5",
3839
"npm-run-all": "^4.1.2",
3940
"prettier": "1.19.0",
4041
"rimraf": "^2.6.3",
Lines changed: 87 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,91 @@
1-
import { RequestOptions } from 'http';
2-
3-
import { extractUrl } from './../../src/integrations/http';
4-
5-
describe('extractUrl()', () => {
6-
const urlString = 'http://dogs.are.great:1231/yay/';
7-
const urlParts: RequestOptions = {
8-
protocol: 'http:',
9-
host: 'dogs.are.great',
10-
method: 'GET',
11-
path: '/yay/',
12-
port: 1231,
13-
};
14-
15-
it('accepts a url string', () => {
16-
expect(extractUrl(urlString)).toBe(urlString);
1+
import * as sentryCore from '@sentry/core';
2+
import { Hub } from '@sentry/hub';
3+
import * as hubModule from '@sentry/hub';
4+
import { addExtensionMethods, Span, TRACEPARENT_REGEXP, Transaction } from '@sentry/tracing';
5+
import * as http from 'http';
6+
import * as nock from 'nock';
7+
8+
import { NodeClient } from '../../src/client';
9+
import { Http as HttpIntegration } from '../../src/integrations/http';
10+
11+
describe('tracing', () => {
12+
function createTransactionOnScope() {
13+
const hub = new Hub(
14+
new NodeClient({
15+
dsn: 'https://[email protected]/12312012',
16+
tracesSampleRate: 1.0,
17+
integrations: [new HttpIntegration({ tracing: true })],
18+
}),
19+
);
20+
addExtensionMethods();
21+
22+
// we need to mock both of these because the tracing handler relies on `@sentry/core` while the sampler relies on
23+
// `@sentry/hub`, and mocking breaks the link between the two
24+
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
25+
jest.spyOn(hubModule, 'getCurrentHub').mockReturnValue(hub);
26+
27+
const transaction = hub.startTransaction({ name: 'dogpark' });
28+
hub.getScope()?.setSpan(transaction);
29+
30+
return transaction;
31+
}
32+
33+
it("creates a span for each outgoing non-sentry request when there's a transaction on the scope", () => {
34+
nock('http://dogs.are.great')
35+
.get('/')
36+
.reply(200);
37+
38+
const transaction = createTransactionOnScope();
39+
const spans = (transaction as Span).spanRecorder?.spans as Span[];
40+
41+
http.get('http://dogs.are.great/');
42+
43+
// TODO: For some reason in node 6 two request spans are appearing. Once we stop testing against it, this can go
44+
// back to being `toEqual()`.
45+
expect(spans.length).toBeGreaterThanOrEqual(2);
46+
expect(spans[1].description).toEqual('GET http://dogs.are.great/');
47+
expect(spans[1].op).toEqual('request');
48+
});
49+
50+
it("doesn't create a span for outgoing sentry requests", () => {
51+
nock('http://squirrelchasers.ingest.sentry.io')
52+
.get('/api/12312012/store/')
53+
.reply(200);
54+
55+
const transaction = createTransactionOnScope();
56+
const spans = (transaction as Span).spanRecorder?.spans as Span[];
57+
58+
http.get('http://squirrelchasers.ingest.sentry.io/api/12312012/store/');
59+
60+
// only the transaction itself should be there
61+
expect(spans.length).toEqual(1);
62+
expect((spans[0] as Transaction).name).toEqual('dogpark');
1763
});
1864

19-
it('accepts a http.RequestOptions object and returns a string with everything in the right place', () => {
20-
expect(extractUrl(urlParts)).toBe(urlString);
65+
it('attaches the sentry-trace header to outgoing non-sentry requests', async () => {
66+
nock('http://dogs.are.great')
67+
.get('/')
68+
.reply(200);
69+
70+
createTransactionOnScope();
71+
72+
const request = http.get('http://dogs.are.great/');
73+
const sentryTraceHeader = request.getHeader('sentry-trace') as string;
74+
75+
expect(sentryTraceHeader).toBeDefined();
76+
expect(TRACEPARENT_REGEXP.test(sentryTraceHeader)).toBe(true);
77+
});
78+
79+
it("doesn't attach the sentry-trace header to outgoing sentry requests", () => {
80+
nock('http://squirrelchasers.ingest.sentry.io')
81+
.get('/api/12312012/store/')
82+
.reply(200);
83+
84+
createTransactionOnScope();
85+
86+
const request = http.get('http://squirrelchasers.ingest.sentry.io/api/12312012/store/');
87+
const sentryTraceHeader = request.getHeader('sentry-trace');
88+
89+
expect(sentryTraceHeader).not.toBeDefined();
2190
});
2291
});

packages/tracing/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ addExtensionMethods();
1515

1616
export { addExtensionMethods };
1717

18-
export { extractTraceparentData, getActiveTransaction, hasTracingEnabled, stripUrlQueryAndFragment } from './utils';
18+
export {
19+
extractTraceparentData,
20+
getActiveTransaction,
21+
hasTracingEnabled,
22+
stripUrlQueryAndFragment,
23+
TRACEPARENT_REGEXP,
24+
} from './utils';

scripts/test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ source ~/.nvm/nvm.sh
66
if [[ "$(cut -d. -f1 <<< "$TRAVIS_NODE_VERSION")" -le 6 ]]; then
77
nvm use 8
88
yarn install --ignore-engines --ignore-scripts
9+
# current versions of nock don't support node 6
10+
cd packages/node
11+
yarn add --dev --ignore-engines [email protected]
12+
yarn list --pattern nock
13+
cd ../..
914
# ember requires Node >= 10 to build
1015
yarn build --ignore="@sentry/ember" --ignore="@sentry/serverless" --ignore="@sentry/gatsby" --ignore="@sentry/react"
1116
nvm use 6

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14159,6 +14159,16 @@ nock@^13.0.4:
1415914159
lodash.set "^4.3.2"
1416014160
propagate "^2.0.0"
1416114161

14162+
nock@^13.0.5:
14163+
version "13.0.5"
14164+
resolved "https://registry.yarnpkg.com/nock/-/nock-13.0.5.tgz#a618c6f86372cb79fac04ca9a2d1e4baccdb2414"
14165+
integrity sha512-1ILZl0zfFm2G4TIeJFW0iHknxr2NyA+aGCMTjDVUsBY4CkMRispF1pfIYkTRdAR/3Bg+UzdEuK0B6HczMQZcCg==
14166+
dependencies:
14167+
debug "^4.1.0"
14168+
json-stringify-safe "^5.0.1"
14169+
lodash.set "^4.3.2"
14170+
propagate "^2.0.0"
14171+
1416214172
1416314173
version "1.0.5"
1416414174
resolved "https://registry.yarnpkg.com/node-environment-flags/-/node-environment-flags-1.0.5.tgz#fa930275f5bf5dae188d6192b24b4c8bbac3d76a"

0 commit comments

Comments
 (0)