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

Fix jest todo tests problem and add package label #810

Merged
merged 3 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions packages/allure-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@
"@jest/core": "^29.6.2",
"@jest/environment": "^29.6.4",
"@jest/types": "^29.6.1",
"@types/chai": "^4.3.6",
"@types/chai-like": "^1.1.1",
"@types/chai-things": "^0.0.36",
"@types/eslint": "^8",
"@types/glob": "^8.1.0",
"@types/jasmine": "^3.3.12",
Expand All @@ -61,16 +58,14 @@
"@types/source-map-support": "^0.5.7",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"chai": "^4.3.8",
"chai-like": "^1.1.1",
"chai-things": "^0.2.0",
"codecov": "^3.8.3",
"eslint": "^8.49.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jsdoc": "^46.6.0",
"eslint-plugin-no-null": "^1.0.2",
"eslint-plugin-prefer-arrow": "^1.2.3",
"expect": "^29.7.0",
"glob": "^10.3.5",
"jest-cli": "^29.6.2",
"mocha": "^10.2.0",
Expand Down
69 changes: 55 additions & 14 deletions packages/allure-jest/src/AllureJest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os from "os";
import process from "process";
import os from "node:os";
import { dirname, sep } from "node:path";
import process from "node:process";
import { EnvironmentContext, JestEnvironment, JestEnvironmentConfig } from "@jest/environment";
import type { Circus } from "@jest/types";
import {
Expand All @@ -21,6 +22,7 @@ const hostname = os.hostname();

export interface AllureEnvironment extends JestEnvironment {
transformLinks(links: Link[]): Link[];

handleAllureMetadata(payload: { currentTestName: string; metadata: MetadataMessage }): void;
}

Expand All @@ -32,7 +34,8 @@ export interface LinkMatcher {
const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T => {
// @ts-expect-error (ts(2545)) Incorrect assumption about a mixin class: https://github.com/microsoft/TypeScript/issues/37142
return class extends Base {
testRootDirPath: string;
// testRootDirPath: string;
testPath: string;
runtime: AllureRuntime;
linksMatchers: LinkMatcher[];
runningTests: Map<string, AllureTest> = new Map();
Expand All @@ -48,7 +51,7 @@ const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T =>
});
this.linksMatchers = links as LinkMatcher[];
this.global.allure = new AllureJestApi(this, this.global);
this.testRootDirPath = config.globalConfig.rootDir;
this.testPath = context.testPath.replace(config.globalConfig.rootDir, "").replace(sep, "");
}

setup() {
Expand Down Expand Up @@ -119,35 +122,36 @@ const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T =>
const newTestPath = newTestSuitesPath.concat(testName);
const newTestId = getTestId(newTestPath);
const newTest = new AllureTest(this.runtime);
const thread = ALLURE_THREAD_NAME || JEST_WORKER_ID || process.pid.toString();
const host = ALLURE_HOST_NAME || hostname;
const threadLabel = ALLURE_THREAD_NAME || JEST_WORKER_ID || process.pid.toString();
const hostLabel = ALLURE_HOST_NAME || hostname;
const packageLabel = dirname(this.testPath).split(sep).join(".");

newTest.name = testName;
newTest.fullName = newTestId;

newTest.addLabel(LabelName.LANGUAGE, "javascript");
newTest.addLabel(LabelName.FRAMEWORK, "jest");
newTest.addLabel(LabelName.PACKAGE, packageLabel);

if (thread) {
newTest.addLabel(LabelName.THREAD, thread);
if (threadLabel) {
newTest.addLabel(LabelName.THREAD, threadLabel);
}

if (host) {
newTest.addLabel(LabelName.HOST, host);
if (hostLabel) {
newTest.addLabel(LabelName.HOST, hostLabel);
}

getSuitesLabels(newTestSuitesPath).forEach((label) => {
newTest.addLabel(label.name, label.value);
});

/**
* if user have some tests with the same name, reporter will throw an
* unexpected error due the test with the same name could be removed from
* the running tests, so better to throw an explicit error
* If user have some tests with the same name, reporter will throw an error due the test with
* the same name could be removed from the running tests, so better to throw an explicit error
*/
if (this.runningTests.has(newTestId)) {
throw new Error(
`Test "${newTestId}" has been already added to run! To continue with reporting, please rename the test.`,
`Test "${newTestId}" has been already initialized! To continue with reporting, please rename the test.`,
);
}

Expand All @@ -158,20 +162,39 @@ const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T =>
const currentTestId = getTestId(getTestPath(test));
const currentTest = this.runningTests.get(currentTestId)!;

if (!currentTest) {
// eslint-disable-next-line no-console
console.error(`Can't find "${currentTestId}" test while tried to start it!`);
return;
}

currentTest.stage = Stage.RUNNING;
}

private handleTestPass(test: Circus.TestEntry) {
const currentTestId = getTestId(getTestPath(test));
const currentTest = this.runningTests.get(currentTestId)!;

if (!currentTest) {
// eslint-disable-next-line no-console
console.error(`Can't find "${currentTestId}" test while tried to mark it as passed!`);
return;
}

currentTest.stage = Stage.FINISHED;
currentTest.status = Status.PASSED;
}

private handleTestFail(test: Circus.TestEntry) {
const currentTestId = getTestId(getTestPath(test));
const currentTest = this.runningTests.get(currentTestId)!;

if (!currentTest) {
// eslint-disable-next-line no-console
console.error(`Can't find "${currentTestId}" test while tried to mark it as failed!`);
return;
}

// jest collects all errors, but we need to report the first one because it's a reason why the test has been failed
const [error] = test.errors;
const hasMultipleErrors = Array.isArray(error);
Expand All @@ -190,6 +213,12 @@ const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T =>
const currentTestId = getTestId(getTestPath(test));
const currentTest = this.runningTests.get(currentTestId)!;

if (!currentTest) {
// eslint-disable-next-line no-console
console.error(`Can't find "${currentTestId}" test while tried to mark it as skipped!`);
return;
}

currentTest.stage = Stage.PENDING;
currentTest.status = Status.SKIPPED;

Expand All @@ -201,6 +230,12 @@ const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T =>
const currentTestId = getTestId(getTestPath(test));
const currentTest = this.runningTests.get(currentTestId)!;

if (!currentTest) {
// eslint-disable-next-line no-console
console.error(`Can't find "${currentTestId}" test while tried to dispose it after start!`);
return;
}

currentTest.endTest();
this.runningTests.delete(currentTestId);
}
Expand All @@ -209,6 +244,12 @@ const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T =>
const currentTestId = getTestId(getTestPath(test));
const currentTest = this.runningTests.get(currentTestId)!;

if (!currentTest) {
// eslint-disable-next-line no-console
console.error(`Can't find "${currentTestId}" test while tried to mark it as todo!`);
return;
}

currentTest.stage = Stage.PENDING;
currentTest.status = Status.SKIPPED;

Expand Down
2 changes: 2 additions & 0 deletions packages/allure-jest/test/fixtures/labels.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
it("package", () => {});

it("custom", () => {
allure.label("foo", "bar");
});
Expand Down
7 changes: 0 additions & 7 deletions packages/allure-jest/test/runner.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import path from "path";
import chai from "chai";
import chaiLike from "chai-like";
import chaiThings from "chai-things";
import * as glob from "glob";
import Mocha from "mocha";
import "source-map-support/register";

chai.should();
chai.use(chaiLike);
chai.use(chaiThings);

const mocha = new Mocha({
timeout: 30000,
// eslint-disable-next-line @typescript-eslint/no-require-imports
Expand Down
8 changes: 4 additions & 4 deletions packages/allure-jest/test/spec/ansiColors.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { expect } from "chai";
import expect from "expect";
import { runJestTests } from "../utils";

describe("ansiColors", () => {
it("removes ansi colors from text errors", async () => {
const results = await runJestTests(["./test/fixtures/ansiColors.test.js"]);
const [result] = Object.values(results);

expect(result.name).eq("hello");
expect(result.statusDetails.message).not.to.contain("\x1b[");
expect(result.statusDetails.trace).not.to.contain("\x1b[");
expect(result.name).toBe("hello");
expect(result.statusDetails.message).not.toContain("\x1b[");
expect(result.statusDetails.trace).not.toContain("\x1b[");
});
});
11 changes: 7 additions & 4 deletions packages/allure-jest/test/spec/attachments.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import expect from "expect";
import { runJestTests, TestResultsByFullName } from "../utils";

describe("attachments", () => {
Expand All @@ -10,9 +11,11 @@ describe("attachments", () => {
it("adds markdown description", () => {
const { attachments } = results.json;

attachments.should.include.something.like({
name: "Attachment",
type: "application/json",
});
expect(attachments).toContainEqual(
expect.objectContaining({
name: "Attachment",
type: "application/json",
}),
);
});
});
41 changes: 23 additions & 18 deletions packages/allure-jest/test/spec/dataTable.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import { Stage, Status } from "allure-js-commons";
import { expect } from "chai";
import expect from "expect";
import { runJestTests, TestResultsByFullName } from "../utils";

describe("each", () => {
it("handles all the 3 tests with data tables", async () => {
const results = await runJestTests(["./test/fixtures/dataTable.test.js"]);
const values = Object.values(results);

expect(values).length(3);

values.should.include.something.like({
name: "1 + 2 = 3",
status: Status.PASSED,
stage: Stage.FINISHED,
});
values.should.include.something.like({
name: "2 + 3 = 5",
status: Status.PASSED,
stage: Stage.FINISHED,
});
values.should.include.something.like({
name: "3 + 4 = 7",
status: Status.PASSED,
stage: Stage.FINISHED,
});
expect(values).toHaveLength(3);
expect(values).toContainEqual(
expect.objectContaining({
name: "1 + 2 = 3",
status: Status.PASSED,
stage: Stage.FINISHED,
}),
);
expect(values).toContainEqual(
expect.objectContaining({
name: "2 + 3 = 5",
status: Status.PASSED,
stage: Stage.FINISHED,
}),
);
expect(values).toContainEqual(
expect.objectContaining({
name: "3 + 4 = 7",
status: Status.PASSED,
stage: Stage.FINISHED,
}),
);
});
});
6 changes: 3 additions & 3 deletions packages/allure-jest/test/spec/description.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect } from "chai";
import expect from "expect";
import { runJestTests, TestResultsByFullName } from "../utils";

describe("description", () => {
Expand All @@ -11,12 +11,12 @@ describe("description", () => {
it("adds markdown description", () => {
const { description } = results.markdown;

expect(description).eq("foo");
expect(description).toBe("foo");
});

it("adds custom history id", () => {
const { descriptionHtml } = results.html;

expect(descriptionHtml).eq("foo");
expect(descriptionHtml).toBe("foo");
});
});
14 changes: 4 additions & 10 deletions packages/allure-jest/test/spec/duplicatedTests.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { expect } from "chai";
import expect from "expect";
import { runJestTests } from "../utils";

describe("tests with the same name", () => {
it("throws an user-friendly error", async () => {
try {
await runJestTests(["./test/fixtures/duplicatedTests.test.js"]);
} catch (err) {
// @ts-ignore
expect(err.message).eq(
// eslint-disable-next-line @typescript-eslint/quotes
'Test "has the same name" has been already added to run! To continue with reporting, please rename the test.',
);
}
await expect(() => runJestTests(["./test/fixtures/duplicatedTests.test.js"])).rejects.toThrow(
'Test "has the same name" has been already initialized! To continue with reporting, please rename the test.',
);
});
});
4 changes: 2 additions & 2 deletions packages/allure-jest/test/spec/historyId.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect } from "chai";
import expect from "expect";
import { runJestTests, TestResultsByFullName } from "../utils";

describe("historyId", () => {
Expand All @@ -11,6 +11,6 @@ describe("historyId", () => {
it("adds custom history id", () => {
const { historyId } = results.historyId;

expect(historyId).eq("foo");
expect(historyId).toBe("foo");
});
});
Loading
Loading