Skip to content

Commit

Permalink
update browser; webworker tests
Browse files Browse the repository at this point in the history
- webworkers don't have DOMParser or Document APIs
- hard-coding an expected length for Document length is fragile
  (and varies across platforms)
  • Loading branch information
MustafaHaddara committed May 15, 2024
1 parent c995492 commit 14c9323
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/opentelemetry-sdk-trace-web/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export function getFetchBodyLength(...args: Parameters<typeof fetch>) {
export function getXHRBodyLength(
body: Document | XMLHttpRequestBodyInit
): number {
if (body instanceof Document) {
if (typeof Document !== 'undefined' && body instanceof Document) {
return new XMLSerializer().serializeToString(document).length;
}
// XMLHttpRequestBodyInit expands to the following:
Expand Down
7 changes: 6 additions & 1 deletion packages/opentelemetry-sdk-trace-web/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,12 +610,17 @@ describe('utils', () => {

describe('getXHRBodyLength', () => {
it('should compute body length for Document payload', () => {
// webworkers don't have DOMParser
if (typeof DOMParser === 'undefined') {
assert.ok(true);
return;
}
const doc = new DOMParser().parseFromString(
'<html><head><head/><body><p>hello world</p></body>',
'text/html'
);

assert.strictEqual(getXHRBodyLength(doc), 3456); // karma scripts get injected into the html :shakes_fist:
assert.ok(getXHRBodyLength(doc) > 0, 'body length is 0');
});
it('should compute body length for Blob payload', () => {
const blob = new Blob(['hello world'], {
Expand Down

0 comments on commit 14c9323

Please sign in to comment.