Skip to content

Commit

Permalink
feat: integrate jest-environment-emit
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Nov 25, 2023
1 parent d932406 commit ffb0a84
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 164 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"dependencies": {
"@noomorph/allure-js-commons": "^2.3.0",
"ci-info": "^3.8.0",
"jest-metadata": "^1.2.1",
"jest-metadata": "1.3.0-beta.1",
"lodash.snakecase": "^4.1.1",
"node-fetch": "^2.6.7",
"pkg-up": "^3.1.0",
Expand Down
159 changes: 0 additions & 159 deletions src/environment/decorator.ts

This file was deleted.

8 changes: 6 additions & 2 deletions src/environment/jsdom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import JestEnvironmentJsdom from 'jest-metadata/environment-jsdom';

import { WithAllure2 } from './decorator';
import listener from './listener';

export const TestEnvironment = JestEnvironmentJsdom.derive(
listener,
'WithAllure',
);

export const TestEnvironment = WithAllure2(JestEnvironmentJsdom);
export default TestEnvironment;
147 changes: 147 additions & 0 deletions src/environment/listener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import type { Circus } from '@jest/types';
import { state } from 'jest-metadata';
import { Stage, Status } from '@noomorph/allure-js-commons';
import type {
AllureTestCaseMetadata,
AllureTestStepMetadata,
} from 'jest-allure2-reporter';
import type {
TestEnvironmentSetupEvent,
TestEnvironmentCircusEvent,
EnvironmentListenerFn,
} from 'jest-environment-emit';

import { CODE, PREFIX, WORKER_ID } from '../constants';
import realm from '../realms';

const listener: EnvironmentListenerFn = (context) => {
context.testEvents
.on('test_environment_setup', testEnvironmentSetup)
.on('add_hook', addHook)
.on('add_test', addTest)
.on('test_start', executableStart)
.on('test_todo', testSkip)
.on('test_skip', testSkip)
.on('test_done', testDone)
.on('hook_start', executableStart)
.on('hook_failure', executableFailure)
.on('hook_success', executableSuccess)
.on('test_fn_start', executableStart)
.on('test_fn_success', executableSuccess)
.on('test_fn_failure', executableFailure)
.on('teardown', flushFiles);
};

function testEnvironmentSetup({ env }: TestEnvironmentSetupEvent) {
env.global.__ALLURE__ = realm;
state.currentMetadata.set(WORKER_ID, process.env.JEST_WORKER_ID);
}

function addHook({
event,
}: TestEnvironmentCircusEvent<Circus.Event & { name: 'add_hook' }>) {
const code = event.fn.toString();
const hidden = code.includes(
"during setup, this cannot be null (and it's fine to explode if it is)",
);

const metadata = {
code,
} as Record<string, unknown>;

if (hidden) {
delete metadata.code;
metadata.hidden = true;
}

state.currentMetadata.assign(PREFIX, metadata);
}

function addTest({
event,
}: TestEnvironmentCircusEvent<Circus.Event & { name: 'add_test' }>) {
state.currentMetadata.set(CODE, event.fn.toString());
}

// eslint-disable-next-line no-empty-pattern
function executableStart({}: TestEnvironmentCircusEvent) {
const metadata: AllureTestStepMetadata = {
start: Date.now(),
stage: Stage.RUNNING,
};

state.currentMetadata.assign(PREFIX, metadata);
}

function executableFailure({
event,
}: TestEnvironmentCircusEvent<
Circus.Event & { name: 'test_fn_failure' | 'hook_failure' }
>) {
const metadata: AllureTestStepMetadata = {
stop: Date.now(),
stage: Stage.INTERRUPTED,
status: Status.FAILED,
};

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

metadata.statusDetails = { message, trace };
}

state.currentMetadata.assign(PREFIX, metadata);
}

async function flushFiles() {
await realm.runtime.flush();
}

// eslint-disable-next-line no-empty-pattern
function executableSuccess({}: TestEnvironmentCircusEvent) {
const metadata: AllureTestStepMetadata = {
stop: Date.now(),
stage: Stage.FINISHED,
status: Status.PASSED,
};

state.currentMetadata.assign(PREFIX, metadata);
}

function testSkip() {
const metadata: AllureTestCaseMetadata = {
stop: Date.now(),
stage: Stage.PENDING,
status: Status.SKIPPED,
};

state.currentMetadata.assign(PREFIX, metadata);
}

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

const metadata: AllureTestCaseMetadata = {
stop: Date.now(),
stage: hasErrors ? Stage.INTERRUPTED : Stage.FINISHED,
status: hasErrors ? errorStatus : Status.PASSED,
};

state.currentMetadata.assign(PREFIX, metadata);
}

function isMatcherError(error: any) {
return Boolean(error?.matcherResult);
}

export default listener;
8 changes: 6 additions & 2 deletions src/environment/node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import JestEnvironmentNode from 'jest-metadata/environment-node';

import { WithAllure2 } from './decorator';
import listener from './listener';

export const TestEnvironment = JestEnvironmentNode.derive(
listener,
'WithAllure',
);

export const TestEnvironment = WithAllure2(JestEnvironmentNode);
export default TestEnvironment;

0 comments on commit ffb0a84

Please sign in to comment.