|
| 1 | +import path from "node:path"; |
1 | 2 | import { describe, expect, it } from "vitest"; |
2 | 3 | import { LabelName } from "../../../src/model.js"; |
3 | | -import { getSuiteLabels } from "../../../src/sdk/reporter/utils.js"; |
| 4 | +import { getPackageName, getRelativePath, getSuiteLabels } from "../../../src/sdk/reporter/utils.js"; |
4 | 5 |
|
5 | 6 | describe("getSuiteLabels", () => { |
6 | 7 | describe("with empty suites", () => { |
@@ -54,3 +55,37 @@ describe("getSuiteLabels", () => { |
54 | 55 | }); |
55 | 56 | }); |
56 | 57 | }); |
| 58 | + |
| 59 | +describe("getPackageName", () => { |
| 60 | + it("should cache the package name on subsequent calls", () => { |
| 61 | + const first = getPackageName(); |
| 62 | + const second = getPackageName(); |
| 63 | + expect(first).toBe(second); |
| 64 | + expect(first).toBe("allure-js-commons"); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should continue searching parent directories if package.json has no name", () => { |
| 68 | + // If a package.json exists but has no "name" field (or is malformed), |
| 69 | + // the function logs an error and continues searching up the directory tree. |
| 70 | + // This test documents the expected behavior. |
| 71 | + // In practice, this test will find "allure-js-commons" since all package.json |
| 72 | + // files in this repo are well-formed. |
| 73 | + const packageName = getPackageName(); |
| 74 | + expect(packageName).toBeDefined(); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +describe("getRelativePath", () => { |
| 79 | + it("should prepend package name to relative path", () => { |
| 80 | + const filepath = path.join("test", "spec", "example.test.ts"); |
| 81 | + const result = getRelativePath(filepath); |
| 82 | + expect(result).toBe(path.join("allure-js-commons", "test", "spec", "example.test.ts")); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should handle absolute paths and prepend package name", () => { |
| 86 | + const absolutePath = path.join(process.cwd(), "test", "spec", "example.test.ts"); |
| 87 | + const result = getRelativePath(absolutePath); |
| 88 | + expect(result).toContain(`allure-js-commons${path.sep}`); |
| 89 | + expect(result).toContain(path.join("test", "spec", "example.test.ts")); |
| 90 | + }); |
| 91 | +}); |
0 commit comments