Skip to content

Commit 19a538d

Browse files
committed
chore(tracing): fix test requests via fake server
1 parent 3993dd3 commit 19a538d

File tree

1 file changed

+99
-85
lines changed

1 file changed

+99
-85
lines changed

packages/tracing/test/hub.test.ts

Lines changed: 99 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable @typescript-eslint/unbound-method */
2+
import { fakeServer, SinonFakeServer } from 'sinon';
23
import { BrowserClient } from '@sentry/browser';
34
import { Hub, makeMain } from '@sentry/hub';
45
import * as utilsModule from '@sentry/utils'; // for mocking
@@ -356,91 +357,104 @@ describe('Hub', () => {
356357
expect(child.sampled).toBe(transaction.sampled);
357358
});
358359

359-
// TODO the way we dig out the headers to test them doesn't work on Node < 10
360-
testOnlyIfNodeVersionAtLeast(10)(
361-
'should propagate positive sampling decision to child transactions in XHR header',
362-
() => {
363-
const hub = new Hub(
364-
new BrowserClient({
365-
dsn: 'https://[email protected]/1121',
366-
tracesSampleRate: 1,
367-
integrations: [new BrowserTracing()],
368-
}),
369-
);
370-
makeMain(hub);
371-
372-
const transaction = hub.startTransaction({ name: 'dogpark' });
373-
hub.configureScope(scope => {
374-
scope.setSpan(transaction);
375-
});
376-
377-
const request = new XMLHttpRequest();
378-
request.open('GET', '/chase-partners');
379-
request.send();
380-
381-
// mock a response having been received successfully (we have to do it in this roundabout way because readyState
382-
// is readonly and changing it doesn't trigger a readystatechange event)
383-
Object.defineProperty(request, 'readyState', { value: 4 });
384-
request.dispatchEvent(new Event('readystatechange'));
385-
386-
// this looks weird, it's true, but it's really just `request.impl.flag.requestHeaders` - it's just that the
387-
// `impl` key is a symbol rather than a string, and therefore needs to be referred to by reference rather than
388-
// value
389-
const headers = (request as any)[getSymbolObjectKeyByName(request, 'impl') as symbol].flag.requestHeaders;
390-
391-
// check that sentry-trace header is added to request
392-
expect(headers).toEqual(
393-
expect.objectContaining({ 'sentry-trace': expect.stringMatching(TRACEPARENT_REGEXP) }),
394-
);
395-
396-
// check that sampling decision is passed down correctly
397-
expect(transaction.sampled).toBe(true);
398-
expect(extractTraceparentData(headers['sentry-trace'])!.parentSampled).toBe(true);
399-
},
400-
);
401-
402-
// TODO the way we dig out the headers to test them doesn't work on Node < 10
403-
testOnlyIfNodeVersionAtLeast(10)(
404-
'should propagate negative sampling decision to child transactions in XHR header',
405-
() => {
406-
const hub = new Hub(
407-
new BrowserClient({
408-
dsn: 'https://[email protected]/1121',
409-
tracesSampleRate: 1,
410-
integrations: [new BrowserTracing()],
411-
}),
412-
);
413-
makeMain(hub);
414-
415-
const transaction = hub.startTransaction({ name: 'dogpark', sampled: false });
416-
hub.configureScope(scope => {
417-
scope.setSpan(transaction);
418-
});
419-
420-
const request = new XMLHttpRequest();
421-
request.open('GET', '/chase-partners');
422-
request.send();
423-
424-
// mock a response having been received successfully (we have to do it in this roundabout way because readyState
425-
// is readonly and changing it doesn't trigger a readystatechange event)
426-
Object.defineProperty(request, 'readyState', { value: 4 });
427-
request.dispatchEvent(new Event('readystatechange'));
428-
429-
// this looks weird, it's true, but it's really just `request.impl.flag.requestHeaders` - it's just that the
430-
// `impl` key is a symbol rather than a string, and therefore needs to be referred to by reference rather than
431-
// value
432-
const headers = (request as any)[getSymbolObjectKeyByName(request, 'impl') as symbol].flag.requestHeaders;
433-
434-
// check that sentry-trace header is added to request
435-
expect(headers).toEqual(
436-
expect.objectContaining({ 'sentry-trace': expect.stringMatching(TRACEPARENT_REGEXP) }),
437-
);
438-
439-
// check that sampling decision is passed down correctly
440-
expect(transaction.sampled).toBe(false);
441-
expect(extractTraceparentData(headers['sentry-trace'])!.parentSampled).toBe(false);
442-
},
443-
);
360+
describe('with fake server', () => {
361+
let server: SinonFakeServer;
362+
363+
beforeEach(() => {
364+
server = fakeServer.create();
365+
server.respondImmediately = true;
366+
});
367+
368+
afterEach(() => {
369+
server.restore();
370+
});
371+
372+
// TODO the way we dig out the headers to test them doesn't work on Node < 10
373+
testOnlyIfNodeVersionAtLeast(10)(
374+
'should propagate positive sampling decision to child transactions in XHR header',
375+
() => {
376+
const hub = new Hub(
377+
new BrowserClient({
378+
dsn: 'https://[email protected]/1121',
379+
tracesSampleRate: 1,
380+
integrations: [new BrowserTracing()],
381+
}),
382+
);
383+
makeMain(hub);
384+
385+
const transaction = hub.startTransaction({ name: 'dogpark' });
386+
hub.configureScope(scope => {
387+
scope.setSpan(transaction);
388+
});
389+
390+
const request = new XMLHttpRequest();
391+
request.open('GET', '/chase-partners');
392+
request.send();
393+
394+
// mock a response having been received successfully (we have to do it in this roundabout way because readyState
395+
// is readonly and changing it doesn't trigger a readystatechange event)
396+
Object.defineProperty(request, 'readyState', { value: 4 });
397+
request.dispatchEvent(new Event('readystatechange'));
398+
399+
// this looks weird, it's true, but it's really just `request.impl.flag.requestHeaders` - it's just that the
400+
// `impl` key is a symbol rather than a string, and therefore needs to be referred to by reference rather than
401+
// value
402+
const headers = (request as any)[getSymbolObjectKeyByName(request, 'impl') as symbol].flag.requestHeaders;
403+
404+
// check that sentry-trace header is added to request
405+
expect(headers).toEqual(
406+
expect.objectContaining({ 'sentry-trace': expect.stringMatching(TRACEPARENT_REGEXP) }),
407+
);
408+
409+
// check that sampling decision is passed down correctly
410+
expect(transaction.sampled).toBe(true);
411+
expect(extractTraceparentData(headers['sentry-trace'])!.parentSampled).toBe(true);
412+
},
413+
);
414+
415+
// TODO the way we dig out the headers to test them doesn't work on Node < 10
416+
testOnlyIfNodeVersionAtLeast(10)(
417+
'should propagate negative sampling decision to child transactions in XHR header',
418+
() => {
419+
const hub = new Hub(
420+
new BrowserClient({
421+
dsn: 'https://[email protected]/1121',
422+
tracesSampleRate: 1,
423+
integrations: [new BrowserTracing()],
424+
}),
425+
);
426+
makeMain(hub);
427+
428+
const transaction = hub.startTransaction({ name: 'dogpark', sampled: false });
429+
hub.configureScope(scope => {
430+
scope.setSpan(transaction);
431+
});
432+
433+
const request = new XMLHttpRequest();
434+
request.open('GET', '/chase-partners');
435+
request.send();
436+
437+
// mock a response having been received successfully (we have to do it in this roundabout way because readyState
438+
// is readonly and changing it doesn't trigger a readystatechange event)
439+
Object.defineProperty(request, 'readyState', { value: 4 });
440+
request.dispatchEvent(new Event('readystatechange'));
441+
442+
// this looks weird, it's true, but it's really just `request.impl.flag.requestHeaders` - it's just that the
443+
// `impl` key is a symbol rather than a string, and therefore needs to be referred to by reference rather than
444+
// value
445+
const headers = (request as any)[getSymbolObjectKeyByName(request, 'impl') as symbol].flag.requestHeaders;
446+
447+
// check that sentry-trace header is added to request
448+
expect(headers).toEqual(
449+
expect.objectContaining({ 'sentry-trace': expect.stringMatching(TRACEPARENT_REGEXP) }),
450+
);
451+
452+
// check that sampling decision is passed down correctly
453+
expect(transaction.sampled).toBe(false);
454+
expect(extractTraceparentData(headers['sentry-trace'])!.parentSampled).toBe(false);
455+
},
456+
);
457+
});
444458

445459
it('should propagate positive sampling decision to child transactions in fetch header', () => {
446460
// TODO

0 commit comments

Comments
 (0)