@competent-devs/test-forge
Version:
Package for UI unit test generation based on storybook context
52 lines • 2.39 kB
JavaScript
import * as path from "node:path";
import * as fs from "node:fs/promises";
import { memory, tokenTracker } from "./agent.js";
import { runVitest } from "./run-test.js";
import { logToFile, resetLogFile } from "./logger.js";
import { readCsfFile, readTestFile, writeTestFile } from "./utils.js";
import { generateTests, regenerateTestsWithErrors } from "./generate-tests.js";
const MAX_ATTEMPTS = 3;
export const processFile = async (storyPath, filePath, resetLog = true) => {
await memory.clear();
tokenTracker.clearUsage();
if (!!resetLog) {
resetLogFile();
}
console.group(`===== ${storyPath} =====`);
logToFile(filePath);
const parsedpath = path.parse(storyPath);
const csffile = await readCsfFile(storyPath);
const fileContent = await fs.readFile(filePath, "utf-8");
const testcode = await generateTests(storyPath, csffile, fileContent);
const testfilename = path.resolve(path.dirname(storyPath), `${parsedpath.name}.test.tsx`);
await writeTestFile(testfilename, testcode);
logToFile("First generation");
logToFile(testcode);
const fileName = testfilename.split("\\/").pop() || "";
let testResult = await runVitest(fileName);
let attempt = 1;
while (testResult.hasErrors && attempt <= MAX_ATTEMPTS) {
console.log(`Test failed on attempt ${attempt}. Regenerating based on error information...`);
const errorDetails = testResult.errors;
logToFile(`Test Result. Attempt ${attempt}`);
logToFile(JSON.stringify(testResult));
logToFile(`Error Details. Attempt ${attempt}`);
logToFile(JSON.stringify(errorDetails));
const currentTestCode = await readTestFile(testfilename);
const regeneratedTestCode = await regenerateTestsWithErrors(currentTestCode, errorDetails);
logToFile(`${attempt} generation`);
logToFile(regeneratedTestCode);
await writeTestFile(testfilename, regeneratedTestCode);
testResult = await runVitest(fileName);
attempt++;
if (!testResult.hasErrors) {
console.log("Test passed after regeneration!");
break;
}
}
if (testResult.hasErrors) {
console.error(`Test failed after ${MAX_ATTEMPTS} regeneration attempts. Please check the error log for more information.`);
}
console.groupEnd();
};
//# sourceMappingURL=test-forge.js.map