Skip to content

Commit

Permalink
Port ignoreNetworkEvents tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chancancode committed Jan 14, 2025
1 parent 54b7fc1 commit ed3d379
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ describe('fetch', () => {
});
});

describe('when network events are ignored', () => {
xdescribe('when network events are ignored', () => {
beforeEach(async () => {
await prepareData(url, () => getData(url), {
ignoreNetworkEvents: true,
Expand All @@ -1203,13 +1203,13 @@ describe('fetch', () => {
afterEach(() => {
clearData();
});
it('should NOT add network events', () => {
xit('should NOT add network events', () => {
const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
const events = span.events;
assert.strictEqual(events.length, 0, 'number of events is wrong');
});

it('should still add the CONTENT_LENGTH attribute', () => {
xit('should still add the CONTENT_LENGTH attribute', () => {
const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
const attributes = span.attributes;
const responseContentLength = attributes[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1712,5 +1712,145 @@ describe('fetch', () => {
});
});
});

describe('`ignoreNetworkEvents` config', () => {
const tracedFetch = async ({
handlers = [
msw.http.get('/api/status.json', () => {
return msw.HttpResponse.json({ ok: true });
}),
],
callback = () => fetch('/api/status.json'),
config = {},
}: {
handlers?: msw.RequestHandler[];
callback?: () => Promise<Response>;
config?: FetchInstrumentationConfig;
} = {}): Promise<{ rootSpan: api.Span; response: Response }> => {
let response: Response | undefined;

await startWorker(...handlers);

const rootSpan = await trace(async () => {
response = await callback();
}, config);

assert.ok(response instanceof Response);
assert.strictEqual(exportedSpans.length, 1);

return { rootSpan, response };
};

describe('when `ignoreNetworkEvents` is not set', function () {
let response: Response | undefined;

beforeEach(async () => {
const result = await tracedFetch();
response = result.response;
});

afterEach(() => {
response = undefined;
});

it('span should have correct events', async () => {
const span: tracing.ReadableSpan = exportedSpans[0];
const events = span.events;
assert.strictEqual(events.length, 8, 'number of events is wrong');
testForCorrectEvents(events, [
PTN.FETCH_START,
PTN.DOMAIN_LOOKUP_START,
PTN.DOMAIN_LOOKUP_END,
PTN.CONNECT_START,
PTN.CONNECT_END,
PTN.REQUEST_START,
PTN.RESPONSE_START,
PTN.RESPONSE_END,
]);
});

it('span should have http.response_content_length attribute', () => {
const span: tracing.ReadableSpan = exportedSpans[0];
assert.strictEqual(
span.attributes[SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH],
parseInt(response!.headers.get('content-length')!),
`attributes ${SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH} is <= 0`
);
});
});

describe('when `ignoreNetworkEvents` is `false`', function () {
let response: Response | undefined;

beforeEach(async () => {
const result = await tracedFetch({
config: { ignoreNetworkEvents: false },
});
response = result.response;
});

afterEach(() => {
response = undefined;
});

it('span should have correct events', async () => {
const span: tracing.ReadableSpan = exportedSpans[0];
const events = span.events;
assert.strictEqual(events.length, 8, 'number of events is wrong');
testForCorrectEvents(events, [
PTN.FETCH_START,
PTN.DOMAIN_LOOKUP_START,
PTN.DOMAIN_LOOKUP_END,
PTN.CONNECT_START,
PTN.CONNECT_END,
PTN.REQUEST_START,
PTN.RESPONSE_START,
PTN.RESPONSE_END,
]);
});

it('span should have http.response_content_length attribute', () => {
const span: tracing.ReadableSpan = exportedSpans[0];
assert.strictEqual(
span.attributes[SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH],
parseInt(response!.headers.get('content-length')!),
`attributes ${SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH} is <= 0`
);
});
});

describe('when `ignoreNetworkEvents` is `true`', function () {
let response: Response | undefined;

beforeEach(async () => {
const result = await tracedFetch({
config: { ignoreNetworkEvents: true },
});
response = result.response;
});

afterEach(() => {
response = undefined;
});

it('span should have no events', async () => {
const span: tracing.ReadableSpan = exportedSpans[0];
assert.strictEqual(
span.events.length,
0,
'should not have any events'
);
});

it('span should have http.response_content_length attribute', () => {
const span: tracing.ReadableSpan = exportedSpans[0];
assert.strictEqual(
span.attributes[SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH],
parseInt(response!.headers.get('content-length')!),
`attributes ${SEMATTRS_HTTP_RESPONSE_CONTENT_LENGTH} is <= 0`
);
});
});
});
});
});

0 comments on commit ed3d379

Please sign in to comment.