Skip to content

Commit

Permalink
fix vitest files resolving (via #845)
Browse files Browse the repository at this point in the history
  • Loading branch information
epszaw authored Jan 18, 2024
1 parent b07d594 commit 0882050
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 210 deletions.
6 changes: 3 additions & 3 deletions packages/allure-vitest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ yarn add -D vitest allure-vitest
Add instance of the reporter to the [`reporters` section](https://vitest.dev/config/#reporters) of your Vitest config:

```js
import AllureReporter from "allure-vitest";
import AllureReporter from "allure-vitest/reporter";
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
// add setup file to be able to use Allure API via `this.allure` in your tests
setupFiles: ["allure-vitest/setup],
setupFiles: ["allure-vitest/setup"],
reporters: [
// do not forget to keep the "default" if you want to see something in the console
"default",
Expand Down Expand Up @@ -75,7 +75,7 @@ to Allure Runtime API:
import { test } from "vitest";

test("sample test", async () => {
await this.allure.label("foo", "bar");
await allure.label("foo", "bar");
});
```

Expand Down
20 changes: 15 additions & 5 deletions packages/allure-vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,22 @@
},
"type": "module",
"exports": {
".": "./dist/src/index.js",
"./setup": "./dist/src/setup.js",
"./reporter": "./dist/src/reporter.js"
".": {
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./reporter": {
"import": "./dist/reporter.js",
"types": "./dist/reporter.d.ts"
},
"./setup": {
"require": "./dist/setup.js",
"import": "./dist/setup.js",
"types": "./dist/setup.d.ts"
}
},
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
Expand Down
144 changes: 53 additions & 91 deletions packages/allure-vitest/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint @typescript-eslint/require-await: 0 */
import * as vitest from "vitest";
import { LabelName, LinkType, MetadataMessage, ParameterOptions, Stage, Status, StepMetadata } from "allure-js-commons";

Expand Down Expand Up @@ -27,82 +28,87 @@ const injectAllureMeta = (context: vitest.TaskContext) => {
};
};

