Skip to content

Commit

Permalink
test: fix reader tests
Browse files Browse the repository at this point in the history
  • Loading branch information
delatrie committed Feb 14, 2025
1 parent 08355b7 commit 60d59a6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
8 changes: 4 additions & 4 deletions packages/reader/test/allure1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ describe("allure1 reader", () => {
it("should parse empty xml file", async () => {
const visitor = mockVisitor();
const resultFile = await readResourceAsResultFile("allure1data/empty-file.xml", randomTestsuiteFileName());
const read = await allure1.read(visitor, resultFile);
const read = await allure1.readFile(visitor, resultFile);

expect(read).toBeFalsy();
});

it("should parse empty xml correct xml heading", async () => {
const visitor = mockVisitor();
const resultFile = await readResourceAsResultFile("allure1data/empty-xml.xml", randomTestsuiteFileName());
const read = await allure1.read(visitor, resultFile);
const read = await allure1.readFile(visitor, resultFile);

expect(read).toBeFalsy();
});

it("should parse empty root element", async () => {
const visitor = mockVisitor();
const resultFile = await readResourceAsResultFile("allure1data/empty-root.xml", randomTestsuiteFileName());
const read = await allure1.read(visitor, resultFile);
const read = await allure1.readFile(visitor, resultFile);

expect(read).toBeFalsy();
});

it("should parse test-suites element with invalid type", async () => {
const visitor = mockVisitor();
const resultFile = await readResourceAsResultFile("allure1data/invalid-root.xml", randomTestsuiteFileName());
const read = await allure1.read(visitor, resultFile);
const read = await allure1.readFile(visitor, resultFile);

expect(read).toBeFalsy();
});
Expand Down
6 changes: 3 additions & 3 deletions packages/reader/test/junitxml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,23 +640,23 @@ describe("junit xml reader", () => {
it("should ignore invalid root element", async () => {
const visitor = mockVisitor();
const resultFile = await readResourceAsResultFile("junitxmldata/invalid.xml", randomTestsuiteFileName());
const read = await junitXml.read(visitor, resultFile);
const read = await junitXml.readFile(visitor, resultFile);

expect(read).toBeFalsy();
});

it("should parse empty root element", async () => {
const visitor = mockVisitor();
const resultFile = await readResourceAsResultFile("junitxmldata/empty.xml", randomTestsuiteFileName());
const read = await junitXml.read(visitor, resultFile);
const read = await junitXml.readFile(visitor, resultFile);

expect(read).toBeTruthy();
});

it("should parse test-suites element with invalid type", async () => {
const visitor = mockVisitor();
const resultFile = await readResourceAsResultFile("junitxmldata/wrong-type.xml", randomTestsuiteFileName());
const read = await junitXml.read(visitor, resultFile);
const read = await junitXml.readFile(visitor, resultFile);

expect(read).toBeFalsy();
});
Expand Down
40 changes: 35 additions & 5 deletions packages/reader/test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { ResultFile } from "@allurereport/plugin-api";
import { PathResultFile, ResultsReader, ResultsVisitor } from "@allurereport/reader-api";
import type { ResultFile } from "@allurereport/plugin-api";
import type { DirectoryResultsReader, FileResultsReader, ResultsVisitor } from "@allurereport/reader-api";
import { PathResultFile } from "@allurereport/reader-api";
import { attachment, step } from "allure-js-commons";
import { existsSync } from "fs";
import { readFile } from "node:fs/promises";
import { basename, resolve } from "node:path";
import { Mocked, expect, vi } from "vitest";
import type { Mocked } from "vitest";
import { expect, vi } from "vitest";

export const buildResourcePath = (path: string) => resolve(__dirname, "./resources", path);

Expand All @@ -14,6 +16,7 @@ export const readResourceAsJson = async <T>(path: string) => {

return JSON.parse(resourceContent) as T;
};

export const readResourceAsResultFile = async (path: string, filename?: string) => {
const resourcePath = buildResourcePath(path);

Expand All @@ -24,6 +27,16 @@ export const readResourceAsResultFile = async (path: string, filename?: string)
return new PathResultFile(resourcePath, filename || basename(path));
};

export const resolveResourceDirectory = async (path: string) => {
const resourcePath = buildResourcePath(path);

if (!existsSync(resourcePath)) {
throw new Error(`Resource ${resourcePath} not found`);
}

return path;
};

export const mockVisitor: () => Mocked<ResultsVisitor> = () => ({
visitTestResult: vi.fn<ResultsVisitor["visitTestResult"]>(),
visitAttachmentFile: vi.fn<ResultsVisitor["visitAttachmentFile"]>(),
Expand All @@ -32,7 +45,7 @@ export const mockVisitor: () => Mocked<ResultsVisitor> = () => ({
});

export const readResults = async (
reader: ResultsReader,
reader: FileResultsReader,
files: Record<string, string> = {},
result: boolean = true,
) => {
Expand All @@ -41,7 +54,20 @@ export const readResults = async (
for (const filesKey in files) {
const resultFile = await readResourceAsResultFile(filesKey, files[filesKey]);
await attachResultFile(resultFile);
const read = await reader.read(visitor, resultFile);
const read = await reader.readFile(visitor, resultFile);
expect(read).toBe(result);
}
return visitor;
});
};

export const readDirectoryResults = async (reader: DirectoryResultsReader, paths: string, result: boolean = true) => {
return step("readResults", async () => {
const visitor = mockVisitor();
for (const path of paths) {
const resultDir = await resolveResourceDirectory(path);
await attachResultDir(resultDir);
const read = await reader.readDirectory(visitor, resultDir);
expect(read).toBe(result);
}
return visitor;
Expand All @@ -54,3 +80,7 @@ export const attachResultFile = async (resultFile: ResultFile) => {
await attachment(resultFile.getOriginalFileName(), content, resultFile.getContentType() ?? "text/plain");
}
};

export const attachResultDir = async (path: string) => {
await attachment(basename(path), "", "application/zip");
};

0 comments on commit 60d59a6

Please sign in to comment.