Skip to content

Commit

Permalink
fix: categories support (#16)
Browse files Browse the repository at this point in the history
* fix: plain string error handling
  • Loading branch information
noomorph authored Nov 11, 2023
1 parent 77f4c5a commit 2ef4190
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ There are two built-in categories for test results:
This doesn't mean that you can't have categories for 🟢 _passed_ or ⚪ _skipped_ tests, but usually it makes more sense
to develop a comprehensive taxonomy for various failure types.

For example, you can also distinguish between **Visual regression**, **Timeout error** and as many others as you need.
For example, you can also distinguish between **Snapshot mismatches**, **Timeouts** and as many others as you need.

```js title="jest.config.js"
const { Status } = require('jest-allure2-reporter');

/**
* @type {import('@jest/types').Config.InitialOptions}
*/
Expand All @@ -52,17 +54,15 @@ module.exports = {
// highlight-start
categories: [
{
name: "Visual Regression",
// optional `matchedStatuses`
matchedStatuses: ["failed"],
// optional `messageRegex`
messageRegex: "Expected .*to match a screenshot.*",
// optional `traceRegex`
traceRegex: ".*visual-regression.*",
name: 'Snapshot mismatches',
matchedStatuses: [Status.FAILED],
messageRegex: /.*\btoMatch(?:[A-Za-z]+)?Snapshot\b.*/,
},
{
name: 'Timeouts',
matchedStatuses: [Status.BROKEN],
messageRegex: /.*Exceeded timeout of.*/,
},
//
// ... other categories
//
],
// highlight-end
}
Expand Down
13 changes: 13 additions & 0 deletions e2e/configs/default.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
// eslint-disable-next-line node/no-extraneous-require,@typescript-eslint/no-var-requires,import/no-extraneous-dependencies
const _ = require('lodash');
const { Status } = require('jest-allure2-reporter');
const PRESET = process.env.ALLURE_PRESET ?? 'default';

/** @type {import('jest-allure2-reporter').ReporterOptions} */
const jestAllure2ReporterOptions = {
resultsDir: `allure-results/${PRESET}`,
categories: [
{
name: 'Snapshot mismatches',
matchedStatuses: [Status.FAILED],
messageRegex: /.*\btoMatch(?:[A-Za-z]+)?Snapshot\b.*/,
},
{
name: 'Timeouts',
matchedStatuses: [Status.BROKEN],
messageRegex: /.*Exceeded timeout of.*/,
},
],
environment: (context) => {
return ({
'version.node': process.version,
Expand Down
8 changes: 4 additions & 4 deletions src/environment/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ export function WithAllure2<E extends WithEmitter>(
};

if (event.error) {
metadata.statusDetails = {
message: event.error.message,
trace: event.error.stack,
};
const message = event.error.message ?? `${event.error}`;
const trace = event.error.stack;

metadata.statusDetails = { message, trace };
}

state.currentMetadata.assign(PREFIX, metadata);
Expand Down
7 changes: 7 additions & 0 deletions src/options/defaultCategories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Category } from '@noomorph/allure-js-commons';
import { Status } from '@noomorph/allure-js-commons';

export const DEFAULT_CATEGORIES: Category[] = [
{ name: 'Product defects', matchedStatuses: [Status.FAILED] },
{ name: 'Test defects', matchedStatuses: [Status.BROKEN] },
];
3 changes: 2 additions & 1 deletion src/options/defaultOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { stripStatusDetails } from './stripStatusDetails';
import { aggregateLabelCustomizers } from './aggregateLabelCustomizers';
import { resolvePlugins } from './resolvePlugins';
import { composeExtractors } from './composeExtractors';
import { DEFAULT_CATEGORIES } from './defaultCategories';

const identity = <T>(context: ExtractorContext<T>) => context.value;
const last = <T>(context: ExtractorContext<T[]>) => context.value?.at(-1);
Expand Down Expand Up @@ -94,7 +95,7 @@ export function defaultOptions(context: PluginContext): ReporterConfig {
testStep,
environment: identity,
executor: identity,
categories: identity,
categories: () => DEFAULT_CATEGORIES,
plugins: resolvePlugins(context, [
plugins.jsdoc,
plugins.manifest,
Expand Down

0 comments on commit 2ef4190

Please sign in to comment.