@dash0/sdk-web 0.18.0 → 0.18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
  import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
2
  import { InitOptions, InstrumentationName } from "../types/options";
3
3
  import { PropagatorConfig, Vars } from "../vars";
4
+ import { SERVICE_NAME } from "../semantic-conventions";
4
5
  import { init as initFun } from "./init";
5
6
 
6
7
  // Mock all the instrumentation modules
@@ -20,6 +21,15 @@ vi.mock("../instrumentations/navigation", () => ({
20
21
  startNavigationInstrumentation: vi.fn(),
21
22
  }));
22
23
 
24
+ // Mock the utils module to control loc.hostname
25
+ vi.mock("../utils", async () => {
26
+ const actual = await vi.importActual("../utils");
27
+ return {
28
+ ...actual,
29
+ loc: { hostname: "test-hostname.example.com" },
30
+ };
31
+ });
32
+
23
33
  import { startErrorInstrumentation } from "../instrumentations/errors";
24
34
  import { instrumentFetch } from "../instrumentations/http/fetch";
25
35
  import { startNavigationInstrumentation } from "../instrumentations/navigation";
@@ -226,4 +236,71 @@ describe("init", () => {
226
236
  expect(spyOnWarn).not.toHaveBeenCalled();
227
237
  });
228
238
  });
239
+
240
+ describe("serviceName fallback", () => {
241
+ it("should use provided serviceName when it is valid", async () => {
242
+ init({
243
+ ...baseOptions,
244
+ serviceName: "my-service",
245
+ });
246
+
247
+ const serviceNameAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAME);
248
+ expect(serviceNameAttr?.value.stringValue).toBe("my-service");
249
+ });
250
+
251
+ it("should fallback to location.hostname when serviceName is empty string", async () => {
252
+ init({
253
+ ...baseOptions,
254
+ serviceName: "",
255
+ });
256
+
257
+ const serviceNameAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAME);
258
+ expect(serviceNameAttr?.value.stringValue).toBe("test-hostname.example.com");
259
+ });
260
+
261
+ it("should fallback to location.hostname when serviceName is only whitespace", async () => {
262
+ init({
263
+ ...baseOptions,
264
+ serviceName: " ",
265
+ });
266
+
267
+ const serviceNameAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAME);
268
+ expect(serviceNameAttr?.value.stringValue).toBe("test-hostname.example.com");
269
+ });
270
+
271
+ it("should fallback to location.hostname when serviceName is tabs and spaces", async () => {
272
+ init({
273
+ ...baseOptions,
274
+ serviceName: " \t \n ",
275
+ });
276
+
277
+ const serviceNameAttr = vars.resource.attributes.find((attr) => attr.key === SERVICE_NAME);
278
+ expect(serviceNameAttr?.value.stringValue).toBe("test-hostname.example.com");
279
+ });
280
+
281
+ it("should fallback to 'unknown' when serviceName is empty and location.hostname is not available", async () => {
282
+ vi.resetModules();
283
+
284
+ // Mock utils module with loc as undefined
285
+ vi.doMock("../utils", async () => {
286
+ const actual = await vi.importActual("../utils");
287
+ return {
288
+ ...actual,
289
+ loc: undefined,
290
+ };
291
+ });
292
+
293
+ const { vars: importedVars } = await import("../vars");
294
+ const { init: importedInit } = await import("./init");
295
+ const { SERVICE_NAME: importedServiceName } = await import("../semantic-conventions");
296
+
297
+ importedInit({
298
+ ...baseOptions,
299
+ serviceName: "",
300
+ });
301
+
302
+ const serviceNameAttr = importedVars.resource.attributes.find((attr) => attr.key === importedServiceName);
303
+ expect(serviceNameAttr?.value.stringValue).toBe("unknown");
304
+ });
305
+ });
229
306
  });