Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Vitest] add Reporter #415

Closed
vovsemenv opened this issue Jan 24, 2022 · 11 comments · Fixed by #830
Closed

[Vitest] add Reporter #415

vovsemenv opened this issue Jan 24, 2022 · 11 comments · Fixed by #830

Comments

@vovsemenv
Copy link
Collaborator

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

(Paused till this issue open vitest-dev/vitest#616 )

@cope
Copy link

cope commented Mar 10, 2022

@vovsemenv looks like vitest-dev/vitest#616 merged on Feb 1st?

@vovsemenv
Copy link
Collaborator Author

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

@markgerra
Copy link

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

@vovsemenv
Copy link
Collaborator Author

vovsemenv commented Aug 26, 2022

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
Copy link
Collaborator Author

@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
Copy link
Collaborator Author

vitest-dev/vitest#1951

@markgerra
Copy link

@vovsemenv thank you so much for this!

@just-boris
Copy link
Contributor

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

@epszaw
Copy link
Member

epszaw commented Jan 8, 2024

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

@just-boris
Copy link
Contributor

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

@epszaw
Copy link
Member

epszaw commented Jan 11, 2024

Will transfer the repo by myself 👍

@epszaw epszaw mentioned this issue Jan 11, 2024
2 tasks
@baev baev closed this as completed in #830 Jan 17, 2024
baev pushed a commit that referenced this issue Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants