allure-vitest
Version:
Allure Vitest integration
115 lines • 4.47 kB
JavaScript
import { getCurrentSuite, getCurrentTest as getCurrentTestGlobal, } from "@vitest/runner";
import { isGlobalRuntimeMessage } from "allure-js-commons/sdk";
import { BaseMessageTestRuntime } from "allure-js-commons/sdk/runtime";
let getCurrentTest = getCurrentTestGlobal;
export const setGetCurrentTest = (fn) => {
getCurrentTest = fn;
};
export const ALLURE_VITEST_GLOBAL_RUNTIME_MESSAGES_KEY = "__allureVitestGlobalRuntimeMessages";
export const ALLURE_VITEST_GLOBAL_RUNTIME_MESSAGES_META_KEY = "allureGlobalRuntimeMessages";
export const ALLURE_VITEST_RUNTIME_MESSAGES_META_KEY = "allureRuntimeMessages";
export const getCurrentTask = () => getCurrentTest();
export const addGlobalMessage = (message) => {
const holder = globalThis;
const messages = (holder[ALLURE_VITEST_GLOBAL_RUNTIME_MESSAGES_KEY] ?? (holder[ALLURE_VITEST_GLOBAL_RUNTIME_MESSAGES_KEY] = []));
messages.push(message);
};
export const addMessageToMeta = (meta, key, message) => {
const typedMeta = meta;
const messages = (typedMeta[key] ?? (typedMeta[key] = []));
messages.push(message);
};
export const takeGlobalRuntimeMessages = () => {
const holder = globalThis;
const result = [...(holder[ALLURE_VITEST_GLOBAL_RUNTIME_MESSAGES_KEY] ?? [])];
holder[ALLURE_VITEST_GLOBAL_RUNTIME_MESSAGES_KEY] = [];
return result;
};
export const processTask = (task, message, isGlobal = false) => {
switch (task.type) {
case "collector":
case "suite":
task.tasks.forEach((sub) => processTask(sub, message, isGlobal));
break;
case "test":
if (isGlobal) {
addMessageToMeta(task.meta, ALLURE_VITEST_GLOBAL_RUNTIME_MESSAGES_META_KEY, message);
}
else {
addMessage(task.meta, message);
}
break;
default:
break;
}
};
// beforeAll is suite-scoped in Vitest (no current test context):
// https://main.vitest.dev/api/hooks#beforeall
// Bind a global message to the first test task to avoid duplicating it across every
// test in the suite when suite-level globals are later collected.
export const attachGlobalMessageToFirstTest = (task, message) => {
switch (task.type) {
case "collector":
case "suite":
return task.tasks.some((sub) => attachGlobalMessageToFirstTest(sub, message));
case "test":
addMessageToMeta(task.meta, ALLURE_VITEST_GLOBAL_RUNTIME_MESSAGES_META_KEY, message);
return true;
default:
return false;
}
};
export const addMessage = (meta, message) => {
addMessageToMeta(meta, ALLURE_VITEST_RUNTIME_MESSAGES_META_KEY, message);
};
const logMissingVitestContext = () => {
// eslint-disable-next-line no-console
console.error("no vitest context is detected. Please ensure you're using allure API within vitest test (it, test) " +
"or setup (beforeAll, beforeEach, afterAll, afterEach) function. Make sure vitest@1.6.0 or above is used");
};
export class BaseVitestTestRuntime extends BaseMessageTestRuntime {
sendMessageSync(message) {
if (!this.processMessage(message)) {
logMissingVitestContext();
}
}
sendMessage(message) {
this.sendMessageSync(message);
return Promise.resolve();
}
processMessage(message) {
const currentTest = getCurrentTest();
if (isGlobalRuntimeMessage(message)) {
if (currentTest) {
addMessageToMeta(currentTest.meta, ALLURE_VITEST_GLOBAL_RUNTIME_MESSAGES_META_KEY, message);
return true;
}
try {
const currentSuite = getCurrentSuite();
if (currentSuite) {
const hasTargetTest = attachGlobalMessageToFirstTest(currentSuite, message);
if (hasTargetTest) {
return true;
}
}
}
catch { }
addGlobalMessage(message);
return true;
}
if (currentTest) {
addMessage(currentTest.meta, message);
return true;
}
try {
const currentSuite = getCurrentSuite();
if (currentSuite) {
processTask(currentSuite, message);
return true;
}
}
catch { }
return false;
}
}
//# sourceMappingURL=runtime.js.map