Skip to content

Commit

Permalink
fix: status and statusDetails logic (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph authored Feb 16, 2024
1 parent 3167180 commit 0e198f0
Show file tree
Hide file tree
Showing 26 changed files with 344 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import LoginHelper from '../../../../utils/LoginHelper';
import {allure} from "../../../../../../src/api";
import {$Tag, $Epic, $Feature, $Story, allure} from 'jest-allure2-reporter/api';

$Tag('client');
$Epic('Authentication');
Expand Down Expand Up @@ -38,6 +38,7 @@ describe('Login screen', () => {
});

it('should show error on short or invalid password format', () => {
allure.status('failed', { message: 'The password is too short' });
// ...
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`POST /forgot-password should return 401 if user is not found 1`] = `
{
"code": 401,
"seed": 0.06426173461501827,
}
`;
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { $Tag, $Epic, $Feature, $Story } from 'jest-allure2-reporter/api';

$Tag('server');
$Epic('Authentication');
$Feature('Restore account');
describe('POST /forgot-password', () => {
$Story('Validation');
it('should return 401 if user is not found', () => {
// ...
expect({ code: 401, seed: Math.random() }).toMatchSnapshot();
});

$Story('Happy path');
Expand Down
40 changes: 40 additions & 0 deletions e2e/src/programmatic/grouping/statuses.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { allure } from 'jest-allure2-reporter/api';

const dummyTest = () => expect(true).toBe(true);
const passingAssertion = () => expect(2 + 2).toBe(4);
const failingAssertion = () => expect(2 + 2).toBe(5);
const failingSnapshot = () => expect({ seed: Math.random() }).toMatchSnapshot();
const brokenAssertion = () => { throw new Error('This assertion is broken'); };

type Fn = () => any;

describe('Status tests', () => {
describe('Simple', () => {
test('passed', passingAssertion);
test('failed assertion', failingAssertion);
test('failed snapshot', failingSnapshot);
test('broken', brokenAssertion);
test.skip('skipped', passingAssertion);
test.todo('todo');
});

describe.each([
['passing', passingAssertion],
['failed assertion in a ', failingAssertion],
['failed snapshot in a ', failingSnapshot],
['broken', brokenAssertion],
])('Status override in a %s', (_parentSuite, callback) => {
describe.each([
['test', (callback: Fn) => (test('', callback), void 0)],
['beforeAll hook', (callback: Fn) => (beforeAll(callback), test('', dummyTest), void 0)],
['beforeEach hook', (callback: Fn) => (beforeEach(callback), test('', dummyTest), void 0)],
['afterEach hook', (callback: Fn) => (afterEach(callback), test('', dummyTest), void 0)],
['afterAll hook', (callback: Fn) => (afterAll(callback), test('', dummyTest), void 0)],
])('%s', (_suite, hook) => {
hook(function () {
allure.status('unknown', { message: 'Custom message', trace: 'Custom trace' });
callback();
});
});
});
});
16 changes: 8 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,14 +476,6 @@ declare module 'jest-allure2-reporter' {
* @example ['steps', '0']
*/
currentStep?: AllureTestStepPath;
/**
* Source code of the test case, test step or a hook.
*/
sourceCode?: string;
/**
* Location (file, line, column) of the test case, test step or a hook.
*/
sourceLocation?: AllureTestItemSourceLocation;
/**
* Markdown description of the test case or test file, or plain text description of a test step.
*/
Expand All @@ -492,6 +484,14 @@ declare module 'jest-allure2-reporter' {
* Key-value pairs to disambiguate test cases or to provide additional information.
*/
parameters?: Parameter[];
/**
* Source code of the test case, test step or a hook.
*/
sourceCode?: string;
/**
* Location (file, line, column) of the test case, test step or a hook.
*/
sourceLocation?: AllureTestItemSourceLocation;
/**
* Indicates test item execution progress.
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"dependencies": {
"@noomorph/allure-js-commons": "^2.3.0",
"ci-info": "^3.8.0",
"jest-metadata": "^1.3.1",
"jest-metadata": "^1.4.1",
"lodash.snakecase": "^4.1.1",
"node-fetch": "^2.6.7",
"pkg-up": "^3.1.0",
Expand Down
113 changes: 54 additions & 59 deletions src/environment/listener.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type { Circus } from '@jest/types';
import type {
AllureTestCaseMetadata,
AllureTestStepMetadata,
Status,
} from 'jest-allure2-reporter';
import type {
EnvironmentListenerFn,
TestEnvironmentCircusEvent,
Expand All @@ -13,6 +8,7 @@ import * as StackTrace from 'stacktrace-js';

import * as api from '../api';
import realm from '../realms';
import { getStatusDetails, isJestAssertionError } from '../utils';

const listener: EnvironmentListenerFn = (context) => {
context.testEvents
Expand All @@ -33,7 +29,7 @@ const listener: EnvironmentListenerFn = (context) => {
.on('add_hook', addSourceLocation)
.on('add_hook', addHookType)
.on('add_test', addSourceLocation)
.on('test_start', executableStart)
.on('test_start', testStart)
.on('test_todo', testSkip)
.on('test_skip', testSkip)
.on('test_done', testDone)
Expand Down Expand Up @@ -111,80 +107,79 @@ function addSourceCode({ event }: TestEnvironmentCircusEvent) {
}

// eslint-disable-next-line no-empty-pattern
function executableStart({}: TestEnvironmentCircusEvent) {
const metadata: AllureTestStepMetadata = {
start: Date.now(),
function executableStart(
_event: TestEnvironmentCircusEvent<
Circus.Event & { name: 'hook_start' | 'test_fn_start' }
>,
) {
realm.runtimeContext.getCurrentMetadata().assign({
stage: 'running',
};

realm.runtimeContext.getCurrentMetadata().assign(metadata);
start: Date.now(),
});
}

function executableFailure({
event,
event: { error },
}: TestEnvironmentCircusEvent<
Circus.Event & { name: 'test_fn_failure' | 'hook_failure' }
>) {
const metadata: AllureTestStepMetadata = {
stop: Date.now(),
realm.runtimeContext.getCurrentMetadata().assign({
stage: 'interrupted',
status: 'failed',
};

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

metadata.statusDetails = { message, trace };
}

realm.runtimeContext.getCurrentMetadata().assign(metadata);
status: isJestAssertionError(error) ? 'failed' : 'broken',
statusDetails: getStatusDetails(error),
stop: Date.now(),
});
}

// eslint-disable-next-line no-empty-pattern
function executableSuccess({}: TestEnvironmentCircusEvent) {
const metadata: AllureTestStepMetadata = {
stop: Date.now(),
function executableSuccess(
_event: TestEnvironmentCircusEvent<
Circus.Event & { name: 'test_fn_success' | 'hook_success' }
>,
) {
realm.runtimeContext.getCurrentMetadata().assign({
stage: 'finished',
status: 'passed',
};
stop: Date.now(),
});
}

realm.runtimeContext.getCurrentMetadata().assign(metadata);
function testStart(
_event: TestEnvironmentCircusEvent<Circus.Event & { name: 'test_start' }>,
) {
realm.runtimeContext.getCurrentMetadata().assign({
start: Date.now(),
});
}

function testSkip() {
const metadata: AllureTestCaseMetadata = {
function testSkip(
_event: TestEnvironmentCircusEvent<
Circus.Event & { name: 'test_skip' | 'test_todo' }
>,
) {
realm.runtimeContext.getCurrentMetadata().assign({
stop: Date.now(),
stage: 'pending',
status: 'skipped',
};

realm.runtimeContext.getCurrentMetadata().assign(metadata);
});
}

function testDone({
event,
}: TestEnvironmentCircusEvent<Circus.Event & { name: 'test_done' }>) {
const hasErrors = event.test.errors.length > 0;
const errorStatus: Status = event.test.errors.some((errors) => {
return Array.isArray(errors)
? errors.some(isMatcherError)
: isMatcherError(errors);
})
? 'failed'
: 'broken';

const metadata: AllureTestCaseMetadata = {
stop: Date.now(),
stage: hasErrors ? 'interrupted' : 'finished',
status: hasErrors ? errorStatus : 'passed',
};

realm.runtimeContext.getCurrentMetadata().assign(metadata);
}
const current = realm.runtimeContext.getCurrentMetadata();

if (event.test.errors.length > 0) {
const hasFailedAssertions = event.test.errors.some((errors) => {
return Array.isArray(errors)
? errors.some(isJestAssertionError)
: isJestAssertionError(errors);
});

current.assign({
status: hasFailedAssertions ? 'failed' : 'broken',
});
}

function isMatcherError(error: any) {
return Boolean(error?.matcherResult);
realm.runtimeContext.getCurrentMetadata().assign({
stop: Date.now(),
});
}

export default listener;
6 changes: 4 additions & 2 deletions src/metadata/MetadataSquasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
AllureTestStepMetadata,
} from 'jest-allure2-reporter';

import { getStart, getStop } from './utils';
import { getStage, getStart, getStatusAndDetails, getStop } from './utils';
import { PREFIX } from './constants';
import {
mergeTestCaseMetadata,
Expand Down Expand Up @@ -42,10 +42,12 @@ export class MetadataSquasher {
resolveTestStep,
);
const steps = result.steps ?? [];
const stage = getStage(metadata);

return {
...result,

...getStatusAndDetails(metadata),
stage,
start: getStart(metadata),
stop: getStop(metadata),
steps: [...befores, ...steps, ...afters],
Expand Down
4 changes: 3 additions & 1 deletion src/metadata/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export const PREFIX = 'allure2' as const;

export const START = [PREFIX, 'start'] as const;

export const STOP = [PREFIX, 'stop'] as const;
export const STAGE = [PREFIX, 'stage'] as const;
export const STATUS = [PREFIX, 'status'] as const;
export const STATUS_DETAILS = [PREFIX, 'statusDetails'] as const;

export const CURRENT_STEP = [PREFIX, 'currentStep'] as const;
export const DESCRIPTION = [PREFIX, 'description'] as const;
Expand Down
5 changes: 5 additions & 0 deletions src/metadata/proxies/AllureMetadataProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export class AllureMetadataProxy<T = unknown> {
return this;
}

defaults(values: Partial<T>): this {
this.$metadata.defaults(this.$localPath(), values);
return this;
}

protected $localPath(key?: keyof T, ...innerKeys: string[]): string[] {
const allKeys = key ? [key as string, ...innerKeys] : innerKeys;
return [PREFIX, ...allKeys];
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/proxies/AllureTestItemMetadataProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class AllureTestItemMetadataProxy<
}

$stopStep(): this {
const currentStep = this.$metadata.get(CURRENT_STEP, []) as string[];
const currentStep = this.$metadata.get(CURRENT_STEP, [] as string[]);
this.$metadata.set(CURRENT_STEP, currentStep.slice(0, -2));
return this;
}
Expand Down
20 changes: 20 additions & 0 deletions src/metadata/utils/getStage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { TestInvocationMetadata } from 'jest-metadata';

import { STAGE } from '../constants';

export const getStage = (testInvocation: TestInvocationMetadata) => {
let finished: boolean | undefined;
for (const invocation of testInvocation.allInvocations()) {
finished ??= true;

const stage = invocation.get(STAGE);
if (stage === 'interrupted') {
return 'interrupted';
}
if (stage !== 'finished') {
finished = false;
}
}

return finished ? 'finished' : undefined;
};
Loading

0 comments on commit 0e198f0

Please sign in to comment.