export const label = (context: vitest.TaskContext, name: string, value: string) => {
export const label = async (context: vitest.TaskContext, name: string, value: string) => {
injectAllureMeta(context);
(context.task.meta as AllureTestMeta).currentTest.labels.push({ name, value });
};

export const link = (context: vitest.TaskContext, type: LinkType, url: string, name?: string) => {
export const link = async (context: vitest.TaskContext, type: LinkType, url: string, name?: string) => {
injectAllureMeta(context);
(context.task.meta as AllureTestMeta).currentTest.links.push({ type, url, name });
};

export const epic = (context: vitest.TaskContext, value: string) => {
export const epic = async (context: vitest.TaskContext, value: string) => {
injectAllureMeta(context);
label(context, LabelName.EPIC, value);
};

export const feature = (context: vitest.TaskContext, value: string) => {
export const feature = async (context: vitest.TaskContext, value: string) => {
injectAllureMeta(context);
label(context, LabelName.FEATURE, value);
};

export const story = (context: vitest.TaskContext, value: string) => {
export const story = async (context: vitest.TaskContext, value: string) => {
injectAllureMeta(context);
label(context, LabelName.STORY, value);
};

export const suite = (context: vitest.TaskContext, value: string) => {
export const suite = async (context: vitest.TaskContext, value: string) => {
injectAllureMeta(context);
label(context, LabelName.SUITE, value);
};

export const parentSuite = (context: vitest.TaskContext, value: string) => {
export const parentSuite = async (context: vitest.TaskContext, value: string) => {
injectAllureMeta(context);
label(context, LabelName.PARENT_SUITE, value);
};

export const subSuite = (context: vitest.TaskContext, value: string) => {
export const subSuite = async (context: vitest.TaskContext, value: string) => {
injectAllureMeta(context);
label(context, LabelName.SUB_SUITE, value);
};

export const owner = (context: vitest.TaskContext, value: string) => {
export const owner = async (context: vitest.TaskContext, value: string) => {
injectAllureMeta(context);
label(context, LabelName.OWNER, value);
};

export const severity = (context: vitest.TaskContext, value: string) => {
export const severity = async (context: vitest.TaskContext, value: string) => {
injectAllureMeta(context);
label(context, LabelName.SEVERITY, value);
};

export const layer = (context: vitest.TaskContext, value: string) => {
export const layer = async (context: vitest.TaskContext, value: string) => {
injectAllureMeta(context);
label(context, LabelName.LAYER, value);
};

export const tag = (context: vitest.TaskContext, value: string) => {
export const tag = async (context: vitest.TaskContext, value: string) => {
injectAllureMeta(context);
label(context, LabelName.TAG, value);
};

export const allureId = (context: vitest.TaskContext, value: string) => {
export const allureId = async (context: vitest.TaskContext, value: string) => {
injectAllureMeta(context);
label(context, LabelName.ALLURE_ID, value);
};

export const issue = (context: vitest.TaskContext, name: string, url: string) => {
export const issue = async (context: vitest.TaskContext, name: string, url: string) => {
injectAllureMeta(context);
link(context, LinkType.ISSUE, url, name);
};

export const tms = (context: vitest.TaskContext, name: string, url: string) => {
export const tms = async (context: vitest.TaskContext, name: string, url: string) => {
injectAllureMeta(context);
link(context, LinkType.TMS, url, name);
};

export const parameter = (context: vitest.TaskContext, name: string, value: string, options?: ParameterOptions) => {
export const parameter = async (
context: vitest.TaskContext,
name: string,
value: string,
options?: ParameterOptions,
) => {
injectAllureMeta(context);
(context.task.meta as AllureTestMeta).currentTest.parameter.push({
name,
Expand All @@ -111,32 +117,32 @@ export const parameter = (context: vitest.TaskContext, name: string, value: stri
});
};

export const description = (context: vitest.TaskContext, markdown: string) => {
export const description = async (context: vitest.TaskContext, markdown: string) => {
injectAllureMeta(context);
(context.task.meta as AllureTestMeta).currentTest.description = markdown;
};

export const descriptionHtml = (context: vitest.TaskContext, html: string) => {
export const descriptionHtml = async (context: vitest.TaskContext, html: string) => {
injectAllureMeta(context);
(context.task.meta as AllureTestMeta).currentTest.descriptionHtml = html;
};

export const displayName = (context: vitest.TaskContext, name: string) => {
export const displayName = async (context: vitest.TaskContext, name: string) => {
injectAllureMeta(context);
(context.task.meta as AllureTestMeta).currentTest.displayName = name;
};

export const historyId = (context: vitest.TaskContext, value: string) => {
export const historyId = async (context: vitest.TaskContext, value: string) => {
injectAllureMeta(context);
(context.task.meta as AllureTestMeta).currentTest.historyId = value;
};

export const testCaseId = (context: vitest.TaskContext, value: string) => {
export const testCaseId = async (context: vitest.TaskContext, value: string) => {
injectAllureMeta(context);
(context.task.meta as AllureTestMeta).currentTest.testCaseId = value;
};

export const attachment = (context: vitest.TaskContext, name: string, content: Buffer | string, type: string) => {
export const attachment = async (context: vitest.TaskContext, name: string, content: Buffer | string, type: string) => {
injectAllureMeta(context);

const { currentTest, currentStep } = context.task.meta as AllureTestMeta;
Expand Down Expand Up @@ -185,74 +191,30 @@ export const step = async (context: vitest.TaskContext, name: string, body: () =

export const bindAllureApi = (task: vitest.Task) => {
return {
label: (name: string, value: string) => {
label({ task } as vitest.TaskContext, name, value);
},
link: (type: LinkType, url: string, name?: string) => {
link({ task } as vitest.TaskContext, type, url, name);
},
parameter: (name: string, value: string, options?: ParameterOptions) => {
parameter({ task } as vitest.TaskContext, name, value, options);
},
description: (markdown: string) => {
description({ task } as vitest.TaskContext, markdown);
},
descriptionHtml: (html: string) => {
descriptionHtml({ task } as vitest.TaskContext, html);
},
testCaseId: (id: string) => {
testCaseId({ task } as vitest.TaskContext, id);
},
historyId: (id: string) => {
historyId({ task } as vitest.TaskContext, id);
},
allureId: (id: string) => {
allureId({ task } as vitest.TaskContext, id);
},
displayName: (name: string) => {
displayName({ task } as vitest.TaskContext, name);
},
attachment: (name: string, content: Buffer | string, type: string) => {
attachment({ task } as vitest.TaskContext, name, content, type);
},
issue: (name: string, url: string) => {
issue({ task } as vitest.TaskContext, name, url);
},
tms: (name: string, url: string) => {
tms({ task } as vitest.TaskContext, name, url);
},
epic: (name: string) => {
epic({ task } as vitest.TaskContext, name);
},
feature: (name: string) => {
feature({ task } as vitest.TaskContext, name);
},
story: (name: string) => {
story({ task } as vitest.TaskContext, name);
},
suite: (name: string) => {
suite({ task } as vitest.TaskContext, name);
},
parentSuite: (name: string) => {
parentSuite({ task } as vitest.TaskContext, name);
},
subSuite: (name: string) => {
subSuite({ task } as vitest.TaskContext, name);
},
owner: (name: string) => {
owner({ task } as vitest.TaskContext, name);
},
severity: (name: string) => {
severity({ task } as vitest.TaskContext, name);
},
layer: (name: string) => {
layer({ task } as vitest.TaskContext, name);
},
tag: (name: string) => {
tag({ task } as vitest.TaskContext, name);
},
step: (name: string, body: () => Promise<void>) => {
step({ task } as vitest.TaskContext, name, body);
},
label: (name: string, value: string) => label({ task } as vitest.TaskContext, name, value),
link: (type: LinkType, url: string, name?: string) => link({ task } as vitest.TaskContext, type, url, name),
parameter: (name: string, value: string, options?: ParameterOptions) =>
parameter({ task } as vitest.TaskContext, name, value, options),
description: (markdown: string) => description({ task } as vitest.TaskContext, markdown),
descriptionHtml: (html: string) => descriptionHtml({ task } as vitest.TaskContext, html),
testCaseId: (id: string) => testCaseId({ task } as vitest.TaskContext, id),
historyId: (id: string) => historyId({ task } as vitest.TaskContext, id),
allureId: (id: string) => allureId({ task } as vitest.TaskContext, id),
displayName: (name: string) => displayName({ task } as vitest.TaskContext, name),
attachment: (name: string, content: Buffer | string, type: string) =>
attachment({ task } as vitest.TaskContext, name, content, type),
issue: (name: string, url: string) => issue({ task } as vitest.TaskContext, name, url),
tms: (name: string, url: string) => tms({ task } as vitest.TaskContext, name, url),
epic: (name: string) => epic({ task } as vitest.TaskContext, name),
feature: (name: string) => feature({ task } as vitest.TaskContext, name),
story: (name: string) => story({ task } as vitest.TaskContext, name),
suite: (name: string) => suite({ task } as vitest.TaskContext, name),
parentSuite: (name: string) => parentSuite({ task } as vitest.TaskContext, name),
subSuite: (name: string) => subSuite({ task } as vitest.TaskContext, name),
owner: (name: string) => owner({ task } as vitest.TaskContext, name),
severity: (name: string) => severity({ task } as vitest.TaskContext, name),
layer: (name: string) => layer({ task } as vitest.TaskContext, name),
tag: (name: string) => tag({ task } as vitest.TaskContext, name),
step: (name: string, body: () => Promise<void>) => step({ task } as vitest.TaskContext, name, body),
};
};
8 changes: 2 additions & 6 deletions packages/allure-vitest/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,9 @@ beforeEach((ctx) => {
attachments: [],
};

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.allure = bindAllureApi(ctx.task);
global.allure = bindAllureApi(ctx.task);
});

afterEach(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.allure = undefined;
global.allure = undefined;
});
4 changes: 2 additions & 2 deletions packages/allure-vitest/test/spec/setup/attachments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ it("adds attachments", async () => {
const { tests, attachments } = await runVitestInlineTest(`
import { test } from "vitest";
test("text attachment", () => {
this.allure.attachment("foo.txt", Buffer.from("bar"), "text/plain");
test("text attachment", async () => {
await allure.attachment("foo.txt", Buffer.from("bar"), "text/plain");
});
`);

Expand Down
8 changes: 4 additions & 4 deletions packages/allure-vitest/test/spec/setup/description.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ it("sets description", async () => {
const { tests } = await runVitestInlineTest(`
import { test } from "vitest";
test("description", () => {
this.allure.description("foo");
test("description", async () => {
await allure.description("foo");
});
`);

Expand All @@ -18,8 +18,8 @@ it("sets html description", async () => {
const { tests } = await runVitestInlineTest(`
import { test } from "vitest";
test("description html", () => {
this.allure.descriptionHtml("foo");
test("description html", async () => {
await allure.descriptionHtml("foo");
});
`);

Expand Down
4 changes: 2 additions & 2 deletions packages/allure-vitest/test/spec/setup/displayName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ it("sets test display name", async () => {
const { tests } = await runVitestInlineTest(`
import { test } from "vitest";
test("display name", () => {
this.allure.displayName("foo");
test("display name", async () => {
await allure.displayName("foo");
});
`);

Expand Down
4 changes: 2 additions & 2 deletions packages/allure-vitest/test/spec/setup/historyId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ it("sets test history id", async () => {
const { tests } = await runVitestInlineTest(`
import { test } from "vitest";
test("history id", () => {
this.allure.historyId("foo");
test("history id", async () => {
await allure.historyId("foo");
});
`);

Expand Down
Loading

0 comments on commit 0882050

Please sign in to comment.