Skip to content
This repository has been archived by the owner on Aug 28, 2022. It is now read-only.

Commit

Permalink
feat(project wide): added support for the global "allure" report help…
Browse files Browse the repository at this point in the history
…er. Added better types support
  • Loading branch information
ryparker committed Jul 6, 2020
1 parent e37d843 commit 3cc3adc
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 48 deletions.
79 changes: 45 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# jest-circus-allure-reporter
# Jest Circus Allure Environment

![Lint-Build-Test-Publish](https://github.com/ryparker/jest-circus-allure-reporter/workflows/Lint-Build-Test-Publish/badge.svg)
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo)
Expand All @@ -18,15 +18,15 @@ A Jest Circus environment for Allure reporting.

## :rocket: Quick start

1. Add this package
1. **Add this package**

```shell
yarn add --dev jest-circus-allure-environment
```

2. Update `jest.config.js`
2. **Update `jest.config.js`**

See the [official Jest docs](https://jestjs.io/docs/en/configuration#testenvironment-string) for more details.
_See the [testEnvironment docs](https://jestjs.io/docs/en/configuration#testenvironment-string) for configuration details._

```JSON
{
Expand All @@ -35,43 +35,54 @@ See the [official Jest docs](https://jestjs.io/docs/en/configuration#testenviron
}
```

3. Run tests
3. **Run tests**

```shell
yarn test
```

4. Open the Allure report
4. **Open the Allure report**

```shell
allure serve ./allure-results
```

## :recycle: Lifecycle events

Updated list available [here](https://github.com/facebook/jest/blob/master/packages/jest-types/src/Circus.ts)

**Bold** items are async test events

**_Italic_** items are synchronous test events

0. **_error_**
1. constructor
2. setup Fn
3. **setup**
4. **_add_hook_**
5. **_start_describe_definition_**
6. **_add_test_**
7. **_finish_describe_definition_**
8. **run_start** / **test_skip** / **test_todo**
9. **run_describe_start**
10. **test_start**
11. **hook_start**
12. **hook_success** / **hook_failure**
13. **test_fn_start**
14. **test_fn_success** / **test_fn_failure** / **error**
15. **test_done**
16. **run_describe_finish**
17. **run_finish**
18. **teardown**
19. teardown Fn
## Allure reporting in your tests

To provide more information in your reports you can call allure from your tests. For types support you'll need some [additional configuration](#typescript--intellisense-setup).

```js
// simple.test.js

test('2 + 3 is 5', () => {
allure.epic('Implement addition functionality')
allure.tag('Accounting')

expect(2 + 3).toBe(5)
})
```

## Typescript & Intellisense setup

1. **Support Typescript & intellisense by loading the module into your `jest.setup.js` file**



```js
// jest.setup.js

import 'jest-circus-allure-environment' // Typescript or ESM
require('jest-circus-allure-environment') // CommonJS
```

2. **Make sure your `jest.setup.js` file is properly configured.**

_See the [setupFilesAfterEnv docs](https://jestjs.io/docs/en/configuration#setupfilesafterenv-array) for configuration details._

```js
// jest.config.js

{
"setupFilesAfterEnv": ["./jest.setup.js"]
}
```
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"files": [
"dist"
],
"main": "./dist",
"main": "dist",
"types": "dist/index.d.ts",
"scripts": {
"build": "rimraf dist && tsc",
"test": "rimraf allure-results && yarn build && jest",
Expand Down Expand Up @@ -85,4 +86,4 @@
"collect",
"analyze"
]
}
}
17 changes: 15 additions & 2 deletions src/allure-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
ExecutableItemWrapper,
Status,
StepInterface,
isPromise
isPromise,
LabelName
} from 'allure-js-commons';

import type AllureReporter from './allure-reporter';
Expand Down Expand Up @@ -37,6 +38,18 @@ export default class JestAllureInterface extends Allure {
this.reporter.currentExecutable = executable;
}

public label(name: string, value: string) {
this.currentTest.addLabel(name, value);
}

public tag(tag: string) {
this.currentTest.addLabel(LabelName.TAG, tag);
}

public epic(epic: string) {
this.label(LabelName.EPIC, epic);
}

public step<T>(name: string, body: (step: StepInterface) => any): any {
const wrappedStep = this.startStep(name);
let result;
Expand Down Expand Up @@ -64,7 +77,7 @@ export default class JestAllureInterface extends Allure {
return result;
}

logStep(
public logStep(
name: string,
status: Status,
attachments?: Array<{ name: string; content: string; type: ContentType }>
Expand Down
12 changes: 9 additions & 3 deletions src/allure-node-environment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Config, Circus} from '@jest/types';
import NodeEnvironment from 'jest-environment-node';
import NodeEnvironment = require('jest-environment-node');
import type {EnvironmentContext} from '@jest/environment';
import AllureReporter from './allure-reporter';
import {AllureRuntime, IAllureConfig} from 'allure-js-commons';
Expand Down Expand Up @@ -28,15 +28,18 @@ export default class AllureNodeEnvironment extends NodeEnvironment {
this.reporter = new AllureReporter(new AllureRuntime(allureConfig));

this.global.allure = this.reporter.getImplementation();
console.log(' this.docblockPragmas:', this.docblockPragmas);

if (this.docblockPragmas === {}) {
console.log(this.docblockPragmas);
}
}

async setup() {
return super.setup();
}

async teardown() {
this.global.allure = null;
// This.global.allure = null;

return super.teardown();
}
Expand Down Expand Up @@ -83,6 +86,7 @@ export default class AllureNodeEnvironment extends NodeEnvironment {
this.reporter.passTestCase(event.test, state, this.testPath);
break;
case 'test_fn_failure':
console.log('TEST_FN_FAILURE:', event.error);
this.reporter.failTestCase(event.test, state, this.testPath, event.error ?? event.test.asyncError);
break;
case 'test_done':
Expand All @@ -95,8 +99,10 @@ export default class AllureNodeEnvironment extends NodeEnvironment {
case 'teardown':
break;
case 'error':
console.log('ERROR EVENT:', event.error);
break;
default:
console.log('unhandled event:', event);
break;
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/allure-reporter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import JestAllureInterface from './allure-interface';
import type * as jest from '@jest/types';
import stripAnsi from 'strip-ansi';
import prettier from 'prettier/standalone';
import parser from 'prettier/parser-typescript';
import stripAnsi = require('strip-ansi');
import prettier = require('prettier/standalone');
import parser = require('prettier/parser-typescript');
import {
AllureGroup,
AllureRuntime,
Expand Down Expand Up @@ -112,7 +112,7 @@ export default class AllureReporter {
this.currentTest.addLabel(LabelName.THREAD, state.parentProcess.env.JEST_WORKER_ID);
}

const pathsArray = testPath.split('/').slice(2);
const pathsArray = testPath.split('/');

const [parentSuite, suite, ...subSuites] = pathsArray;

Expand Down Expand Up @@ -159,6 +159,7 @@ export default class AllureReporter {
}
}

console.log('ERROR FOUND:', error);
const status = error.matcherResult ? Status.FAILED : Status.BROKEN;
const message = stripAnsi(error.message);
const trace = stripAnsi(error.stack).replace(message, '');
Expand Down
11 changes: 9 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export {default} from './allure-node-environment';

import AllureNodeEnvironment from './allure-node-environment';
module.exports = AllureNodeEnvironment;
import type AllureNodeEnvironment from './allure-node-environment';
export type {AllureNodeEnvironment};

import type JestAllureInterface from './allure-interface';

export type {JestAllureInterface};

declare global {
const allure: JestAllureInterface;
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"isolatedModules": true,
"importsNotUsedAsValues": "error",
"esModuleInterop": false,
"noImplicitReturns": true,
"strict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"types": ["node", "jest", "allure-js-commons"],
"outDir": "dist/"
Expand Down

0 comments on commit 3cc3adc

Please sign in to comment.