Skip to content

Commit

Permalink
chore(exporter-otlp-*-http): clean up tests (#5198)
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc authored Nov 26, 2024
1 parent 556eecf commit e35f3d6
Show file tree
Hide file tree
Showing 16 changed files with 589 additions and 3,100 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ All notable changes to experimental packages in this project will be documented

### :house: (Internal)

* chore(otlp-exporter-\*-http): clean up tests [#5196](https://github.com/open-telemetry/opentelemetry-js/pull/5198) @pichlermarc
* chore(otlp-exporter-\*-proto): clean up tests [#5196](https://github.com/open-telemetry/opentelemetry-js/pull/5199) @pichlermarc

## 0.55.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,78 @@ import * as assert from 'assert';
import * as sinon from 'sinon';

import { OTLPLogExporter } from '../../src/platform/browser';
import {
LoggerProvider,
SimpleLogRecordProcessor,
} from '@opentelemetry/sdk-logs';

describe('OTLPLogExporter', () => {
/*
* NOTE: Tests here are not intended to test the underlying components directly. They are intended as a quick
* check if the correct components are used. Use the following packages to test details:
* - `@opentelemetry/oltp-exporter-base`: OTLP common exporter logic (handling of concurrent exports, ...), HTTP transport code
* - `@opentelemetry/otlp-transformer`: Everything regarding serialization and transforming internal representations to OTLP
*/

describe('OTLPLogExporter', function () {
afterEach(() => {
sinon.restore();
});

describe('constructor', () => {
it('should create an instance', () => {
const exporter = new OTLPLogExporter();
assert.ok(exporter instanceof OTLPLogExporter);
describe('export', function () {
describe('when sendBeacon is available', function () {
it('should successfully send data using sendBeacon', async function () {
// arrange
const stubBeacon = sinon.stub(navigator, 'sendBeacon');
const loggerProvider = new LoggerProvider();
loggerProvider.addLogRecordProcessor(
new SimpleLogRecordProcessor(new OTLPLogExporter())
);

// act
loggerProvider.getLogger('test-logger').emit({ body: 'test-body' });
await loggerProvider.shutdown();

// assert
const args = stubBeacon.args[0];
const blob: Blob = args[1] as unknown as Blob;
const body = await blob.text();
assert.doesNotThrow(
() => JSON.parse(body),
'expected requestBody to be in JSON format, but parsing failed'
);
});
});

describe('when sendBeacon is not available', function () {
beforeEach(function () {
// fake sendBeacon not being available
(window.navigator as any).sendBeacon = false;
});

it('should successfully send data using XMLHttpRequest', async function () {
// arrange
const server = sinon.fakeServer.create();
const loggerProvider = new LoggerProvider();
loggerProvider.addLogRecordProcessor(
new SimpleLogRecordProcessor(new OTLPLogExporter())
);

// act
loggerProvider.getLogger('test-logger').emit({ body: 'test-body' });
queueMicrotask(() => {
// simulate success response
server.requests[0].respond(200, {}, '');
});
await loggerProvider.shutdown();

// assert
const request = server.requests[0];
const body = request.requestBody as unknown as Uint8Array;
assert.doesNotThrow(
() => JSON.parse(new TextDecoder().decode(body)),
'expected requestBody to be in JSON format, but parsing failed'
);
});
});
});
});
167 changes: 0 additions & 167 deletions experimental/packages/exporter-logs-otlp-http/test/logHelper.ts

This file was deleted.

Loading

0 comments on commit e35f3d6

Please sign in to comment.