Skip to content

Commit

Permalink
wip: code blocks POC
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Sep 1, 2023
1 parent ef5f55d commit 778d745
Show file tree
Hide file tree
Showing 39 changed files with 260 additions and 1,066 deletions.
12 changes: 12 additions & 0 deletions packages/e2e/default/src/simple.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
describe('Simple suite', () => {
beforeAll(() => {
console.log('beforeAll');
});

it('should pass', () => {
expect(true).toBe(true);
});

it('should also pass', () => {
expect(true).toBe(true);
});

afterEach(() => {
console.log('afterEach');
});
});
6 changes: 3 additions & 3 deletions packages/library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@types/glob": "^8.0.0",
"@types/lodash": "^4.14.186",
"@types/node": "^14.18.46",
"@types/rimraf": "^3.0.2",
"@types/rimraf": "^4.0.5",
"fs-extra": "^10.1.0",
"glob": "^8.0.3",
"lodash": "^4.17.21",
Expand All @@ -56,9 +56,9 @@
"dependencies": {
"@noomorph/allure-js-commons": "^2.3.0",
"ci-info": "^3.8.0",
"jest-metadata": "^1.0.0-beta.11",
"jest-metadata": "^1.0.0-beta.15",
"pkg-up": "^3.1.0",
"rimraf": "^3.0.2",
"rimraf": "^4.3.1",
"strip-ansi": "^6.0.0"
},
"prettier": "@wix/prettier-config-jest-allure2-reporter",
Expand Down
148 changes: 99 additions & 49 deletions packages/library/src/JestAllure2Reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ import type {
ReporterOnStartOptions,
Test,
TestCaseResult,
TestContext,
TestResult,
} from '@jest/reporters';
import { query, JestMetadataReporter } from 'jest-metadata/reporter';
import {JestMetadataReporter, query} from 'jest-metadata/reporter';
import rimraf from 'rimraf';
import { AllureRuntime } from '@noomorph/allure-js-commons';
import {
AllureRuntime,
Stage,
Status,
} from '@noomorph/allure-js-commons';

import type {
AllureTestCaseMetadata,
AllureTestStepMetadata,
GlobalExtractorContext,
ReporterOptions,
AllureRunMetadata,
AllureTestCaseMetadata,
} from './ReporterOptions';
import { resolveOptions } from './options';
} from './options/ReporterOptions';
import {resolveOptions} from './options';

const NAMESPACE = 'allure2' as const;
const ns = (key?: string) => (key ? ['allure2', key] : ['allure2']);

