Skip to content

Commit

Permalink
Implement importable runtime API
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris committed Aug 26, 2019
1 parent cbbb449 commit 6d41304
Show file tree
Hide file tree
Showing 25 changed files with 82 additions and 82 deletions.
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
},
"devDependencies": {
"@types/node": "^12.7.1",
"@typescript-eslint/eslint-plugin": "^2.0.0",
"@typescript-eslint/parser": "^2.0.0",
"allure-commandline": "^2.9.0",
"eslint": "^5.16.0",
"lerna": "^3.13.3",
"npm-run-all": "^4.1.5",
"nyc": "^14.0.0",
"rimraf": "^3.0.0",
"typescript": "^3.4.4",
"eslint": "^5.16.0",
"@typescript-eslint/eslint-plugin": "^2.0.0",
"@typescript-eslint/parser": "^2.0.0",
"nyc": "^14.0.0"
"ts-node": "^8.3.0",
"typescript": "^3.4.4"
}
}
16 changes: 10 additions & 6 deletions packages/allure-mocha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ Note that it's recommended to add the following dependencies as well for better
Either add **allure-mocha** into **mocha.opts**:

```text
--ui mocha-typescript
--require source-map-support/register
--reporter allure-mocha
```

Expand All @@ -34,12 +32,18 @@ Or pass the same value via commandline / scripts:
mocha -R allure-mocha
```

Now you can access a global **allure** object from within your project:
If you want to provide extra information, such as steps and attachments, import the `allure` object
into your code:

```typescript
import { MochaAllureInterface } from 'allure-mocha';
```javascript
// es-modules
import { allure } from 'allure-mocha/runtime';
// or commonjs
const { allure } = require('allure-mocha/runtime');

declare const allure: MochaAllureInterface;
it('is a test', () => {
allure.epic('Some info');
});
```

## Decorators Support
Expand Down
5 changes: 0 additions & 5 deletions packages/allure-mocha/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/allure-mocha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"build": "npm run lint && npm run compile",
"generate-report": "allure generate ./out/allure-results -o ./out/allure-report --clean",
"allure-report": "allure serve ./out/allure-results",
"test": "nyc node dist/test/runner",
"test": "nyc ts-node --project test/tsconfig.json test/runner.ts",
"coverage": "codecov",
"lint": "eslint ./src ./test ./index.ts --ext .ts",
"lint": "eslint ./src ./test --ext .ts",
"lint-fix": "eslint ./src ./test ./index.ts --ext .ts --fix"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/allure-mocha/runtime.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { allure } from "./dist/MochaAllureReporter";
15 changes: 15 additions & 0 deletions packages/allure-mocha/runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Typescript does not handle re-exported values correctly
// https://github.com/microsoft/TypeScript/issues/12522
// We write a manual code here that represents the actual behavior of the standard.
// Based on the Babel output:
// https://babeljs.io/repl#?code_lz=KYDwDg9gTgLgBAbwIYBsUFcrAL5wGZQQC2cA5AHQD0AJgJYDOMl9UAxpQLISsAWSAgmkzAASsEixgUUkA
"use strict";

const reporter = require("./dist/MochaAllureReporter");

Object.defineProperty(exports, "__esModule", { value: true });
Object.defineProperty(module.exports, "allure", {
get() {
return reporter.allure;
}
});
2 changes: 1 addition & 1 deletion packages/allure-mocha/src/AllureReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class AllureReporter {
}

this.currentTest = this.currentSuite.startTest(test.title);
this.currentTest.fullName = test.name;
this.currentTest.fullName = test.title;
this.currentTest.historyId = createHash("md5")
.update(test.fullTitle())
.digest("hex");
Expand Down
21 changes: 12 additions & 9 deletions packages/allure-mocha/src/MochaAllureReporter.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import * as Mocha from "mocha";
import { AllureRuntime, IAllureConfig } from "allure-js-commons";
import { AllureReporter } from "./AllureReporter";
import { MochaAllureInterface } from "./MochaAllureInterface";

export let allure: MochaAllureInterface;

