Skip to content

Commit

Permalink
fix: false positive in fallback mode (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph authored Oct 13, 2023
1 parent 926077a commit ac1f293
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/environment-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { inspect } from 'util';
import type { EnvironmentContext, JestEnvironment, JestEnvironmentConfig } from '@jest/environment';
import type { Circus } from '@jest/types';
import { JestMetadataError } from './errors';
import { realm, injectRealmIntoSandbox } from './realms';
import { injectRealmIntoSandbox, realm, detectDuplicateRealms } from './realms';
import { jestUtils, SemiAsyncEmitter } from './utils';

const emitterMap: WeakMap<object, SemiAsyncEmitter<ForwardedCircusEvent>> = new WeakMap();
Expand All @@ -12,6 +12,7 @@ export function onTestEnvironmentCreate(
jestEnvironmentConfig: JestEnvironmentConfig,
environmentContext: EnvironmentContext,
): void {
detectDuplicateRealms(true);
injectRealmIntoSandbox(jestEnvironment.global, realm);
const testFilePath = environmentContext.testPath;
realm.environmentHandler.handleEnvironmentCreated(testFilePath);
Expand Down Expand Up @@ -103,6 +104,8 @@ export async function onTestEnvironmentSetup(_env: JestEnvironment): Promise<voi
}

export async function onTestEnvironmentTeardown(_env: JestEnvironment): Promise<void> {
detectDuplicateRealms(false);

if (realm.type === 'child_process') {
await realm.ipc.stop();
}
Expand Down
19 changes: 18 additions & 1 deletion src/realms/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,27 @@ export function injectRealmIntoSandbox(sandbox: any, realm: ProcessRealm): Proce
return realm;
}

/**
* Workaround for the fallback mode, when Jest uses jest-environment-node.
* Jest blindly copies `globalThis` into the sandbox, so it is not enough to
* simply check that __JEST_METADATA_SANDBOX__ is not truthy.
*
* This is especially bad in Jest's single worker mode, because
* reporter's globalThis === testEnvironment's globalThis == sandbox.
*
* This hook is enabled after the copying happens, and disabled at later stages
* when all potentially conflicting packages are loaded. It is not easy to
* grasp, but it works.
*/
export function detectDuplicateRealms(enabled: boolean): void {
const globalAny = globalThis as any;
globalAny.__JEST_METADATA_SANDBOX__ = enabled ? false : undefined;
}

export function getSandboxedRealm(): ProcessRealm | undefined {
const globalAny = globalThis as any;
const realm = globalAny.__JEST_METADATA__;
if (realm && !globalAny.__JEST_METADATA_SANDBOX__) {
if (realm && globalAny.__JEST_METADATA_SANDBOX__ === false) {
console.warn(
'[jest-metadata] Detected duplicate jest-metadata package in the same process. This may lead to unexpected behavior.',
);
Expand Down
2 changes: 1 addition & 1 deletion src/realms/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { injectRealmIntoSandbox } from './detect';
export { injectRealmIntoSandbox, detectDuplicateRealms } from './detect';
export { default as realm } from './realm';
export type { ParentProcessRealm } from './ParentProcessRealm';
export type { ChildProcessRealm } from './ChildProcessRealm';
5 changes: 4 additions & 1 deletion src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import type {
TestResult,
} from '@jest/reporters';
import { JestMetadataError } from './errors';
import { realm as unknownRealm } from './realms';
import { detectDuplicateRealms, realm as unknownRealm } from './realms';
import type { ParentProcessRealm } from './realms';

const realm = unknownRealm as ParentProcessRealm;

detectDuplicateRealms(true);

export const query = realm.query;

/**
Expand All @@ -32,6 +34,7 @@ export class JestMetadataReporter implements Reporter {
}

onRunStart(_results: AggregatedResult, _options: ReporterOnStartOptions): Promise<void> {
detectDuplicateRealms(false);
return realm.reporterServer.onRunStart();
}

Expand Down

0 comments on commit ac1f293

Please sign in to comment.