allure-js icon indicating copy to clipboard operation
allure-js copied to clipboard

[Vitest] add Reporter

Open vovsemenv opened this issue 3 years ago • 7 comments

I think we need reporter for https://github.com/vitest-dev/vitest

(Paused till this issue open https://github.com/vitest-dev/vitest/issues/616 )

vovsemenv avatar Jan 24 '22 08:01 vovsemenv

@vovsemenv looks like https://github.com/vitest-dev/vitest/issues/616 merged on Feb 1st?

cope avatar Mar 10 '22 09:03 cope

Actually vitest plugin system don't ready. Right now I can easily implement only basic results reporting.

vovsemenv avatar Mar 10 '22 19:03 vovsemenv

Hello

I was wondering if there were still plans on creating a reporter for vitest. My team recently switched from jest to vitest and would be keen to see an allure reporter for vitest.

Thanks

markgerra avatar Aug 25 '22 11:08 markgerra

Hello @markgerra, allure-js is Open-Source, so PR is welcome. We have plans to support it, but without ETA. If your company urgently needs this feature and can sponsor it, then just email us allure at qameta.io.

vovsemenv avatar Aug 26 '22 15:08 vovsemenv

@markgerra Creating fully-features reporter for vitest is really tricky without decent plugin system. So i created simple reporter just to report failed/done tests and their suites for now and you can generate basic reports. Once vitest team provide way to send metadata i will pack it to production ready package.

"reporter.ts"

import {
  AllureConfig,
    AllureGroup,
    AllureRuntime,
    LabelName,
    Status
} from "allure-js-commons";

import { File, Reporter, Task, Vitest } from "vitest";

export class AllureReporter implements Reporter {
  private vitestInstance: Vitest | null;
  private allureRuntime: AllureRuntime | null;

  constructor(config?: Partial<AllureConfig>){
    this.allureRuntime = new AllureRuntime({
      resultsDir: "allure-results",
      ...config,
    });
  }
  onInit(ctx: Vitest) {
    this.vitestInstance = ctx;
  }

  onFinished(files?: File[]) {
    if (!this.allureRuntime) {
      return;
    }

    const rootSuite = this.allureRuntime.startGroup(undefined);
    for (const file of files || []) {
      const group = rootSuite.startGroup(file.name);
      group.name = file.name;
      for (const task of file.tasks) {
        this.handleTask(group, task);
      }
      group.endGroup();
    }
    rootSuite.endGroup();
  }

  handleTask(parent: AllureGroup, task: Task) {
    if (task.type === "suite") {
      const group = parent.startGroup(task.name);
      group.name = task.name;
      for (const innerTask of task.tasks) {
        this.handleTask(group, innerTask);
      }
      group.endGroup();
    } else {
      const test = parent.startTest(task.name, 0);
      test.name = task.name;
      test.fullName = `${task.file?.name}#${task.name}`;
      switch (task.result?.state) {
        case "fail": {
          test.detailsMessage = task.result.error?.message;
          test.detailsTrace = task.result.error?.stackStr;
          test.status = Status.FAILED;
          break;
        }
        case "pass": {
          test.status = Status.PASSED;
          break;
        }
        case "skip": {
          test.status = Status.SKIPPED;
          break;
        }
      }
      test.addLabel(LabelName.SUITE, parent.name);
      if (task.suite.suite?.name) {
        test.addLabel(LabelName.PARENT_SUITE, task.suite.suite.name);
      }
      test.historyId = task.id;
      test.endTest(task.result?.duration);
    }
  }
}

"vite.config.ts"

import { defineConfig } from "vite";
import { AllureReporter } from "./reporter";

export default defineConfig({
  test: {
    reporters: [new AllureReporter({}), "default"],
    watch: false,
  },
});

vovsemenv avatar Sep 01 '22 19:09 vovsemenv

https://github.com/vitest-dev/vitest/issues/1951

vovsemenv avatar Sep 01 '22 19:09 vovsemenv

@vovsemenv thank you so much for this!

markgerra avatar Sep 22 '22 08:09 markgerra

Happy new year! It seems like upstream issues were resolved and building a reporter is possible now.

I build a poc version here: https://github.com/just-boris/vitest-allure

Let me know if you are interested merging this into the core

just-boris avatar Jan 05 '24 22:01 just-boris

@just-boris thank you! Could you please open PR to the repository to merge the reporter?

epszaw avatar Jan 08 '24 12:01 epszaw

sure, I can, but it may take some time as I am busy with other tasks. If somebody wants to take it over, please do this

just-boris avatar Jan 08 '24 14:01 just-boris

Will transfer the repo by myself 👍

epszaw avatar Jan 11 '24 11:01 epszaw