export class JestAllure2Reporter extends JestMetadataReporter {
private readonly _allure: AllureRuntime;
Expand All @@ -35,6 +37,10 @@ export class JestAllure2Reporter extends JestMetadataReporter {
super(globalConfig, options);

this._options = resolveOptions(options);
if (this._options.overwrite) {
rimraf.sync(this._options.resultsDir);
}

this._allure = new AllureRuntime({
resultsDir: this._options.resultsDir,
});
Expand All @@ -52,9 +58,6 @@ export class JestAllure2Reporter extends JestMetadataReporter {
): Promise<void> {
await super.onRunStart(aggregatedResult, options);

if (this._options.overwrite) {
rimraf.sync(this._options.resultsDir);
}

const environment = this._options.environment(this._globalContext);
if (environment) {
Expand All @@ -70,49 +73,96 @@ export class JestAllure2Reporter extends JestMetadataReporter {
onTestFileStart(test: Test) {
super.onTestFileStart(test);

const run = query.test(test)!.get(NAMESPACE, {}) as AllureRunMetadata;
run.startedAt = Date.now();
run.threadId = '1';
}

onTestCaseStart(_test: unknown, _testCaseStartInfo: unknown) {
super.onTestCaseStart(_test, _testCaseStartInfo);

const testEntry = query.testCaseResult(testCaseResult)!;
const metadata = testEntry.get(NAMESPACE, {}) as AllureTestCaseMetadata;

metadata.start = Date.now();
query.test(test)!.assign(ns(), {
start: Date.now(),
threadId: '1',
});
}

onTestCaseResult(test: Test, testCaseResult: TestCaseResult) {
super.onTestCaseResult(test, testCaseResult);

const testEntry = query.testCaseResult(testCaseResult)!;
const metadata = testEntry.get(NAMESPACE, {}) as AllureTestCaseMetadata;

metadata.identifier = testEntry.id;
metadata.start =
metadata.start ?? Date.now() - (testCaseResult.duration ?? 0);
metadata.stop = Date.now();
}

onTestFileResult(
test: Test,
testResult: TestResult,
aggregatedResult: AggregatedResult,
): Promise<void> | void {
super.onTestFileResult(test, testResult, aggregatedResult);

const run = query.test(test)!.get(NAMESPACE, {}) as AllureRunMetadata;
const fileContext = this._testRunContext.getFileContext(test.path)!;
fileContext.handleTestFileResult(testResult);
}

async onRunComplete(
testContexts: Set<TestContext>,
aggregatedResult: AggregatedResult,
): Promise<void> {
await super.onRunComplete(testContexts, aggregatedResult);
const lastInvocation = testEntry.lastInvocation;
if (lastInvocation) {
const userTestMetadata = {
...(testEntry.get(ns()) as AllureTestCaseMetadata),
...(lastInvocation.get(ns()) as AllureTestCaseMetadata),
} as AllureTestStepMetadata;

const group = this._allure.startGroup(testCaseResult.fullName);

const test = group.startTest(testCaseResult.title, userTestMetadata.start);
test.fullName = testCaseResult.fullName;

const allInvocations = [
[() => group.addBefore(), lastInvocation.beforeAll],
[() => group.addBefore(), lastInvocation.before],
[() => test.startStep('test'), lastInvocation.fn ? [lastInvocation.fn] : []],
[() => group.addAfter(), lastInvocation.after],
[() => group.addAfter(), lastInvocation.afterAll],
] as const;

for (const [createGroup, invocations] of allInvocations) {
for (const invocation of invocations) {
const definition = invocation.definition.get(
ns(),
)! as AllureTestStepMetadata;
const executable = invocation.get(ns())! as AllureTestStepMetadata;
const userMetadata: AllureTestStepMetadata = {
...userTestMetadata,
...definition,
...executable,
};

const step = createGroup();
step.stage = userMetadata.stage ?? Stage.SCHEDULED;
step.status = userMetadata.status ?? step.status;
if (userMetadata.start != null) {
step.wrappedItem.start = userMetadata.start;
}
if (userMetadata.stop != null) {
step.wrappedItem.stop = userMetadata.stop;
}
if (userMetadata.statusDetails != null) {
step.statusDetails = userMetadata.statusDetails;
}
if (userMetadata.name != null) {
step.name = userMetadata.name;
}
}
}

let code = '';
const attachCodeAs = (type: string, content: unknown) => {
if (content) {
code += `${type}(${content})\n`;
}
};
for (const block of lastInvocation.beforeAll) {
attachCodeAs('beforeAll', block.definition.get(ns('code')));
}
for (const block of lastInvocation.before) {
attachCodeAs('beforeEach', block.definition.get(ns('code')));
}
attachCodeAs('test', lastInvocation.entry.get(ns('code')));
debugger;
for (const block of lastInvocation.after) {
attachCodeAs('afterEach', block.definition.get(ns('code')));
}
for (const block of lastInvocation.afterAll) {
attachCodeAs('afterAll', block.definition.get(ns('code')));
}

test.descriptionHtml = '<details><summary>Test code</summary><pre><code>\n' + code + '\n</code></pre></details>';
test.status = testCaseResult.status === 'passed' ? Status.PASSED : Status.FAILED;
test.stage = Stage.FINISHED;
test.statusDetails = {
message: testCaseResult.failureMessages.join('\n'),
};
test.endTest(userTestMetadata.stop);
group.endGroup();
}
}

getLastError(): Error | void {
Expand Down
40 changes: 0 additions & 40 deletions packages/library/src/__tests__/environment.ts

This file was deleted.

Loading

0 comments on commit 778d745

Please sign in to comment.