From 1dc4ac3136645da8795f9cb8e6ebac178446762a Mon Sep 17 00:00:00 2001 From: Boris Serdiuk Date: Wed, 21 Aug 2019 16:55:44 -0700 Subject: [PATCH 1/2] Improve typescript config --- packages/allure-cucumberjs/package.json | 1 - packages/allure-cucumberjs/tsconfig.json | 3 +-- packages/allure-jasmine/package.json | 1 - packages/allure-jasmine/tsconfig.json | 3 +-- packages/allure-jest/package.json | 1 - packages/allure-jest/tsconfig.json | 3 +-- packages/allure-js-commons/package.json | 1 - packages/allure-js-commons/tsconfig.json | 3 +-- packages/allure-mocha/README.md | 13 +++---------- packages/allure-mocha/index.ts | 2 ++ packages/allure-mocha/package.json | 1 - packages/allure-mocha/tsconfig.json | 3 +-- 12 files changed, 10 insertions(+), 25 deletions(-) diff --git a/packages/allure-cucumberjs/package.json b/packages/allure-cucumberjs/package.json index 3a634a019..74f65dd5f 100644 --- a/packages/allure-cucumberjs/package.json +++ b/packages/allure-cucumberjs/package.json @@ -9,7 +9,6 @@ ], "repository": "https://github.com/allure-framework/allure-js", "main": "./dist/index.js", - "typings": "./dist/declarations/", "scripts": { "clean": "rimraf ./dist ./out", "lint": "eslint ./src ./features ./index.ts --ext .ts", diff --git a/packages/allure-cucumberjs/tsconfig.json b/packages/allure-cucumberjs/tsconfig.json index 52c118a4e..3ed6a1772 100644 --- a/packages/allure-cucumberjs/tsconfig.json +++ b/packages/allure-cucumberjs/tsconfig.json @@ -17,7 +17,6 @@ "chai", "cucumber" ], - "outDir": "./dist", - "declarationDir": "./dist/declarations" + "outDir": "./dist" } } diff --git a/packages/allure-jasmine/package.json b/packages/allure-jasmine/package.json index 7fa351849..e2f0f0ab6 100644 --- a/packages/allure-jasmine/package.json +++ b/packages/allure-jasmine/package.json @@ -6,7 +6,6 @@ "author": "Ilya Korobitsyn ", "repository": "https://github.com/allure-framework/allure-js", "main": "./dist/index.js", - "typings": "./dist/declarations/", "scripts": { "clean": "rimraf ./dist ./out", "lint": "eslint ./src ./test ./index.ts --ext .ts", diff --git a/packages/allure-jasmine/tsconfig.json b/packages/allure-jasmine/tsconfig.json index abd2e2f7d..82f9a9512 100644 --- a/packages/allure-jasmine/tsconfig.json +++ b/packages/allure-jasmine/tsconfig.json @@ -10,7 +10,6 @@ "node", "jasmine" ], - "outDir": "./dist", - "declarationDir": "./dist/declarations" + "outDir": "./dist" } } diff --git a/packages/allure-jest/package.json b/packages/allure-jest/package.json index 3e425c751..9c4e0392a 100644 --- a/packages/allure-jest/package.json +++ b/packages/allure-jest/package.json @@ -7,7 +7,6 @@ "author": "Stanislav Seliverstov ", "repository": "https://github.com/allure-framework/allure-js", "main": "./dist/index.js", - "typings": "./dist/declarations/", "scripts": { "clean": "rimraf ./dist ./out", "lint": "eslint ./test --ext .ts", diff --git a/packages/allure-jest/tsconfig.json b/packages/allure-jest/tsconfig.json index c0a6f76c8..244dd2452 100644 --- a/packages/allure-jest/tsconfig.json +++ b/packages/allure-jest/tsconfig.json @@ -10,7 +10,6 @@ ], "skipLibCheck": true, "outDir": "./dist", - "rootDir": "./", - "declarationDir": "./dist/declarations" + "rootDir": "./" } } diff --git a/packages/allure-js-commons/package.json b/packages/allure-js-commons/package.json index da96268ec..695312b09 100644 --- a/packages/allure-js-commons/package.json +++ b/packages/allure-js-commons/package.json @@ -6,7 +6,6 @@ "author": "Ilya Korobitsyn ", "repository": "https://github.com/allure-framework/allure-js", "main": "./dist/index.js", - "typings": "./dist/declarations/", "scripts": { "clean": "rimraf ./dist", "lint": "eslint ./src ./index.ts --ext .ts", diff --git a/packages/allure-js-commons/tsconfig.json b/packages/allure-js-commons/tsconfig.json index 2764018cf..0e41eafee 100644 --- a/packages/allure-js-commons/tsconfig.json +++ b/packages/allure-js-commons/tsconfig.json @@ -8,7 +8,6 @@ "types": [ "node" ], - "outDir": "./dist", - "declarationDir": "./dist/declarations" + "outDir": "./dist" } } diff --git a/packages/allure-mocha/README.md b/packages/allure-mocha/README.md index b4f69d509..3cba14294 100644 --- a/packages/allure-mocha/README.md +++ b/packages/allure-mocha/README.md @@ -18,15 +18,6 @@ Note that it's recommended to add the following dependencies as well for better - mocha-typescript - source-map-support -## Allure types configuration - -Add the following into your **tsconfig.json** to access exported Allure types. -```json - "typeRoots": [ - "./node_modules/allure-js-commons/dist/declarations/**/" - ] -``` - ## Usage Either add **allure-mocha** into **mocha.opts**: @@ -46,7 +37,9 @@ mocha -R allure-mocha Now you can access a global **allure** object from within your project: ```typescript -const allure: AllureInterface = global.allure; +import { MochaAllureInterface } from 'allure-mocha'; + +declare const allure: MochaAllureInterface; ``` ## Decorators Support diff --git a/packages/allure-mocha/index.ts b/packages/allure-mocha/index.ts index d689474c2..8fe6d8063 100644 --- a/packages/allure-mocha/index.ts +++ b/packages/allure-mocha/index.ts @@ -1,3 +1,5 @@ import { MochaAllureReporter } from "./src/MochaAllureReporter"; +export { MochaAllureInterface } from "./src/MochaAllureInterface"; + module.exports = MochaAllureReporter; diff --git a/packages/allure-mocha/package.json b/packages/allure-mocha/package.json index 82c0a14fb..c9ac7dcf0 100644 --- a/packages/allure-mocha/package.json +++ b/packages/allure-mocha/package.json @@ -11,7 +11,6 @@ "allure" ], "main": "./dist/index.js", - "typings": "./dist/declarations/", "scripts": { "clean": "rimraf ./dist ./out", "compile": "tsc", diff --git a/packages/allure-mocha/tsconfig.json b/packages/allure-mocha/tsconfig.json index 2df26fd9d..6aa124ecf 100644 --- a/packages/allure-mocha/tsconfig.json +++ b/packages/allure-mocha/tsconfig.json @@ -14,7 +14,6 @@ "mocha", "chai" ], - "outDir": "./dist", - "declarationDir": "./dist/declarations" + "outDir": "./dist" } } From cb1365d534cf147b951a2c2ebf4d8d1b0b05617f Mon Sep 17 00:00:00 2001 From: Boris Serdiuk Date: Wed, 21 Aug 2019 18:19:02 -0700 Subject: [PATCH 2/2] Introduce swappable writer interface and use in-memory version in mocha tests --- .../allure-mocha/.prettierrc => .prettierrc | 2 +- packages/allure-cucumberjs/index.ts | 2 +- .../src/CucumberAllureInterface.ts | 14 +-- .../src/CucumberJSAllureReporter.ts | 7 +- packages/allure-jasmine/index.ts | 1 - .../src/JasmineAllureReporter.ts | 43 ++++----- packages/allure-jasmine/test/helpers.ts | 20 ++-- packages/allure-jasmine/test/setup.ts | 39 ++------ .../test/default-runner/allure-report.ts | 18 ++-- packages/allure-js-commons/index.ts | 4 +- packages/allure-js-commons/src/Allure.ts | 16 +++- .../allure-js-commons/src/AllureConfig.ts | 13 ++- packages/allure-js-commons/src/AllureGroup.ts | 4 +- .../allure-js-commons/src/AllureRuntime.ts | 74 +++------------ packages/allure-js-commons/src/AllureTest.ts | 4 +- .../allure-js-commons/src/GlobalInfoWriter.ts | 9 -- .../allure-js-commons/src/IAllureRuntime.ts | 19 ---- .../src/InMemoryAllureRuntime.ts | 57 ------------ .../src/writers/AllureWriter.ts | 49 ++++++++++ .../src/writers/IAllureWriter.ts | 13 +++ .../src/writers/InMemoryAllureWriter.ts | 52 +++++++++++ .../allure-js-commons/src/writers/index.ts | 4 + .../allure-js-commons/src/writers/utils.ts | 29 ++++++ packages/allure-mocha/package.json | 1 - packages/allure-mocha/src/AllureReporter.ts | 21 +---- .../allure-mocha/src/MochaAllureInterface.ts | 15 +-- .../allure-mocha/src/MochaAllureReporter.ts | 28 +++--- .../test/fixtures/specs/attachment.ts | 3 +- .../test/fixtures/specs/description.ts | 3 +- .../test/fixtures/specs/feature.ts | 3 +- .../test/fixtures/specs/globalInfo.ts | 21 +---- .../test/fixtures/specs/issueAndTms.ts | 3 +- .../allure-mocha/test/fixtures/specs/owner.ts | 3 +- .../test/fixtures/specs/parameter.ts | 3 +- .../test/fixtures/specs/severity.ts | 3 +- .../allure-mocha/test/fixtures/specs/step.ts | 3 +- .../allure-mocha/test/fixtures/specs/story.ts | 3 +- .../allure-mocha/test/fixtures/specs/tag.ts | 3 +- .../allure-mocha/test/specs/attachment.ts | 52 ----------- .../allure-mocha/test/specs/attachments.ts | 38 ++++++++ packages/allure-mocha/test/specs/common.ts | 33 +++---- .../allure-mocha/test/specs/description.ts | 20 ++-- packages/allure-mocha/test/specs/feature.ts | 19 ++-- .../allure-mocha/test/specs/globalInfo.ts | 61 ++++--------- .../allure-mocha/test/specs/issueAndTms.ts | 34 ++----- packages/allure-mocha/test/specs/owner.ts | 19 ++-- packages/allure-mocha/test/specs/parameter.ts | 25 ++--- packages/allure-mocha/test/specs/severity.ts | 20 ++-- packages/allure-mocha/test/specs/step.ts | 43 ++++----- packages/allure-mocha/test/specs/story.ts | 21 ++--- packages/allure-mocha/test/specs/tag.ts | 19 ++-- packages/allure-mocha/test/utils/index.ts | 91 ++----------------- packages/allure-mocha/tsconfig.json | 1 - 53 files changed, 429 insertions(+), 676 deletions(-) rename packages/allure-mocha/.prettierrc => .prettierrc (81%) delete mode 100644 packages/allure-js-commons/src/GlobalInfoWriter.ts delete mode 100644 packages/allure-js-commons/src/IAllureRuntime.ts delete mode 100644 packages/allure-js-commons/src/InMemoryAllureRuntime.ts create mode 100644 packages/allure-js-commons/src/writers/AllureWriter.ts create mode 100644 packages/allure-js-commons/src/writers/IAllureWriter.ts create mode 100644 packages/allure-js-commons/src/writers/InMemoryAllureWriter.ts create mode 100644 packages/allure-js-commons/src/writers/index.ts create mode 100644 packages/allure-js-commons/src/writers/utils.ts delete mode 100644 packages/allure-mocha/test/specs/attachment.ts create mode 100644 packages/allure-mocha/test/specs/attachments.ts diff --git a/packages/allure-mocha/.prettierrc b/.prettierrc similarity index 81% rename from packages/allure-mocha/.prettierrc rename to .prettierrc index d021bb054..a3fb4ac03 100644 --- a/packages/allure-mocha/.prettierrc +++ b/.prettierrc @@ -1,5 +1,5 @@ { - "singleQuote": true, + "singleQuote": false, "useTabs": false, "tabWidth": 2, "semi": true, diff --git a/packages/allure-cucumberjs/index.ts b/packages/allure-cucumberjs/index.ts index c7dcbc5ad..9abfb65ec 100644 --- a/packages/allure-cucumberjs/index.ts +++ b/packages/allure-cucumberjs/index.ts @@ -3,5 +3,5 @@ export { } from "./src/CucumberJSAllureReporter"; export { - AllureRuntime, AllureConfig, ContentType, Severity, GlobalInfoWriter, ExecutorInfo, Category + AllureRuntime, AllureConfig, ContentType, Severity, ExecutorInfo, Category } from "allure-js-commons"; diff --git a/packages/allure-cucumberjs/src/CucumberAllureInterface.ts b/packages/allure-cucumberjs/src/CucumberAllureInterface.ts index 0a9eb2657..43d3736e1 100644 --- a/packages/allure-cucumberjs/src/CucumberAllureInterface.ts +++ b/packages/allure-cucumberjs/src/CucumberAllureInterface.ts @@ -4,16 +4,16 @@ import { AllureTest, ContentType, ExecutableItemWrapper, - GlobalInfoWriter, isPromise, - LabelName, - Severity, Status, StepInterface + Status, + StepInterface, + AllureRuntime } from "allure-js-commons"; import { CucumberJSAllureFormatter } from "./CucumberJSAllureReporter"; export class CucumberAllureInterface extends Allure { - constructor(private readonly reporter: CucumberJSAllureFormatter) { - super(); + constructor(private readonly reporter: CucumberJSAllureFormatter, runtime: AllureRuntime) { + super(runtime); } protected get currentExecutable(): ExecutableItemWrapper { @@ -78,10 +78,6 @@ export class CucumberAllureInterface extends Allure { addLabel(name: string, value: string): void { this.currentTest.addLabel(name, value); } - - getGlobalInfoWriter(): GlobalInfoWriter { - return this.reporter.getGlobalInfoWriter(); - } } export class WrappedStep { diff --git a/packages/allure-cucumberjs/src/CucumberJSAllureReporter.ts b/packages/allure-cucumberjs/src/CucumberJSAllureReporter.ts index 33408cd02..3b6f24eba 100644 --- a/packages/allure-cucumberjs/src/CucumberJSAllureReporter.ts +++ b/packages/allure-cucumberjs/src/CucumberJSAllureReporter.ts @@ -7,7 +7,6 @@ import { AllureTest, ContentType, ExecutableItemWrapper, - GlobalInfoWriter, LabelName } from "allure-js-commons"; import { Result } from "./events/Result"; @@ -81,7 +80,7 @@ export class CucumberJSAllureFormatter extends Formatter { return message; }; - this.allureInterface = new CucumberAllureInterface(this); + this.allureInterface = new CucumberAllureInterface(this, this.allureRuntime); options.supportCodeLibrary.World.prototype.allure = this.allureInterface; this.beforeHooks = options.supportCodeLibrary.beforeTestCaseHookDefinitions; this.afterHooks = options.supportCodeLibrary.afterTestCaseHookDefinitions; @@ -317,10 +316,6 @@ export class CucumberJSAllureFormatter extends Formatter { writeAttachment(content: Buffer | string, type: ContentType): string { return this.allureRuntime.writeAttachment(content, type); } - - getGlobalInfoWriter(): GlobalInfoWriter { - return this.allureRuntime as GlobalInfoWriter; - } } diff --git a/packages/allure-jasmine/index.ts b/packages/allure-jasmine/index.ts index 10deeb5fa..3bdb31445 100644 --- a/packages/allure-jasmine/index.ts +++ b/packages/allure-jasmine/index.ts @@ -6,7 +6,6 @@ export { ContentType, Severity, Allure, - GlobalInfoWriter, ExecutorInfo, Category } from "allure-js-commons"; diff --git a/packages/allure-jasmine/src/JasmineAllureReporter.ts b/packages/allure-jasmine/src/JasmineAllureReporter.ts index eb483b1c4..d2f36eea1 100644 --- a/packages/allure-jasmine/src/JasmineAllureReporter.ts +++ b/packages/allure-jasmine/src/JasmineAllureReporter.ts @@ -1,7 +1,18 @@ -/* eslint-disable no-undef */ import { - AllureGroup, IAllureRuntime, AllureStep, AllureTest, ContentType, ExecutableItemWrapper, - isPromise, LabelName, Severity, Stage, Status, GlobalInfoWriter, Allure, StepInterface, Label + AllureGroup, + AllureRuntime, + AllureStep, + AllureTest, + ContentType, + ExecutableItemWrapper, + isPromise, + LabelName, + Stage, + Status, + Allure, + StepInterface, + Label, + IAllureConfig } from "allure-js-commons"; import FailedExpectation = jasmine.FailedExpectation; @@ -22,8 +33,10 @@ export class JasmineAllureReporter implements jasmine.CustomReporter { private runningTest: AllureTest | null = null; private stepStack: AllureStep[] = []; private runningExecutable: ExecutableItemWrapper | null = null; + private readonly runtime: AllureRuntime; - constructor(private readonly runtime: IAllureRuntime) { + constructor(config: IAllureConfig) { + this.runtime = new AllureRuntime(config); this.installHooks(); } @@ -39,11 +52,7 @@ export class JasmineAllureReporter implements jasmine.CustomReporter { } getInterface(): Allure { - return new JasmineAllureInterface(this); - } - - getGlobalInfoWriter(): GlobalInfoWriter { - return this.runtime as GlobalInfoWriter; + return new JasmineAllureInterface(this, this.runtime); } get currentTest(): AllureTest { @@ -164,8 +173,7 @@ export class JasmineAllureReporter implements jasmine.CustomReporter { this.labelStack.pop(); } - jasmineDone(runDetails: jasmine.RunDetails): void { - } + jasmineDone(runDetails: jasmine.RunDetails): void {} private findMessageAboutThrow(expectations?: FailedExpectation[]): FailedExpectation | null { for (const e of expectations || []) { @@ -252,10 +260,9 @@ export class JasmineAllureReporter implements jasmine.CustomReporter { } } - export class JasmineAllureInterface extends Allure { - constructor(private readonly reporter: JasmineAllureReporter) { - super(); + constructor(private readonly reporter: JasmineAllureReporter, runtime: AllureRuntime) { + super(runtime); } public label(name: string, value: string): void { @@ -267,9 +274,7 @@ export class JasmineAllureInterface extends Allure { } protected get currentExecutable(): ExecutableItemWrapper { - return this.reporter.currentStep - || this.reporter.currentExecutable - || this.reporter.currentTest; + return this.reporter.currentStep || this.reporter.currentExecutable || this.reporter.currentTest; } protected get currentTest(): AllureTest { @@ -314,10 +319,6 @@ export class JasmineAllureInterface extends Allure { const file = this.reporter.writeAttachment(content, type); this.currentExecutable.addAttachment(name, type, file); } - - getGlobalInfoWriter(): GlobalInfoWriter { - return this.reporter.getGlobalInfoWriter(); - } } class WrappedStep { // needed? diff --git a/packages/allure-jasmine/test/helpers.ts b/packages/allure-jasmine/test/helpers.ts index 38d03a0a0..76b3fef33 100644 --- a/packages/allure-jasmine/test/helpers.ts +++ b/packages/allure-jasmine/test/helpers.ts @@ -1,14 +1,11 @@ -import {Allure, InMemoryAllureRuntime} from "allure-js-commons"; -import {JasmineAllureReporter} from "../src/JasmineAllureReporter"; +import { Allure, InMemoryAllureWriter } from "allure-js-commons"; +import { JasmineAllureReporter } from "../src/JasmineAllureReporter"; import Env = jasmine.Env; -export async function runTest(fun: (j: Env, k: Allure) => void): Promise { - const runtime = new InMemoryAllureRuntime({ - resultsDir: "./out/allure-results" - }); - - const d = new Promise((resolve, reject) => { - const reporter = new JasmineAllureReporter(runtime); +export async function runTest(fun: (j: Env, k: Allure) => void) { + const writer = new InMemoryAllureWriter(); + await new Promise((resolve, reject) => { + const reporter = new JasmineAllureReporter({ writer, resultsDir: "unused" }); const env: Env = eval("new jasmine.Env()"); env.addReporter(reporter); env.addReporter({ jasmineDone: resolve }); @@ -16,9 +13,7 @@ export async function runTest(fun: (j: Env, k: Allure) => void): Promise { if (result.status == Status.SKIPPED) result.fullName = `(WAS SKIPPED) ${result.fullName}`; return result; } }); -const reporter = new JasmineAllureReporter(runtime); jasmine.getEnv().addReporter(reporter); -export const allure: Allure = reporter.getInterface(); -const giw: GlobalInfoWriter = allure.getGlobalInfoWriter(); +export const allure = reporter.getInterface(); -giw.writeExecutorInfo({ - "name": "Jenkins", - "type": "jenkins", - "url": "http://example.org", - "buildOrder": 13, - "buildName": "allure-report_deploy#13", - "buildUrl": "http://example.org/build#13", - "reportUrl": "http://example.org/build#13/AllureReport", - "reportName": "Demo allure report" +allure.writeEnvironmentInfo({ + a: "b", + PATH: "azazaz", + APPDATA: "C:\\USERS\\test (x86)\\AppData", + PS1: "\\[\\0330;$MSYSTEM;${PWD//[^[:ascii:]]/?}\\007\\]", + TEST1: "\\usr\\bin" }); -giw.writeEnvironmentInfo({ - "a": "b", - "PATH": "azazaz", - "APPDATA": "C:\\USERS\\test (x86)\\AppData", - "PS1": "\\[\\0330;$MSYSTEM;${PWD//[^[:ascii:]]/?}\\007\\]", - "TEST1": "\\usr\\bin" -}); - -giw.writeCategories([ +allure.writeCategoriesDefinitions([ { "name": "Sad tests", "messageRegex": /.*Sad.*/, diff --git a/packages/allure-jest/test/default-runner/allure-report.ts b/packages/allure-jest/test/default-runner/allure-report.ts index fe703f683..5fd677e65 100644 --- a/packages/allure-jest/test/default-runner/allure-report.ts +++ b/packages/allure-jest/test/default-runner/allure-report.ts @@ -1,13 +1,11 @@ import { JasmineAllureReporter } from "allure-jasmine"; -import { AllureRuntime, Status, TestResult } from "allure-js-commons"; +import { Status, TestResult } from "allure-js-commons"; -const reporter = new JasmineAllureReporter( - new AllureRuntime({ - resultsDir: "./out/allure-results", - testMapper: (result: TestResult) => { - if (result.status == Status.SKIPPED) result.fullName = `(WAS SKIPPED) ${result.fullName}`; - return result; - } - }) -); +const reporter = new JasmineAllureReporter({ + resultsDir: "./out/allure-results", + testMapper: (result: TestResult) => { + if (result.status == Status.SKIPPED) result.fullName = `(WAS SKIPPED) ${result.fullName}`; + return result; + } +}); jasmine.getEnv().addReporter(reporter); diff --git a/packages/allure-js-commons/index.ts b/packages/allure-js-commons/index.ts index f3a27c9bd..fefc4ad57 100644 --- a/packages/allure-js-commons/index.ts +++ b/packages/allure-js-commons/index.ts @@ -1,4 +1,5 @@ export { AllureRuntime } from "./src/AllureRuntime"; +export { InMemoryAllureWriter } from "./src/writers"; export { AllureConfig, IAllureConfig } from "./src/AllureConfig"; export { AllureGroup } from "./src/AllureGroup"; export { ExecutableItemWrapper } from "./src/ExecutableItemWrapper"; @@ -6,7 +7,6 @@ export { AllureTest } from "./src/AllureTest"; export { AllureStep } from "./src/ExecutableItemWrapper"; export { isPromise } from "./src/isPromise"; export { Allure, StepInterface } from "./src/Allure"; -export { GlobalInfoWriter } from "./src/GlobalInfoWriter"; export { Attachment, Category, @@ -26,5 +26,3 @@ export { Status, LinkType } from "./src/model"; -export { InMemoryAllureRuntime } from "./src/InMemoryAllureRuntime"; -export { IAllureRuntime } from "./src/IAllureRuntime"; diff --git a/packages/allure-js-commons/src/Allure.ts b/packages/allure-js-commons/src/Allure.ts index e316e7699..20483fdd7 100644 --- a/packages/allure-js-commons/src/Allure.ts +++ b/packages/allure-js-commons/src/Allure.ts @@ -1,14 +1,16 @@ -import { LinkType, Status } from "./model"; +import { Category, LinkType, Status } from "./model"; import { ContentType } from "./model"; import { LabelName } from "./model"; -import { GlobalInfoWriter } from "./GlobalInfoWriter"; import { AllureTest } from "./AllureTest"; import { ExecutableItemWrapper } from "./ExecutableItemWrapper"; +import { AllureRuntime } from "./AllureRuntime"; export abstract class Allure { protected abstract get currentTest(): AllureTest; // test only protected abstract get currentExecutable(): ExecutableItemWrapper; // step or test + protected constructor(protected runtime: AllureRuntime) {} + public epic(epic: string) { this.label(LabelName.EPIC, epic); } @@ -75,11 +77,17 @@ export abstract class Allure { this.label(LabelName.TAG, tag); } + public writeEnvironmentInfo(info: Record) { + this.runtime.writeEnvironmentInfo(info); + } + + public writeCategoriesDefinitions(categories: Category[]) { + this.runtime.writeCategoriesDefinitions(categories); + } + public abstract logStep(name: string, status?: Status): void; public abstract step(name: string, body: (step: StepInterface) => any): any; - public abstract getGlobalInfoWriter(): GlobalInfoWriter; - // below are compatibility functions /** diff --git a/packages/allure-js-commons/src/AllureConfig.ts b/packages/allure-js-commons/src/AllureConfig.ts index 835932a57..917a9f486 100644 --- a/packages/allure-js-commons/src/AllureConfig.ts +++ b/packages/allure-js-commons/src/AllureConfig.ts @@ -1,12 +1,19 @@ import { TestResult } from "./model"; +import { IAllureWriter } from "./writers"; export interface IAllureConfig { readonly resultsDir: string; + readonly writer?: IAllureWriter; readonly testMapper?: (test: TestResult) => TestResult | null; } +/** + * @deprecated Instantiate config object directly + */ export class AllureConfig implements IAllureConfig { - constructor(public readonly resultsDir: string = "allure-results", - public readonly testMapper?: (test: TestResult) => TestResult | null) { - } + constructor( + public readonly resultsDir: string = "allure-results", + public readonly testMapper?: (test: TestResult) => TestResult | null, + public readonly writer?: IAllureWriter + ) {} } diff --git a/packages/allure-js-commons/src/AllureGroup.ts b/packages/allure-js-commons/src/AllureGroup.ts index af8536795..543484db5 100644 --- a/packages/allure-js-commons/src/AllureGroup.ts +++ b/packages/allure-js-commons/src/AllureGroup.ts @@ -2,12 +2,12 @@ import { TestResultContainer } from "./model"; import { ExecutableItemWrapper } from "./ExecutableItemWrapper"; import { AllureTest } from "./AllureTest"; import { fixtureResult, testResultContainer } from "./constructors"; -import { IAllureRuntime } from "./IAllureRuntime"; +import { AllureRuntime } from "./AllureRuntime"; export class AllureGroup { private testResultContainer: TestResultContainer = testResultContainer(); - constructor(private readonly runtime: IAllureRuntime) {} + constructor(private readonly runtime: AllureRuntime) {} startGroup(name?: string): AllureGroup { const group = new AllureGroup(this.runtime); diff --git a/packages/allure-js-commons/src/AllureRuntime.ts b/packages/allure-js-commons/src/AllureRuntime.ts index 3f5ca97e5..c948f5645 100644 --- a/packages/allure-js-commons/src/AllureRuntime.ts +++ b/packages/allure-js-commons/src/AllureRuntime.ts @@ -1,46 +1,14 @@ -import { Category, ContentType, ExecutorInfo, TestResult, TestResultContainer } from "./model"; -import { existsSync, writeFileSync } from "fs"; -import { sync as mkdirSync } from "mkdirp"; -import { join as buildPath } from "path"; +import { Category, ContentType, TestResult, TestResultContainer } from "./model"; import { v4 as randomUUID } from "uuid"; -import { stringify } from "properties"; import { IAllureConfig } from "./AllureConfig"; import { AllureGroup } from "./AllureGroup"; -import { IAllureRuntime } from "./IAllureRuntime"; +import { AllureWriter, IAllureWriter, typeToExtension } from "./writers"; -function typeToExtension(type: ContentType): string { - switch (type) { - case ContentType.TEXT: - return "txt"; - case ContentType.XML: - return "xml"; - case ContentType.CSV: - return "csv"; - case ContentType.TSV: - return "tsv"; - case ContentType.CSS: - return "css"; - case ContentType.URI: - return "uri"; - case ContentType.SVG: - return "svg"; - case ContentType.PNG: - return "png"; - case ContentType.JSON: - return "json"; - case ContentType.WEBM: - return "webm"; - case ContentType.JPEG: - return "jpg"; - } - throw new Error(`Unrecognized extension: ${type}`); -} +export class AllureRuntime { + private writer: IAllureWriter; -export class AllureRuntime implements IAllureRuntime { constructor(private config: IAllureConfig) { - if (!existsSync(this.config.resultsDir)) { - mkdirSync(this.config.resultsDir); - } + this.writer = config.writer || new AllureWriter(config); } startGroup(name?: string): AllureGroup { @@ -50,45 +18,33 @@ export class AllureRuntime implements IAllureRuntime { } writeResult(result: TestResult): void { - const modifiedResult = - this.config.testMapper !== undefined ? this.config.testMapper(result) : result; + const modifiedResult = this.config.testMapper !== undefined ? this.config.testMapper(result) : result; if (modifiedResult != null) { - const path = buildPath(this.config.resultsDir, `${modifiedResult.uuid}-result.json`); - writeFileSync(path, JSON.stringify(modifiedResult), { encoding: "utf-8" }); + this.writer.writeResult(modifiedResult); } } writeGroup(result: TestResultContainer): void { - const path = buildPath(this.config.resultsDir, `${result.uuid}-container.json`); - writeFileSync(path, JSON.stringify(result), { encoding: "utf-8" }); + this.writer.writeGroup(result); } writeAttachment(content: Buffer | string, contentType: ContentType): string { const extension = typeToExtension(contentType); const fileName = `${randomUUID()}-attachment.${extension}`; - const path = buildPath(this.config.resultsDir, fileName); - writeFileSync(path, content, { encoding: "utf-8" }); + this.writer.writeAttachment(fileName, content); return fileName; } - writeExecutorInfo(info: ExecutorInfo) { - const path = buildPath(this.config.resultsDir, "executor.json"); - writeFileSync(path, JSON.stringify(info), { encoding: "utf-8" }); - } - - writeEnvironmentInfo(info?: { [key: string]: string }) { - const path = buildPath(this.config.resultsDir, "environment.properties"); - const target = info || process.env; - const text = stringify(target, { unicode: true }); - writeFileSync(path, text, { encoding: "utf-8" }); + writeEnvironmentInfo(info?: Record) { + this.writer.writeEnvironmentInfo(info || process.env); } - writeCategories(categories: Category[]) { - const path = buildPath(this.config.resultsDir, "categories.json"); - writeFileSync(path, JSON.stringify(categories.map(c => { + writeCategoriesDefinitions(categories: Category[]) { + const serializedCategories = categories.map(c => { if (c.messageRegex instanceof RegExp) c.messageRegex = c.messageRegex.source; if (c.traceRegex instanceof RegExp) c.traceRegex = c.traceRegex.source; return c; - })), { encoding: "utf-8" }); + }); + this.writer.writeCategoriesDefinitions(serializedCategories); } } diff --git a/packages/allure-js-commons/src/AllureTest.ts b/packages/allure-js-commons/src/AllureTest.ts index 8e8bc303d..8eb52565f 100644 --- a/packages/allure-js-commons/src/AllureTest.ts +++ b/packages/allure-js-commons/src/AllureTest.ts @@ -1,12 +1,12 @@ import { ExecutableItemWrapper } from "./ExecutableItemWrapper"; import { TestResult } from "./model"; import { testResult } from "./constructors"; -import { IAllureRuntime } from "./IAllureRuntime"; +import { AllureRuntime } from "./AllureRuntime"; export class AllureTest extends ExecutableItemWrapper { private readonly testResult: TestResult; - constructor(private readonly runtime: IAllureRuntime) { + constructor(private readonly runtime: AllureRuntime) { super(testResult()); this.testResult = this.wrappedItem as TestResult; this.testResult.start = Date.now(); diff --git a/packages/allure-js-commons/src/GlobalInfoWriter.ts b/packages/allure-js-commons/src/GlobalInfoWriter.ts deleted file mode 100644 index fdef16bb4..000000000 --- a/packages/allure-js-commons/src/GlobalInfoWriter.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { ExecutorInfo, Category } from "./model"; - -export interface GlobalInfoWriter { - writeExecutorInfo(info: ExecutorInfo): void; - - writeEnvironmentInfo(info?: { [key: string]: string }): void; - - writeCategories(categories: Category[]): void; -} diff --git a/packages/allure-js-commons/src/IAllureRuntime.ts b/packages/allure-js-commons/src/IAllureRuntime.ts deleted file mode 100644 index 1bb29ec0a..000000000 --- a/packages/allure-js-commons/src/IAllureRuntime.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { GlobalInfoWriter } from "./GlobalInfoWriter"; -import { AllureGroup } from "./AllureGroup"; -import { Category, ContentType, ExecutorInfo, TestResult, TestResultContainer } from "./model"; - -export interface IAllureRuntime extends GlobalInfoWriter { - startGroup(name?: string): AllureGroup; - - writeResult(result: TestResult): void; - - writeGroup(result: TestResultContainer): void; - - writeAttachment(content: Buffer | string, contentType: ContentType): string; - - writeExecutorInfo(info: ExecutorInfo): void; - - writeEnvironmentInfo(info?: { [key: string]: string }): void; - - writeCategories(categories: Category[]): void; -} diff --git a/packages/allure-js-commons/src/InMemoryAllureRuntime.ts b/packages/allure-js-commons/src/InMemoryAllureRuntime.ts deleted file mode 100644 index 3f0b344c2..000000000 --- a/packages/allure-js-commons/src/InMemoryAllureRuntime.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { IAllureConfig } from "./AllureConfig"; -import { AllureGroup } from "./AllureGroup"; -import { Category, ContentType, ExecutorInfo, TestResult, TestResultContainer } from "./model"; -import { IAllureRuntime } from "./IAllureRuntime"; - -export class InMemoryAllureRuntime implements IAllureRuntime { - constructor(public config: IAllureConfig) { - - } - - startGroup(name?: string): AllureGroup { - const allureContainer = new AllureGroup(this); - allureContainer.name = name || "Unnamed"; - return allureContainer; - } - - writeAttachment(content: Buffer | string, contentType: ContentType): string { - return ""; - } - - writeCategories(categories: Category[]): void { - } - - writeEnvironmentInfo(info?: { [p: string]: string }): void { - } - - writeExecutorInfo(info: ExecutorInfo): void { - } - - public groups: TestResultContainer[] = []; - - writeGroup(result: TestResultContainer): void { - this.groups.push(result); - } - - public tests: TestResult[] = []; - - writeResult(result: TestResult): void { - const res = this.config.testMapper !== undefined ? this.config.testMapper(result) : result; - if (res !== null) this.tests.push(res); - } - - public reset(): void { - this.groups = []; - this.tests = []; - } - - public getMaybeTestByName(name: string): TestResult | undefined { - return this.tests.find(t => t.name === name); - } - - public getTestByName(name: string): TestResult { - const res = this.getMaybeTestByName(name); - if (res === undefined) throw new Error(`Test not found: ${name}`); - return res; - } -} diff --git a/packages/allure-js-commons/src/writers/AllureWriter.ts b/packages/allure-js-commons/src/writers/AllureWriter.ts new file mode 100644 index 000000000..cfbafcd24 --- /dev/null +++ b/packages/allure-js-commons/src/writers/AllureWriter.ts @@ -0,0 +1,49 @@ +import { join } from "path"; +import { sync as mkdirSync } from "mkdirp"; +import { existsSync, writeFileSync } from "fs"; +import { Category, TestResult, TestResultContainer } from "../model"; +import { IAllureWriter } from "./IAllureWriter"; +import { IAllureConfig } from "../AllureConfig"; +import { stringify } from "properties"; + +function writeJson(path: string, data: object) { + writeFileSync(path, JSON.stringify(data), { encoding: "utf-8" }); +} + +export class AllureWriter implements IAllureWriter { + constructor(private config: IAllureConfig) { + if (!existsSync(this.config.resultsDir)) { + mkdirSync(this.config.resultsDir); + } + } + + private buildPath(name: string) { + return join(this.config.resultsDir, name); + } + + writeAttachment(name: string, content: Buffer | string) { + const path = this.buildPath(name); + writeFileSync(path, content); + } + + writeEnvironmentInfo(info?: Record) { + const text = stringify(info, { unicode: true }); + const path = this.buildPath("environment.properties"); + writeFileSync(path, text); + } + + writeCategoriesDefinitions(categories: Category[]) { + const path = this.buildPath("categories.json"); + writeJson(path, categories); + } + + writeGroup(result: TestResultContainer) { + const path = this.buildPath(`${result.uuid}-container.json`); + writeJson(path, result); + } + + writeResult(result: TestResult) { + const path = this.buildPath(`${result.uuid}-result.json`); + writeJson(path, result); + } +} diff --git a/packages/allure-js-commons/src/writers/IAllureWriter.ts b/packages/allure-js-commons/src/writers/IAllureWriter.ts new file mode 100644 index 000000000..01132e162 --- /dev/null +++ b/packages/allure-js-commons/src/writers/IAllureWriter.ts @@ -0,0 +1,13 @@ +import { Category, TestResult, TestResultContainer } from "../model"; + +export interface IAllureWriter { + writeResult(result: TestResult): void; + + writeGroup(result: TestResultContainer): void; + + writeAttachment(name: string, content: Buffer | string): void; + + writeEnvironmentInfo(info: Record): void; + + writeCategoriesDefinitions(categories: Category[]): void; +} diff --git a/packages/allure-js-commons/src/writers/InMemoryAllureWriter.ts b/packages/allure-js-commons/src/writers/InMemoryAllureWriter.ts new file mode 100644 index 000000000..188cb51c2 --- /dev/null +++ b/packages/allure-js-commons/src/writers/InMemoryAllureWriter.ts @@ -0,0 +1,52 @@ +import { Category, TestResult, TestResultContainer } from "../model"; +import { IAllureWriter } from "./IAllureWriter"; + +export class InMemoryAllureWriter implements IAllureWriter { + public groups: TestResultContainer[] = []; + public tests: TestResult[] = []; + public attachments: Record = {}; + public categories?: Category[]; + public envInfo?: Record; + + public writeGroup(result: TestResultContainer) { + this.groups.push(result); + } + + public writeResult(result: TestResult): void { + this.tests.push(result); + } + + public writeAttachment(name: string, content: Buffer | string) { + this.attachments[name] = content; + } + + public writeCategoriesDefinitions(categories: Category[]) { + if (this.categories) { + console.warn("overwriting existing categories"); + } + this.categories = categories; + } + + public writeEnvironmentInfo(envInfo?: Record) { + if (this.envInfo) { + console.warn("overwriting existing environment info"); + } + this.envInfo = envInfo; + } + + public reset(): void { + this.groups = []; + this.tests = []; + this.attachments = {}; + } + + public getMaybeTestByName(name: string): TestResult | undefined { + return this.tests.find(t => t.name === name); + } + + public getTestByName(name: string): TestResult { + const res = this.getMaybeTestByName(name); + if (res === undefined) throw new Error(`Test not found: ${name}`); + return res; + } +} diff --git a/packages/allure-js-commons/src/writers/index.ts b/packages/allure-js-commons/src/writers/index.ts new file mode 100644 index 000000000..1b8e6c96f --- /dev/null +++ b/packages/allure-js-commons/src/writers/index.ts @@ -0,0 +1,4 @@ +export { AllureWriter } from "./AllureWriter"; +export { IAllureWriter } from "./IAllureWriter"; +export { InMemoryAllureWriter } from "./InMemoryAllureWriter"; +export * from "./utils"; diff --git a/packages/allure-js-commons/src/writers/utils.ts b/packages/allure-js-commons/src/writers/utils.ts new file mode 100644 index 000000000..af16fc608 --- /dev/null +++ b/packages/allure-js-commons/src/writers/utils.ts @@ -0,0 +1,29 @@ +import { ContentType } from "../model"; + +export function typeToExtension(type: ContentType): string { + switch (type) { + case ContentType.TEXT: + return "txt"; + case ContentType.XML: + return "xml"; + case ContentType.CSV: + return "csv"; + case ContentType.TSV: + return "tsv"; + case ContentType.CSS: + return "css"; + case ContentType.URI: + return "uri"; + case ContentType.SVG: + return "svg"; + case ContentType.PNG: + return "png"; + case ContentType.JSON: + return "json"; + case ContentType.WEBM: + return "webm"; + case ContentType.JPEG: + return "jpg"; + } + throw new Error(`Unrecognized extension: ${type}`); +} diff --git a/packages/allure-mocha/package.json b/packages/allure-mocha/package.json index c9ac7dcf0..ed38f34fb 100644 --- a/packages/allure-mocha/package.json +++ b/packages/allure-mocha/package.json @@ -34,7 +34,6 @@ "mocha-typescript": "^1.1.17", "nyc": "^14.0.0", "prettier": "^1.17.0", - "properties-reader": "^0.0.16", "source-map-support": "^0.5.12" }, "dependencies": { diff --git a/packages/allure-mocha/src/AllureReporter.ts b/packages/allure-mocha/src/AllureReporter.ts index 9b9a2da19..57f4386db 100644 --- a/packages/allure-mocha/src/AllureReporter.ts +++ b/packages/allure-mocha/src/AllureReporter.ts @@ -1,11 +1,9 @@ import { AllureGroup, - Allure, AllureRuntime, AllureStep, AllureTest, ContentType, - GlobalInfoWriter, LabelName, Stage, Status @@ -17,18 +15,11 @@ export class AllureReporter { private suites: AllureGroup[] = []; private steps: AllureStep[] = []; private runningTest: AllureTest | null = null; - private runtime: AllureRuntime | null = null; - public setupRuntime(path: string) { - this.runtime = new AllureRuntime({ resultsDir: path }); - } - - public getInterface(): Allure { - return new MochaAllureInterface(this); - } + constructor(private runtime: AllureRuntime) {} - public getGlobalInfoWriter(): GlobalInfoWriter { - return this.runtime as GlobalInfoWriter; + public getInterface(): MochaAllureInterface { + return new MochaAllureInterface(this, this.runtime); } get currentSuite(): AllureGroup | null { @@ -56,9 +47,6 @@ export class AllureReporter { public startSuite(suiteName: string) { if (suiteName) { const scope = this.currentSuite || this.runtime; - if (scope == null) { - throw Error("Suite scope is not defined"); - } const suite = scope.startGroup(suiteName); this.pushSuite(suite); } @@ -110,9 +98,6 @@ export class AllureReporter { } public writeAttachment(content: Buffer | string, type: ContentType): string { - if (this.runtime == null) { - throw Error("AllureReporter runtime is not defined"); - } return this.runtime.writeAttachment(content, type); } diff --git a/packages/allure-mocha/src/MochaAllureInterface.ts b/packages/allure-mocha/src/MochaAllureInterface.ts index fcee0ed21..f93b05e4f 100644 --- a/packages/allure-mocha/src/MochaAllureInterface.ts +++ b/packages/allure-mocha/src/MochaAllureInterface.ts @@ -1,20 +1,19 @@ import { - Allure, + Allure, AllureRuntime, AllureStep, AllureTest, ContentType, ExecutableItemWrapper, - GlobalInfoWriter, isPromise, - LabelName, - Severity, Status, StepInterface + Status, + StepInterface } from "allure-js-commons"; import { AllureReporter } from "./AllureReporter"; import { StepWrapper } from "./StepWrapper"; export class MochaAllureInterface extends Allure { - constructor(private readonly reporter: AllureReporter) { - super(); + constructor(private readonly reporter: AllureReporter, runtime: AllureRuntime) { + super(runtime); } public step(name: string, body: (step: StepInterface) => any): any { @@ -56,10 +55,6 @@ export class MochaAllureInterface extends Allure { this.currentTest.addAttachment(name, type, file); } - public getGlobalInfoWriter(): GlobalInfoWriter { - return this.reporter.getGlobalInfoWriter(); - } - private startStep(name: string): StepWrapper { const allureStep: AllureStep = this.currentExecutable.startStep(name); this.reporter.pushStep(allureStep); diff --git a/packages/allure-mocha/src/MochaAllureReporter.ts b/packages/allure-mocha/src/MochaAllureReporter.ts index 42eb0301d..41b6ebe2d 100644 --- a/packages/allure-mocha/src/MochaAllureReporter.ts +++ b/packages/allure-mocha/src/MochaAllureReporter.ts @@ -1,17 +1,18 @@ import * as Mocha from "mocha"; +import { AllureRuntime, IAllureConfig } from "allure-js-commons"; import { AllureReporter } from "./AllureReporter"; -const allure = new AllureReporter(); -// @ts-ignore -global.allure = allure.getInterface(); - export class MochaAllureReporter extends Mocha.reporters.Base { + private allure: AllureReporter; + constructor(readonly runner: Mocha.Runner, readonly opts: Mocha.MochaOptions) { super(runner, opts); - const reporterOpts = opts.reporterOptions; - const resultsDir = (reporterOpts && reporterOpts.resultsDir) || "allure-results"; - allure.setupRuntime(resultsDir); + const allureConfig: IAllureConfig = { resultsDir: "allure-results", ...opts.reporterOptions }; + + this.allure = new AllureReporter(new AllureRuntime(allureConfig)); + + (global as any).allure = this.allure.getInterface(); this.runner .on("suite", this.onSuite.bind(this)) @@ -23,27 +24,28 @@ export class MochaAllureReporter extends Mocha.reporters.Base { } private onSuite(suite: Mocha.Suite) { - allure.startSuite(suite.fullTitle()); + this.allure.startSuite(suite.fullTitle()); } private onSuiteEnd() { - allure.endSuite(); + this.allure.endSuite(); } private onTest(test: Mocha.Test) { const suite = test.parent; - allure.startCase((suite && suite.title) || "Unnamed", test.title); + this.allure.startCase((suite && suite.title) || "Unnamed", test.title); } private onPassed(test: Mocha.Test) { - allure.passTestCase(); + this.allure.passTestCase(); } private onFailed(test: Mocha.Test, error: Error) { - allure.failTestCase(test, error); + console.error(error); + this.allure.failTestCase(test, error); } private onPending(test: Mocha.Test) { - allure.pendingTestCase(test); + this.allure.pendingTestCase(test); } } diff --git a/packages/allure-mocha/test/fixtures/specs/attachment.ts b/packages/allure-mocha/test/fixtures/specs/attachment.ts index adfbfec96..5ef071bb5 100644 --- a/packages/allure-mocha/test/fixtures/specs/attachment.ts +++ b/packages/allure-mocha/test/fixtures/specs/attachment.ts @@ -2,8 +2,7 @@ import { ContentType } from "allure-js-commons"; import { suite, test } from "mocha-typescript"; import { MochaAllureInterface } from "../../../src/MochaAllureInterface"; -// @ts-ignore -const allure: MochaAllureInterface = global.allure; +declare const allure: MochaAllureInterface; @suite class AttachmentSubSuite { diff --git a/packages/allure-mocha/test/fixtures/specs/description.ts b/packages/allure-mocha/test/fixtures/specs/description.ts index 5c55a3af6..41cacacb9 100644 --- a/packages/allure-mocha/test/fixtures/specs/description.ts +++ b/packages/allure-mocha/test/fixtures/specs/description.ts @@ -1,8 +1,7 @@ import { suite, test } from "mocha-typescript"; import { MochaAllureInterface } from "../../../src/MochaAllureInterface"; -// @ts-ignore -const allure: MochaAllureInterface = global.allure; +declare const allure: MochaAllureInterface; @suite class Description { diff --git a/packages/allure-mocha/test/fixtures/specs/feature.ts b/packages/allure-mocha/test/fixtures/specs/feature.ts index df0ceed30..8b82166a1 100644 --- a/packages/allure-mocha/test/fixtures/specs/feature.ts +++ b/packages/allure-mocha/test/fixtures/specs/feature.ts @@ -1,8 +1,7 @@ import { suite, test } from "mocha-typescript"; import { MochaAllureInterface } from "../../../src/MochaAllureInterface"; -// @ts-ignore -const allure: MochaAllureInterface = global.allure; +declare const allure: MochaAllureInterface; @suite class Feature { diff --git a/packages/allure-mocha/test/fixtures/specs/globalInfo.ts b/packages/allure-mocha/test/fixtures/specs/globalInfo.ts index 2e335cfd7..86ded67b2 100644 --- a/packages/allure-mocha/test/fixtures/specs/globalInfo.ts +++ b/packages/allure-mocha/test/fixtures/specs/globalInfo.ts @@ -2,28 +2,13 @@ import { Status } from "allure-js-commons"; import { suite, test } from "mocha-typescript"; import { MochaAllureInterface } from "../../../src/MochaAllureInterface"; -// @ts-ignore -const allure: MochaAllureInterface = global.allure; +declare const allure: MochaAllureInterface; @suite class GlobalInfo { - @test - shouldWriteExecutorInfo() { - allure.getGlobalInfoWriter().writeExecutorInfo({ - name: "Jenkins", - type: "jenkins", - url: "http://example.org", - buildOrder: 11, - buildName: "allure-report_deploy#11", - buildUrl: "http://example.org/build#11", - reportUrl: "http://example.org/build#11/AllureReport", - reportName: "Demo allure report" - }); - } - @test shouldWriteEnvironment() { - allure.getGlobalInfoWriter().writeEnvironmentInfo({ + allure.writeEnvironmentInfo({ Browser: "chrome", GitHub: "https://github.com/sskorol", Author: "Sergey Korol" @@ -32,7 +17,7 @@ class GlobalInfo { @test shouldWriteCategories() { - allure.getGlobalInfoWriter().writeCategories([ + allure.writeCategoriesDefinitions([ { name: "Sad tests", messageRegex: /.*Sad.*/, diff --git a/packages/allure-mocha/test/fixtures/specs/issueAndTms.ts b/packages/allure-mocha/test/fixtures/specs/issueAndTms.ts index 60efea530..04c25054c 100644 --- a/packages/allure-mocha/test/fixtures/specs/issueAndTms.ts +++ b/packages/allure-mocha/test/fixtures/specs/issueAndTms.ts @@ -1,8 +1,7 @@ import { suite, test } from "mocha-typescript"; import { MochaAllureInterface } from "../../../src/MochaAllureInterface"; -// @ts-ignore -const allure: MochaAllureInterface = global.allure; +declare const allure: MochaAllureInterface; @suite class IssueAndTms { diff --git a/packages/allure-mocha/test/fixtures/specs/owner.ts b/packages/allure-mocha/test/fixtures/specs/owner.ts index 35d37655e..813df4d92 100644 --- a/packages/allure-mocha/test/fixtures/specs/owner.ts +++ b/packages/allure-mocha/test/fixtures/specs/owner.ts @@ -1,8 +1,7 @@ import { suite, test } from "mocha-typescript"; import { MochaAllureInterface } from "../../../src/MochaAllureInterface"; -// @ts-ignore -const allure: MochaAllureInterface = global.allure; +declare const allure: MochaAllureInterface; @suite class Owner { diff --git a/packages/allure-mocha/test/fixtures/specs/parameter.ts b/packages/allure-mocha/test/fixtures/specs/parameter.ts index 3d4215372..67168316a 100644 --- a/packages/allure-mocha/test/fixtures/specs/parameter.ts +++ b/packages/allure-mocha/test/fixtures/specs/parameter.ts @@ -1,8 +1,7 @@ import { suite, test } from "mocha-typescript"; import { MochaAllureInterface } from "../../../src/MochaAllureInterface"; -// @ts-ignore -const allure: MochaAllureInterface = global.allure; +declare const allure: MochaAllureInterface; @suite class Parameter { diff --git a/packages/allure-mocha/test/fixtures/specs/severity.ts b/packages/allure-mocha/test/fixtures/specs/severity.ts index f568f55e5..3f57b134e 100644 --- a/packages/allure-mocha/test/fixtures/specs/severity.ts +++ b/packages/allure-mocha/test/fixtures/specs/severity.ts @@ -2,8 +2,7 @@ import { Severity } from "allure-js-commons"; import { suite, test } from "mocha-typescript"; import { MochaAllureInterface } from "../../../src/MochaAllureInterface"; -// @ts-ignore -const allure: MochaAllureInterface = global.allure; +declare const allure: MochaAllureInterface; @suite class SeveritySubSuite { diff --git a/packages/allure-mocha/test/fixtures/specs/step.ts b/packages/allure-mocha/test/fixtures/specs/step.ts index a78f8d893..3cd7b815c 100644 --- a/packages/allure-mocha/test/fixtures/specs/step.ts +++ b/packages/allure-mocha/test/fixtures/specs/step.ts @@ -1,8 +1,7 @@ import { suite, test } from "mocha-typescript"; import { MochaAllureInterface } from "../../../src/MochaAllureInterface"; -// @ts-ignore -const allure: MochaAllureInterface = global.allure; +declare const allure: MochaAllureInterface; @suite class Step { diff --git a/packages/allure-mocha/test/fixtures/specs/story.ts b/packages/allure-mocha/test/fixtures/specs/story.ts index 3c4ab49e7..3a57c2e4d 100644 --- a/packages/allure-mocha/test/fixtures/specs/story.ts +++ b/packages/allure-mocha/test/fixtures/specs/story.ts @@ -1,8 +1,7 @@ import { suite, test } from "mocha-typescript"; import { MochaAllureInterface } from "../../../src/MochaAllureInterface"; -// @ts-ignore -const allure: MochaAllureInterface = global.allure; +declare const allure: MochaAllureInterface; @suite class Story { diff --git a/packages/allure-mocha/test/fixtures/specs/tag.ts b/packages/allure-mocha/test/fixtures/specs/tag.ts index 2eee2662c..87d1595ed 100644 --- a/packages/allure-mocha/test/fixtures/specs/tag.ts +++ b/packages/allure-mocha/test/fixtures/specs/tag.ts @@ -1,8 +1,7 @@ import { suite, test } from "mocha-typescript"; import { MochaAllureInterface } from "../../../src/MochaAllureInterface"; -// @ts-ignore -const allure: MochaAllureInterface = global.allure; +declare const allure: MochaAllureInterface; @suite class Tag { diff --git a/packages/allure-mocha/test/specs/attachment.ts b/packages/allure-mocha/test/specs/attachment.ts deleted file mode 100644 index 4b106e292..000000000 --- a/packages/allure-mocha/test/specs/attachment.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { Status } from "allure-js-commons"; -import { expect } from "chai"; -import { suite } from "mocha-typescript"; -import { - cleanResults, - findAttachments, - findStepAttachments, - findSteps, - findTest, - runTests, - whenResultsAppeared -} from "../utils/index"; - -@suite -class AttachmentsSuite { - before() { - cleanResults(); - runTests("attachment"); - } - - @test - shouldHaveAttachments() { - const testName = "shouldAssignAttachments"; - return whenResultsAppeared().then(() => { - expect(findTest("AttachmentSubSuite")).not.eq(undefined); - expect(findTest(testName).status).eq(Status.PASSED); - - const testAttachments = findAttachments(testName); - expect(testAttachments).length(2); - expect(testAttachments[0].name).eq("test attachment 1"); - expect(testAttachments[0].type).eq("text/plain"); - expect(testAttachments[1].name).eq("test attachment 2"); - expect(testAttachments[1].type).eq("application/json"); - - expect(findSteps(testName)).length(2); - - let stepAttachments = findStepAttachments(testName, "step 1"); - expect(stepAttachments).length(2); - expect(stepAttachments[0].name).eq("step 1 attachment 1"); - expect(stepAttachments[0].type).eq("text/plain"); - expect(stepAttachments[1].name).eq("step 1 attachment 2"); - expect(stepAttachments[1].type).eq("text/plain"); - - stepAttachments = findStepAttachments(testName, "step 2"); - expect(stepAttachments).length(2); - expect(stepAttachments[0].name).eq("step 2 attachment 1"); - expect(stepAttachments[0].type).eq("text/plain"); - expect(stepAttachments[1].name).eq("step 2 attachment 2"); - expect(stepAttachments[1].type).eq("text/plain"); - }); - } -} diff --git a/packages/allure-mocha/test/specs/attachments.ts b/packages/allure-mocha/test/specs/attachments.ts new file mode 100644 index 000000000..d562ad095 --- /dev/null +++ b/packages/allure-mocha/test/specs/attachments.ts @@ -0,0 +1,38 @@ +import { Status } from "allure-js-commons"; +import { expect } from "chai"; +import { suite } from "mocha-typescript"; +import { runTests } from "../utils"; + +@suite +class AttachmentsSuite { + @test + async shouldHaveAttachments() { + const writerStub = await runTests("attachment"); + + expect(writerStub.groups.find(test => test.name === "AttachmentSubSuite")).not.eq(undefined); + + const test = writerStub.getTestByName("shouldAssignAttachments"); + expect(test).not.eq(undefined); + expect(test.status).eq(Status.PASSED); + + expect(test.attachments).length(2); + expect(test.attachments[0].name).eq("test attachment 1"); + expect(test.attachments[0].type).eq("text/plain"); + expect(test.attachments[1].name).eq("test attachment 2"); + expect(test.attachments[1].type).eq("application/json"); + + expect(test.steps).length(2); + + expect(test.steps[0].attachments).length(2); + expect(test.steps[0].attachments[0].name).eq("step 1 attachment 1"); + expect(test.steps[0].attachments[0].type).eq("text/plain"); + expect(test.steps[0].attachments[1].name).eq("step 1 attachment 2"); + expect(test.steps[0].attachments[1].type).eq("text/plain"); + + expect(test.steps[1].attachments).length(2); + expect(test.steps[1].attachments[0].name).eq("step 2 attachment 1"); + expect(test.steps[1].attachments[0].type).eq("text/plain"); + expect(test.steps[1].attachments[1].name).eq("step 2 attachment 2"); + expect(test.steps[1].attachments[1].type).eq("text/plain"); + } +} diff --git a/packages/allure-mocha/test/specs/common.ts b/packages/allure-mocha/test/specs/common.ts index 2cc87327b..2b3b1e58f 100644 --- a/packages/allure-mocha/test/specs/common.ts +++ b/packages/allure-mocha/test/specs/common.ts @@ -1,31 +1,24 @@ import { Status } from "allure-js-commons"; import { expect } from "chai"; import { suite } from "mocha-typescript"; -import { cleanResults, findTest, runTests, whenResultsAppeared } from "../utils/index"; +import { runTests } from "../utils"; @suite class CommonSuite { - before() { - cleanResults(); - runTests("common"); - } - @test - shouldHavePassedAndFailedTests() { - return whenResultsAppeared().then(() => { - expect(findTest("Common")).not.eq(undefined); - expect(findTest("shouldPass").status).eq(Status.PASSED); - expect(findTest("shouldFail").status).eq(Status.FAILED); + async shouldHavePassedAndFailedTests() { + const writerStub = await runTests("common"); + expect(writerStub.getTestByName("shouldPass").status).eq(Status.PASSED); + expect(writerStub.getTestByName("shouldFail").status).eq(Status.FAILED); - const brokenTest = findTest("shouldBreak"); - expect(brokenTest.status).eq(Status.BROKEN); - expect(brokenTest.statusDetails.message).eq("Broken"); - expect(brokenTest.statusDetails.trace).not.eq(undefined); + const brokenTest = writerStub.getTestByName("shouldBreak"); + expect(brokenTest.status).eq(Status.BROKEN); + expect(brokenTest.statusDetails.message).eq("Broken"); + expect(brokenTest.statusDetails.trace).not.eq(undefined); - const skippedTest = findTest("shouldSkip"); - expect(skippedTest.status).eq(Status.SKIPPED); - expect(skippedTest.statusDetails.message).eq("Test ignored"); - expect(skippedTest.statusDetails.trace).eq(undefined); - }); + const skippedTest = writerStub.getTestByName("shouldSkip"); + expect(skippedTest.status).eq(Status.SKIPPED); + expect(skippedTest.statusDetails.message).eq("Test ignored"); + expect(skippedTest.statusDetails.trace).eq(undefined); } } diff --git a/packages/allure-mocha/test/specs/description.ts b/packages/allure-mocha/test/specs/description.ts index f5a35adf6..14906721b 100644 --- a/packages/allure-mocha/test/specs/description.ts +++ b/packages/allure-mocha/test/specs/description.ts @@ -1,24 +1,16 @@ import { Status } from "allure-js-commons"; import { expect } from "chai"; import { suite } from "mocha-typescript"; -import { cleanResults, findTest, runTests, whenResultsAppeared } from "../utils/index"; +import { runTests } from "../utils"; @suite class DescriptionSuite { - before() { - cleanResults(); - runTests("description"); - } - @test - shouldHaveDescription() { - const testName = "shouldAssignDescription"; - return whenResultsAppeared().then(() => { - expect(findTest("Description")).not.eq(undefined); + async shouldHaveDescription() { + const writerStub = await runTests("description"); - const currentTest = findTest(testName); - expect(currentTest.status).eq(Status.PASSED); - expect(currentTest.description).eq("Test description"); - }); + const currentTest = writerStub.getTestByName("shouldAssignDescription"); + expect(currentTest.status).eq(Status.PASSED); + expect(currentTest.description).eq("Test description"); } } diff --git a/packages/allure-mocha/test/specs/feature.ts b/packages/allure-mocha/test/specs/feature.ts index 8b9ab48ad..20d788125 100644 --- a/packages/allure-mocha/test/specs/feature.ts +++ b/packages/allure-mocha/test/specs/feature.ts @@ -1,23 +1,16 @@ import { Status } from "allure-js-commons"; import { expect } from "chai"; import { suite } from "mocha-typescript"; -import { cleanResults, findLabel, findTest, runTests, whenResultsAppeared } from "../utils/index"; +import { findLabel, runTests } from "../utils"; @suite class FeatureSuite { - before() { - cleanResults(); - runTests("feature"); - } - @test - shouldHaveFeature() { - const testName = "shouldAssignFeature"; - return whenResultsAppeared().then(() => { - expect(findTest("Feature")).not.eq(undefined); + async shouldHaveFeature() { + const writerStub = await runTests("feature"); + const test = writerStub.getTestByName("shouldAssignFeature"); - expect(findTest(testName).status).eq(Status.PASSED); - expect(findLabel(testName, "feature").value).eq("Login"); - }); + expect(test.status).eq(Status.PASSED); + expect(findLabel(test, "feature")!.value).eq("Login"); } } diff --git a/packages/allure-mocha/test/specs/globalInfo.ts b/packages/allure-mocha/test/specs/globalInfo.ts index 207ee6715..20cfd0195 100644 --- a/packages/allure-mocha/test/specs/globalInfo.ts +++ b/packages/allure-mocha/test/specs/globalInfo.ts @@ -1,52 +1,29 @@ import { expect } from "chai"; import { suite } from "mocha-typescript"; -import { - cleanResults, - findFiles, - readProperties, - readResults, - runTests, - whenResultsAppeared -} from "../utils/index"; +import { runTests } from "../utils"; @suite class GlobalInfoSuite { - before() { - cleanResults(); - runTests("globalInfo"); - } - @test - shouldHaveGlobalInfo() { - return whenResultsAppeared().then(() => { - expect(readResults("categories.json").pop()).deep.eq([ - { - name: "Sad tests", - messageRegex: ".*Sad.*", - matchedStatuses: ["failed"] - }, - { - name: "Infrastructure problems", - messageRegex: ".*Error.*", - matchedStatuses: ["broken"] - } - ]); - - expect(readResults("executor.json").pop()).deep.eq({ - name: "Jenkins", - type: "jenkins", - url: "http://example.org", - buildOrder: 11, - buildName: "allure-report_deploy#11", - buildUrl: "http://example.org/build#11", - reportUrl: "http://example.org/build#11/AllureReport", - reportName: "Demo allure report" - }); + async shouldHaveGlobalInfo() { + const writerStub = await runTests("globalInfo"); + expect(writerStub.categories).deep.eq([ + { + name: "Sad tests", + messageRegex: ".*Sad.*", + matchedStatuses: ["failed"] + }, + { + name: "Infrastructure problems", + messageRegex: ".*Error.*", + matchedStatuses: ["broken"] + } + ]); - const environment = readProperties(findFiles("environment.properties").pop()); - expect(environment.get("Browser")).eq("chrome"); - expect(environment.get("GitHub")).eq("https://github.com/sskorol"); - expect(environment.get("Author")).eq("Sergey Korol"); + expect(writerStub.envInfo).deep.eq({ + Browser: "chrome", + GitHub: "https://github.com/sskorol", + Author: "Sergey Korol" }); } } diff --git a/packages/allure-mocha/test/specs/issueAndTms.ts b/packages/allure-mocha/test/specs/issueAndTms.ts index 6cc121a68..c7444bc32 100644 --- a/packages/allure-mocha/test/specs/issueAndTms.ts +++ b/packages/allure-mocha/test/specs/issueAndTms.ts @@ -1,36 +1,20 @@ import { Status } from "allure-js-commons"; import { expect } from "chai"; import { suite } from "mocha-typescript"; -import { - cleanResults, - findLabel, - findLinks, - findTest, - runTests, - whenResultsAppeared -} from "../utils"; +import { runTests } from "../utils"; @suite class IssueAndTmsSuite { - before() { - cleanResults(); - runTests("issueAndTms"); - } - @test - shouldHaveIssueAndTmsLinks() { - const testName = "shouldAssignIssueAndTms"; - return whenResultsAppeared().then(results => { - expect(findTest("IssueAndTms")).not.eq(undefined); + async shouldHaveIssueAndTmsLinks() { + const writerStub = await runTests("issueAndTms"); + const test = writerStub.getTestByName("shouldAssignIssueAndTms"); - const links = findLinks(testName); - expect(links).length(2); - expect(findTest(testName).status).eq(Status.PASSED); + expect(test.links).length(2); + expect(test.status).eq(Status.PASSED); - expect(links.map(link => link.name)).contains("1", "2"); - expect(links.map(link => link.url)) - .contains("http://localhost/issues/1", "http://localhost/issues/2"); - expect(links.map(link => link.type)).contains("issue", "tms"); - }); + expect(test.links.map(link => link.name)).contains("1", "2"); + expect(test.links.map(link => link.url)).contains("http://localhost/issues/1", "http://localhost/issues/2"); + expect(test.links.map(link => link.type)).contains("issue", "tms"); } } diff --git a/packages/allure-mocha/test/specs/owner.ts b/packages/allure-mocha/test/specs/owner.ts index 49ebf6a39..1017635bd 100644 --- a/packages/allure-mocha/test/specs/owner.ts +++ b/packages/allure-mocha/test/specs/owner.ts @@ -1,23 +1,16 @@ import { Status } from "allure-js-commons"; import { expect } from "chai"; import { suite } from "mocha-typescript"; -import { cleanResults, findLabel, findTest, runTests, whenResultsAppeared } from "../utils/index"; +import { findLabel, runTests } from "../utils"; @suite class OwnerSuite { - before() { - cleanResults(); - runTests("owner"); - } - @test - shouldHaveOwner() { - const testName = "shouldAssignOwner"; - return whenResultsAppeared().then(() => { - expect(findTest("Owner")).not.eq(undefined); + async shouldHaveOwner() { + const writerStub = await runTests("owner"); + const test = writerStub.getTestByName("shouldAssignOwner"); - expect(findTest(testName).status).eq(Status.PASSED); - expect(findLabel(testName, "owner").value).eq("sskorol"); - }); + expect(test.status).eq(Status.PASSED); + expect(findLabel(test, "owner")!.value).eq("sskorol"); } } diff --git a/packages/allure-mocha/test/specs/parameter.ts b/packages/allure-mocha/test/specs/parameter.ts index cbaa55e02..99b235586 100644 --- a/packages/allure-mocha/test/specs/parameter.ts +++ b/packages/allure-mocha/test/specs/parameter.ts @@ -1,29 +1,16 @@ import { Status } from "allure-js-commons"; import { expect } from "chai"; import { suite } from "mocha-typescript"; -import { - cleanResults, - findParameter, - findTest, - runTests, - whenResultsAppeared -} from "../utils"; +import { findParameter, runTests } from "../utils"; @suite class ParameterSuite { - before() { - cleanResults(); - runTests("parameter"); - } - @test - shouldHaveParameter() { - const testName = "shouldAssignParameter"; - return whenResultsAppeared().then(() => { - expect(findTest("Parameter")).not.eq(undefined); + async shouldHaveParameter() { + const writerStub = await runTests("parameter"); + const test = writerStub.getTestByName("shouldAssignParameter"); - expect(findTest(testName).status).eq(Status.PASSED); - expect(findParameter(testName, "key").value).eq("value"); - }); + expect(test.status).eq(Status.PASSED); + expect(findParameter(test, "key").value).eq("value"); } } diff --git a/packages/allure-mocha/test/specs/severity.ts b/packages/allure-mocha/test/specs/severity.ts index d2cfe112b..e084505d8 100644 --- a/packages/allure-mocha/test/specs/severity.ts +++ b/packages/allure-mocha/test/specs/severity.ts @@ -1,23 +1,15 @@ import { Severity, Status } from "allure-js-commons"; import { expect } from "chai"; import { suite } from "mocha-typescript"; -import { cleanResults, findLabel, findTest, runTests, whenResultsAppeared } from "../utils/index"; +import { findLabel, runTests } from "../utils"; @suite class SeveritySuite { - before() { - cleanResults(); - runTests("severity"); - } - @test - shouldHaveSeverity() { - const testName = "shouldAssignSeverity"; - return whenResultsAppeared().then(() => { - expect(findTest("SeveritySubSuite")).not.eq(undefined); - - expect(findTest(testName).status).eq(Status.PASSED); - expect(findLabel(testName, "severity").value).eq(Severity.BLOCKER); - }); + async shouldHaveSeverity() { + const writerStub = await runTests("severity"); + const test = writerStub.getTestByName("shouldAssignSeverity"); + expect(test.status).eq(Status.PASSED); + expect(findLabel(test, "severity")!.value).eq(Severity.BLOCKER); } } diff --git a/packages/allure-mocha/test/specs/step.ts b/packages/allure-mocha/test/specs/step.ts index 83c5526cd..91afa3e58 100644 --- a/packages/allure-mocha/test/specs/step.ts +++ b/packages/allure-mocha/test/specs/step.ts @@ -1,38 +1,29 @@ import { Status } from "allure-js-commons"; import { expect } from "chai"; import { suite } from "mocha-typescript"; -import { cleanResults, findSteps, findTest, runTests, whenResultsAppeared } from "../utils/index"; +import { runTests } from "../utils"; @suite class StepSuite { - before() { - cleanResults(); - runTests("step"); - } - @test - shouldHaveSteps() { - const test1Name = "shouldAddSteps"; - const test2Name = "shouldAddInnerStep"; - return whenResultsAppeared().then(() => { - expect(findTest("Step")).not.eq(undefined); - expect(findTest(test1Name).status).eq(Status.PASSED); - expect(findTest(test2Name).status).eq(Status.PASSED); + async shouldHaveSteps() { + const writerStub = await runTests("step"); + const test1 = writerStub.getTestByName("shouldAddSteps"); + const test2 = writerStub.getTestByName("shouldAddInnerStep"); + expect(test1.status).eq(Status.PASSED); + expect(test2.status).eq(Status.PASSED); - let steps = findSteps(test1Name); - expect(steps.map(step => step.name)).contains("Step 1", "Step 2"); - expect(steps.map(step => step.status)).contains(Status.PASSED); + expect(test1.steps.map(step => step.name)).contains("Step 1", "Step 2"); + expect(test1.steps.map(step => step.status)).contains(Status.PASSED); - steps = findSteps(test2Name); - expect(steps.map(step => step.name)).contains("Step 3"); - expect(steps.map(step => step.status)).contains(Status.PASSED); + expect(test2.steps.map(step => step.name)).contains("Step 3"); + expect(test2.steps.map(step => step.status)).contains(Status.PASSED); - const subStep = steps - .map(step => step.steps) - .pop() - .pop(); - expect(subStep.name).eq("Step 4"); - expect(subStep.status).eq(Status.PASSED); - }); + const subStep = test2.steps + .map(step => step.steps) + .pop()! + .pop(); + expect(subStep!.name).eq("Step 4"); + expect(subStep!.status).eq(Status.PASSED); } } diff --git a/packages/allure-mocha/test/specs/story.ts b/packages/allure-mocha/test/specs/story.ts index 16ed65174..4fd516f41 100644 --- a/packages/allure-mocha/test/specs/story.ts +++ b/packages/allure-mocha/test/specs/story.ts @@ -1,23 +1,16 @@ -import { Status } from "allure-js-commons"; +import { Severity, Status } from "allure-js-commons"; import { expect } from "chai"; import { suite } from "mocha-typescript"; -import { cleanResults, findLabel, findTest, runTests, whenResultsAppeared } from "../utils/index"; +import { findLabel, runTests } from "../utils"; @suite class StorySuite { - before() { - cleanResults(); - runTests("story"); - } - @test - shouldHaveStories() { - const testName = "shouldAssignStory"; - return whenResultsAppeared().then(() => { - expect(findTest("Story")).not.eq(undefined); + async shouldHaveStories() { + const writerStub = await runTests("story"); + const test = writerStub.getTestByName("shouldAssignStory"); - expect(findTest(testName).status).eq(Status.PASSED); - expect(findLabel(testName, "story").value).eq("Common story"); - }); + expect(test.status).eq(Status.PASSED); + expect(findLabel(test, "story")!.value).eq("Common story"); } } diff --git a/packages/allure-mocha/test/specs/tag.ts b/packages/allure-mocha/test/specs/tag.ts index 03bb607f5..3c50c1585 100644 --- a/packages/allure-mocha/test/specs/tag.ts +++ b/packages/allure-mocha/test/specs/tag.ts @@ -1,23 +1,16 @@ import { Status } from "allure-js-commons"; import { expect } from "chai"; import { suite } from "mocha-typescript"; -import { cleanResults, findLabel, findTest, runTests, whenResultsAppeared } from "../utils/index"; +import { findLabel, runTests } from "../utils"; @suite class TagSuite { - before() { - cleanResults(); - runTests("tag"); - } - @test - shouldHaveTags() { - const testName = "shouldAssignTag"; - return whenResultsAppeared().then(() => { - expect(findTest("Tag")).not.eq(undefined); + async shouldHaveTags() { + const writerStub = await runTests("tag"); + const test = writerStub.getTestByName("shouldAssignTag"); - expect(findTest(testName).status).eq(Status.PASSED); - expect(findLabel(testName, "tag").value).eq("smoke"); - }); + expect(test.status).eq(Status.PASSED); + expect(findLabel(test, "tag")!.value).eq("smoke"); } } diff --git a/packages/allure-mocha/test/utils/index.ts b/packages/allure-mocha/test/utils/index.ts index 3f1d79555..a955686f8 100644 --- a/packages/allure-mocha/test/utils/index.ts +++ b/packages/allure-mocha/test/utils/index.ts @@ -1,98 +1,27 @@ import jetpack = require("fs-jetpack"); import * as path from "path"; import Mocha from "mocha"; -import propertiesReader from "properties-reader"; import { MochaAllureReporter } from "../../src/MochaAllureReporter"; +import { InMemoryAllureWriter, TestResult } from "allure-js-commons"; const testDir = "./dist/test/fixtures/specs"; -const resultsDir = path.join(__dirname, "../../../out/allure-results"); -const resultsPollingInterval: number = 10; -let results: any[] = []; - -export function runTests(...specs: string[]) { +export function runTests(...specs: string[]): Promise { + const writer = new InMemoryAllureWriter(); const mocha = new Mocha(); + mocha.reporter(MochaAllureReporter, { writer }); assignSpecs(mocha, specs); - mocha - .reporter(MochaAllureReporter, { "resultsDir": "./out/allure-results" }) - .run(failures => { - process.exitCode = failures ? 1 : 0; - }) - .on("end", () => (results = readResults("*.json"))); -} - -export function whenResultsAppeared() { - return new Promise((resolve, reject) => { - (function waitForResults() { - if (results.length > 0) { - return resolve(); - } - setTimeout(waitForResults, resultsPollingInterval); - })(); + return new Promise(resolve => { + mocha.run(() => resolve(writer)); }); } -export function findTest(name: string): any { - return results.find(result => result.name === name); -} - -export function findStatusDetails(testName: string, key: string): any { - return findTest(testName).statusDetails[key]; -} - -export function findLabel(testName: string, labelName: string): any { - return findTest(testName).labels.find(label => label.name === labelName); -} - -export function findParameter(testName: string, parameterName: string): any { - return findParameters(testName).find(parameter => parameter.name === parameterName); -} - -export function findParameters(testName: string): any[] { - return findTest(testName).parameters; -} - -export function findAttachment(testName: string, attachmentName: string): any { - return findAttachments(testName).find(attachment => attachment.name === attachmentName); -} - -export function findAttachments(testName: string): any[] { - return findTest(testName).attachments; -} - -export function findStepAttachments(testName: string, stepName: string): any[] { - return findStep(testName, stepName).attachments; -} - -export function findLinks(testName: string): any[] { - return findTest(testName).links; -} - -export function findSteps(testName: string): any[] { - return findTest(testName).steps; -} - -export function findStep(testName: string, stepName: string): any { - return findTest(testName).steps.find(step => step.name === stepName); -} - -export function cleanResults() { - results = []; -} - -export function readResults(pattern: string): any[] { - return findFiles(pattern).map(file => require(file)); -} - -export function findFiles(pattern: string): any[] { - return jetpack - .dir(resultsDir) - .find({ matching: pattern }) - .map(fileName => path.join(resultsDir, fileName)); +export function findLabel(test: TestResult, labelName: string) { + return test.labels.find(label => label.name === labelName); } -export function readProperties(fileName: string) { - return propertiesReader(fileName); +export function findParameter(test: TestResult, parameterName: string): any { + return test.parameters.find(parameter => parameter.name === parameterName); } function assignSpecs(mocha: Mocha, specs: string[]) { diff --git a/packages/allure-mocha/tsconfig.json b/packages/allure-mocha/tsconfig.json index 6aa124ecf..0b1c731e8 100644 --- a/packages/allure-mocha/tsconfig.json +++ b/packages/allure-mocha/tsconfig.json @@ -8,7 +8,6 @@ "compilerOptions": { "emitDecoratorMetadata": true, "experimentalDecorators": true, - "noImplicitAny": false, "types": [ "node", "mocha",