Skip to content

Commit

Permalink
wip: extend from reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Oct 12, 2023
1 parent 926077a commit d0d7afb
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 17 deletions.
5 changes: 5 additions & 0 deletions e2e/reporters/recorder.js → e2e/reporters/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ class E2eRecorderReporter extends JestMetadataReporter {
await debugUtils.aggregateLogs();
}
}

static onTestEnvironmentCreate(env) {
console.log('I AM ALIVE');
console.log(' ..--..');
}
}

function sleep(ms) {
Expand Down
8 changes: 4 additions & 4 deletions src/environment-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { JestEnvironment } from '@jest/environment';
import type { Circus } from '@jest/types';

import {
ForwardedCircusEvent,
TestEnvironmentEvent,
getEmitter,
onHandleTestEvent,
onTestEnvironmentCreate,
Expand All @@ -11,10 +11,10 @@ import {
} from './environment-hooks';
import type { ReadonlyAsyncEmitter } from './types';

export { ForwardedCircusEvent } from './environment-hooks';
export { TestEnvironmentEvent } from './environment-hooks';

export type WithEmitter<E extends JestEnvironment = JestEnvironment> = E & {
readonly testEvents: ReadonlyAsyncEmitter<ForwardedCircusEvent>;
readonly testEvents: ReadonlyAsyncEmitter<TestEnvironmentEvent>;
};

/**
Expand Down Expand Up @@ -89,7 +89,7 @@ export function WithMetadata<E extends JestEnvironment>(
onTestEnvironmentCreate(this, args[0], args[1]);
}

protected get testEvents(): ReadonlyAsyncEmitter<ForwardedCircusEvent> {
protected get testEvents(): ReadonlyAsyncEmitter<TestEnvironmentEvent> {
return getEmitter(this);
}

Expand Down
78 changes: 67 additions & 11 deletions src/environment-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { JestMetadataError } from './errors';
import { realm, injectRealmIntoSandbox } from './realms';
import { jestUtils, SemiAsyncEmitter } from './utils';

const emitterMap: WeakMap<object, SemiAsyncEmitter<ForwardedCircusEvent>> = new WeakMap();
const emitterMap: WeakMap<object, SemiAsyncEmitter<TestEnvironmentEvent>> = new WeakMap();
const configMap: WeakMap<object, JestEnvironmentConfig> = new WeakMap();

export function onTestEnvironmentCreate(
jestEnvironment: JestEnvironment,
Expand Down Expand Up @@ -52,13 +53,15 @@ export function onTestEnvironmentCreate(

const flushHandler = () => realm.ipc.flush();

const emitter = new SemiAsyncEmitter<ForwardedCircusEvent>('environment', [
const emitter = new SemiAsyncEmitter<TestEnvironmentEvent>('environment', [
'start_describe_definition',
'finish_describe_definition',
'add_hook',
'add_test',
'error',
])
.on('test_environment_setup', startIpc, -1)
.on('test_environment_teardown', stopIpc, Number.MAX_SAFE_INTEGER)
.on('setup', testEventHandler, -1)
.on('include_test_location_in_result', testEventHandler, -1)
.on('start_describe_definition', testEventHandler, -1)
Expand Down Expand Up @@ -88,24 +91,28 @@ export function onTestEnvironmentCreate(
.on('teardown', testEventHandler, Number.MAX_SAFE_INTEGER);

emitterMap.set(jestEnvironment, emitter);
configMap.set(jestEnvironment, jestEnvironmentConfig);
}

export type ForwardedCircusEvent<E extends Circus.Event = Circus.Event> = {
export type TestEnvironmentEvent =
| {
type: 'test_environment_setup' | 'test_environment_teardown';
}
| ForwardedCircusEvent;

type ForwardedCircusEvent<E extends Circus.Event = Circus.Event> = {
type: E['name'];
event: E;
state: Circus.State;
};

export async function onTestEnvironmentSetup(_env: JestEnvironment): Promise<void> {
if (realm.type === 'child_process') {
await realm.ipc.start();
}
export async function onTestEnvironmentSetup(env: JestEnvironment): Promise<void> {
await initReporters(env);
await getEmitter(env).emit({ type: 'test_environment_setup' });
}

export async function onTestEnvironmentTeardown(_env: JestEnvironment): Promise<void> {
if (realm.type === 'child_process') {
await realm.ipc.stop();
}
export async function onTestEnvironmentTeardown(env: JestEnvironment): Promise<void> {
await getEmitter(env).emit({ type: 'test_environment_teardown' });
}

/**
Expand All @@ -118,6 +125,9 @@ export const onHandleTestEvent = (
state: Circus.State,
): void | Promise<void> => getEmitter(env).emit({ type: event.name, event, state });

/**
* Get the environment event emitter by the environment reference.
*/
export const getEmitter = (env: JestEnvironment) => {
const emitter = emitterMap.get(env);
if (!emitter) {
Expand All @@ -128,3 +138,49 @@ export const getEmitter = (env: JestEnvironment) => {

return emitter;
};

/**
* Get the environment configuration by the environment reference.
*/
export const getConfig = (env: JestEnvironment) => {
const config = configMap.get(env);
if (!config) {
throw new JestMetadataError(
'Environment config is not found. Most likely, you are using a non-valid environment reference.',
);
}

return config;
};

async function initReporters(env: JestEnvironment) {
const reporterModules = (getConfig(env)?.globalConfig?.reporters ?? []).map((r) => r[0]);
const reporterExports = await Promise.all(
reporterModules.map((m) => {
try {
return import(m);
} catch (error: unknown) {
// TODO: log this to trace
console.warn(`[jest-metadata] Failed to import reporter module "${m}"`, error);
return;
}
}),
);

for (const reporterExport of reporterExports) {
const ReporterClass = reporterExport?.default ?? reporterExport;
ReporterClass?.onTestEnvironmentCreate?.(env);
}
}

async function startIpc() {
if (realm.type === 'child_process') {
await realm.ipc.start();
}
}

async function stopIpc() {
if (realm.type === 'child_process') {
await realm.ipc.stop();
}
}
2 changes: 1 addition & 1 deletion src/environment-jsdom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JestEnvironmentJsdom from 'jest-environment-jsdom';
import { WithMetadata } from './environment-decorator';

export { ForwardedCircusEvent } from './environment-hooks';
export { TestEnvironmentEvent } from './environment-hooks';
export const TestEnvironment = WithMetadata(JestEnvironmentJsdom);
export default TestEnvironment;
2 changes: 1 addition & 1 deletion src/environment-node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JestEnvironmentNode from 'jest-environment-node';
import { WithMetadata } from './environment-decorator';

export { ForwardedCircusEvent } from './environment-hooks';
export { TestEnvironmentEvent } from './environment-hooks';
export const TestEnvironment = WithMetadata(JestEnvironmentNode);
export default TestEnvironment;

0 comments on commit d0d7afb

Please sign in to comment.