From 3cc3adc70364a645b3d1051fa5a9210f68decc06 Mon Sep 17 00:00:00 2001 From: Ryan Parker Date: Mon, 6 Jul 2020 13:57:30 -0500 Subject: [PATCH] feat(project wide): added support for the global "allure" report helper. Added better types support --- README.md | 79 +++++++++++++++++++--------------- package.json | 5 ++- src/allure-interface.ts | 17 +++++++- src/allure-node-environment.ts | 12 ++++-- src/allure-reporter.ts | 9 ++-- src/index.ts | 11 ++++- tsconfig.json | 3 +- 7 files changed, 88 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index ff44831..034ad98 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 { @@ -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"] +} +``` diff --git a/package.json b/package.json index 4dca55a..0ce0628 100644 --- a/package.json +++ b/package.json @@ -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", @@ -85,4 +86,4 @@ "collect", "analyze" ] -} +} \ No newline at end of file diff --git a/src/allure-interface.ts b/src/allure-interface.ts index bee8b1a..3250008 100644 --- a/src/allure-interface.ts +++ b/src/allure-interface.ts @@ -7,7 +7,8 @@ import { ExecutableItemWrapper, Status, StepInterface, - isPromise + isPromise, + LabelName } from 'allure-js-commons'; import type AllureReporter from './allure-reporter'; @@ -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(name: string, body: (step: StepInterface) => any): any { const wrappedStep = this.startStep(name); let result; @@ -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 }> diff --git a/src/allure-node-environment.ts b/src/allure-node-environment.ts index 8f7e22a..3dab406 100644 --- a/src/allure-node-environment.ts +++ b/src/allure-node-environment.ts @@ -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'; @@ -28,7 +28,10 @@ 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() { @@ -36,7 +39,7 @@ export default class AllureNodeEnvironment extends NodeEnvironment { } async teardown() { - this.global.allure = null; + // This.global.allure = null; return super.teardown(); } @@ -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': @@ -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; } } diff --git a/src/allure-reporter.ts b/src/allure-reporter.ts index 2fd0bd0..1df0ad8 100644 --- a/src/allure-reporter.ts +++ b/src/allure-reporter.ts @@ -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, @@ -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; @@ -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, ''); diff --git a/src/index.ts b/src/index.ts index bd24833..2ee8abb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; +} diff --git a/tsconfig.json b/tsconfig.json index 434fc31..e0450d6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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/"