Skip to content

Commit

Permalink
fix: status and statusDetails logic
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Feb 15, 2024
1 parent 3167180 commit 9b3ce67
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 62 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
108 changes: 53 additions & 55 deletions src/environment/listener.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import type { Circus } from '@jest/types';
import type {
AllureTestCaseMetadata,
AllureTestStepMetadata,
Status,
} from 'jest-allure2-reporter';
import type { Status, StatusDetails } from 'jest-allure2-reporter';
import type {
EnvironmentListenerFn,
TestEnvironmentCircusEvent,
Expand Down Expand Up @@ -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,76 +107,78 @@ 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,
}: TestEnvironmentCircusEvent<
Circus.Event & { name: 'test_fn_failure' | 'hook_failure' }
>) {
const metadata: AllureTestStepMetadata = {
stop: Date.now(),
stage: 'interrupted',
status: 'failed',
};
const current = realm.runtimeContext.getCurrentMetadata();
let status: Status = 'broken';
let statusDetails: StatusDetails | undefined;

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

metadata.statusDetails = { message, trace };
status = isMatcherError(event.error) ? 'failed' : 'broken';
statusDetails = {
message: event.error.message ?? `${event.error}`,
trace: event.error.stack,
};
}

realm.runtimeContext.getCurrentMetadata().assign(metadata);
}
// TODO: switch to defaults
current.assign({
status,
statusDetails,
});

// eslint-disable-next-line no-empty-pattern
function executableSuccess({}: TestEnvironmentCircusEvent) {
const metadata: AllureTestStepMetadata = {
current.assign({
stage: 'interrupted',
stop: Date.now(),
stage: 'finished',
status: 'passed',
};

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

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

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

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 = {
function testSkip(
_event: TestEnvironmentCircusEvent<
Circus.Event & { name: 'test_skip' | 'test_todo' }
>,
) {
realm.runtimeContext.getCurrentMetadata().assign({
stop: Date.now(),
stage: hasErrors ? 'interrupted' : 'finished',
status: hasErrors ? errorStatus : 'passed',
};
stage: 'pending',
});
}

realm.runtimeContext.getCurrentMetadata().assign(metadata);
function testDone(
_event: TestEnvironmentCircusEvent<Circus.Event & { name: 'test_done' }>,
) {
realm.runtimeContext.getCurrentMetadata().set('stop', Date.now());
}

function isMatcherError(error: any) {

Check warning on line 184 in src/environment/listener.ts

View workflow job for this annotation

GitHub Actions / Sanity

Unexpected any. Specify a different type
Expand Down
5 changes: 3 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, getStatus, getStop } from './utils';
import { PREFIX } from './constants';
import {
mergeTestCaseMetadata,
Expand Down Expand Up @@ -45,7 +45,8 @@ export class MetadataSquasher {

return {
...result,

...getStatus(metadata),
stage: getStage(metadata),
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
14 changes: 14 additions & 0 deletions src/metadata/utils/getStage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { TestInvocationMetadata } from 'jest-metadata';
import type { Stage } from 'jest-allure2-reporter';

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

export const getStage = (testInvocation: TestInvocationMetadata) => {
for (const invocation of testInvocation.allInvocations()) {
if (invocation.get(STAGE) === 'interrupted') {
return 'interrupted';
}
}

return testInvocation.get(STAGE) as Stage | undefined;
};
23 changes: 23 additions & 0 deletions src/metadata/utils/getStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { TestInvocationMetadata } from 'jest-metadata';
import type { Status, StatusDetails } from 'jest-allure2-reporter';

import { STATUS, STATUS_DETAILS } from '../constants';

export const getStatus = (testInvocation: TestInvocationMetadata) => {
const items = [...testInvocation.allInvocations(), testInvocation].reverse();
for (const item of items) {
const status = item.get(STATUS) as Status | undefined;
if (status !== undefined) {
const statusDetails = item.get(STATUS_DETAILS) as
| StatusDetails
| undefined;

return {
status,
statusDetails,
};
}
}

return;
};
2 changes: 2 additions & 0 deletions src/metadata/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './getStage';
export * from './getStart';
export * from './getStatus';
export * from './getStop';
3 changes: 2 additions & 1 deletion src/options/default-options/testCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export const testCase: ResolvedTestCaseCustomizer = {
testCaseMetadata.start ??
(testCaseMetadata.stop ?? Date.now()) - (testCase.duration ?? 0),
stop: ({ testCaseMetadata }) => testCaseMetadata.stop ?? Date.now(),
stage: ({ testCase }) => getTestCaseStage(testCase),
stage: ({ testCase, testCaseMetadata }) =>
testCaseMetadata.stage ?? getTestCaseStage(testCase),
status: ({ testCase, testCaseMetadata }) =>
testCaseMetadata.status ?? getTestCaseStatus(testCase),
statusDetails: ({ testCase, testCaseMetadata }) =>
Expand Down
4 changes: 3 additions & 1 deletion src/realms/AllureRealm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export class AllureRealm {
'config',
);
if (!config) {
throw new Error('Shared reporter config is not defined');
throw new Error(
"Cannot receive jest-allure2-reporter's config from the parent process. Have you set up Jest test environment correctly?",
);
}

return config as SharedReporterConfig;
Expand Down

0 comments on commit 9b3ce67

Please sign in to comment.