export class MochaAllureReporter extends Mocha.reporters.Base {
private allure: AllureReporter;
private coreReporter: AllureReporter;

constructor(readonly runner: Mocha.Runner, readonly opts: Mocha.MochaOptions) {
super(runner, opts);

const allureConfig: IAllureConfig = { resultsDir: "allure-results", ...opts.reporterOptions };

this.allure = new AllureReporter(new AllureRuntime(allureConfig));
this.coreReporter = new AllureReporter(new AllureRuntime(allureConfig));

(global as any).allure = this.allure.getInterface();
allure = this.coreReporter.getInterface();

this.runner
.on("suite", this.onSuite.bind(this))
Expand All @@ -24,26 +27,26 @@ export class MochaAllureReporter extends Mocha.reporters.Base {
}

private onSuite(suite: Mocha.Suite) {
this.allure.startSuite(suite.fullTitle());
this.coreReporter.startSuite(suite.fullTitle());
}

private onSuiteEnd() {
this.allure.endSuite();
this.coreReporter.endSuite();
}

private onTest(test: Mocha.Test) {
this.allure.startCase(test);
this.coreReporter.startCase(test);
}

private onPassed(test: Mocha.Test) {
this.allure.passTestCase(test);
this.coreReporter.passTestCase(test);
}

private onFailed(test: Mocha.Test, error: Error) {
this.allure.failTestCase(test, error);
this.coreReporter.failTestCase(test, error);
}

private onPending(test: Mocha.Test) {
this.allure.pendingTestCase(test);
this.coreReporter.pendingTestCase(test);
}
}
3 changes: 3 additions & 0 deletions packages/allure-mocha/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { MochaAllureReporter } from "./MochaAllureReporter";

export = MochaAllureReporter;
4 changes: 1 addition & 3 deletions packages/allure-mocha/test/fixtures/specs/attachment.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { ContentType } from "allure-js-commons";
import { suite, test } from "mocha-typescript";
import { MochaAllureInterface } from "../../../src/MochaAllureInterface";

declare const allure: MochaAllureInterface;
import { allure } from "../../../runtime";

@suite
class AttachmentSubSuite {
Expand Down
4 changes: 1 addition & 3 deletions packages/allure-mocha/test/fixtures/specs/description.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { suite, test } from "mocha-typescript";
import { MochaAllureInterface } from "../../../src/MochaAllureInterface";

declare const allure: MochaAllureInterface;
import { allure } from "../../../runtime";

@suite
class Description {
Expand Down
4 changes: 1 addition & 3 deletions packages/allure-mocha/test/fixtures/specs/feature.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { suite, test } from "mocha-typescript";
import { MochaAllureInterface } from "../../../src/MochaAllureInterface";

declare const allure: MochaAllureInterface;
import { allure } from "../../../runtime";

@suite
class Feature {
Expand Down
4 changes: 1 addition & 3 deletions packages/allure-mocha/test/fixtures/specs/globalInfo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Status } from "allure-js-commons";
import { suite, test } from "mocha-typescript";
import { MochaAllureInterface } from "../../../src/MochaAllureInterface";

declare const allure: MochaAllureInterface;
import { allure } from "../../../runtime";

@suite
class GlobalInfo {
Expand Down
6 changes: 1 addition & 5 deletions packages/allure-mocha/test/fixtures/specs/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { MochaAllureInterface } from "../../..";
import { ContentType } from "allure-js-commons";
import { expect } from "chai";

declare const allure: MochaAllureInterface;
import { allure } from "../../../runtime";

describe("hooks test", () => {
describe("before fails", () => {
Expand Down Expand Up @@ -50,5 +48,3 @@ describe("hooks test", () => {
});
});
});


4 changes: 1 addition & 3 deletions packages/allure-mocha/test/fixtures/specs/issueAndTms.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { suite, test } from "mocha-typescript";
import { MochaAllureInterface } from "../../../src/MochaAllureInterface";

declare const allure: MochaAllureInterface;
import { allure } from "../../../runtime";

@suite
class IssueAndTms {
Expand Down
4 changes: 1 addition & 3 deletions packages/allure-mocha/test/fixtures/specs/owner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { suite, test } from "mocha-typescript";
import { MochaAllureInterface } from "../../../src/MochaAllureInterface";

declare const allure: MochaAllureInterface;
import { allure } from "../../../runtime";

@suite
class Owner {
Expand Down
4 changes: 1 addition & 3 deletions packages/allure-mocha/test/fixtures/specs/parameter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { suite, test } from "mocha-typescript";
import { MochaAllureInterface } from "../../../src/MochaAllureInterface";

declare const allure: MochaAllureInterface;
import { allure } from "../../../runtime";

@suite
class Parameter {
Expand Down
4 changes: 1 addition & 3 deletions packages/allure-mocha/test/fixtures/specs/severity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Severity } from "allure-js-commons";
import { suite, test } from "mocha-typescript";
import { MochaAllureInterface } from "../../../src/MochaAllureInterface";

declare const allure: MochaAllureInterface;
import { allure } from "../../../runtime";

@suite
class SeveritySubSuite {
Expand Down
4 changes: 1 addition & 3 deletions packages/allure-mocha/test/fixtures/specs/step.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { suite, test } from "mocha-typescript";
import { MochaAllureInterface } from "../../../src/MochaAllureInterface";

declare const allure: MochaAllureInterface;
import { allure } from "../../../runtime";

@suite
class Step {
Expand Down
4 changes: 1 addition & 3 deletions packages/allure-mocha/test/fixtures/specs/story.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { suite, test } from "mocha-typescript";
import { MochaAllureInterface } from "../../../src/MochaAllureInterface";

declare const allure: MochaAllureInterface;
import { allure } from "../../../runtime";

@suite
class Story {
Expand Down
4 changes: 1 addition & 3 deletions packages/allure-mocha/test/fixtures/specs/tag.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { suite, test } from "mocha-typescript";
import { MochaAllureInterface } from "../../../src/MochaAllureInterface";

declare const allure: MochaAllureInterface;
import { allure } from "../../../runtime";

@suite
class Tag {
Expand Down
3 changes: 2 additions & 1 deletion packages/allure-mocha/test/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const mocha = new Mocha({
ui: "mocha-typescript",
timeout: 16000,
reporter: "mocha-multi",
ignoreLeaks: false,
reporterOptions: {
list: "-",
[require.resolve("../")]: {
Expand All @@ -25,6 +26,6 @@ const mocha = new Mocha({
}
});

glob.sync("dist/test/specs/**/*.js").forEach(file => mocha.addFile(file));
glob.sync("test/specs/**/*.ts").forEach(file => mocha.addFile(file));

mocha.run(failures => process.exit(failures === 0 ? 0 : 1));
12 changes: 12 additions & 0 deletions packages/allure-mocha/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"rootDir": "."
},
"exclude": [],
"include": [
"**/*.ts"
]
}
6 changes: 3 additions & 3 deletions packages/allure-mocha/test/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import jetpack = require("fs-jetpack");
import * as path from "path";
import Mocha from "mocha";
import { MochaAllureReporter } from "../../src/MochaAllureReporter";
import MochaAllureReporter from "../..";
import { InMemoryAllureWriter, TestResult } from "allure-js-commons";

const testDir = "./dist/test/fixtures/specs";
const testDir = "./test/fixtures/specs";

export function runTests(...specs: string[]): Promise<InMemoryAllureWriter> {
const writer = new InMemoryAllureWriter();
Expand All @@ -28,7 +28,7 @@ export function findParameter(test: TestResult, parameterName: string): any {
function assignSpecs(mocha: Mocha, specs: string[]) {
jetpack
.dir(testDir)
.find({ matching: specs.map(spec => `${spec}.js`) })
.find({ matching: specs.map(spec => `${spec}.ts`) })
.forEach(file => {
const testPath = path.resolve(testDir, file);
// remove the test from node_modules cache, so it can be executed again
Expand Down
15 changes: 3 additions & 12 deletions packages/allure-mocha/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
{
"extends": "../../tsconfig.json",
"include": [
"./src/**/*",
"./test/**/*.ts",
"index.ts"
],
"exclude": ["test/**", "runtime.d.ts"],
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"types": [
"node",
"mocha",
"chai"
],
"types": [],
"rootDir": "./src",
"outDir": "./dist"
}
}

0 comments on commit 6d41304

Please sign in to comment.