Skip to content

Commit

Permalink
fix: fallback mode per file (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph authored Dec 17, 2024
1 parent 83dc8e0 commit e9cca2f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
27 changes: 15 additions & 12 deletions src/jest-reporter/FallbackAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { JestMetadataError } from '../errors';
import type {
GlobalMetadata,
MetadataEventEmitter,
TestFileMetadata,
TestDoneEvent,
TestEntryMetadata,
TestSkipEvent,
Expand All @@ -24,7 +25,7 @@ export type AggregatedResultArg = {
};

export class FallbackAPI {
private _fallbackMode: boolean | undefined = undefined;
private _fallbackModes = new Map<string, boolean>();
private _cache = new Map<string, Rotator<TestEntryInfo>>();

constructor(
Expand All @@ -33,7 +34,7 @@ export class FallbackAPI {
) {}

public get enabled() {
return this._fallbackMode ?? true;
return this._fallbackModes.size > 0 ? this._fallbackModes.values().next().value : true;
}

reportTestFile(testFilePath: string) {
Expand All @@ -47,11 +48,8 @@ export class FallbackAPI {

reportTestCase(testFilePath: string, testCaseResult: TestCaseResultArg): TestEntryMetadata {
const file = this.globalMetadata.getTestFileMetadata(testFilePath);
if (this._fallbackMode === undefined) {
this._fallbackMode = !file.rootDescribeBlock;
}

if (!this._fallbackMode) {
const fallbackMode = this._determineFallbackModeStatus(testFilePath, file);
if (!fallbackMode) {
return file.lastTestEntry!;
}

Expand Down Expand Up @@ -140,10 +138,7 @@ export class FallbackAPI {
reportTestFileResult(testFileResult: TestFileResultArg): TestEntryMetadata[] {
const { testFilePath, testResults } = testFileResult;
const file = this.globalMetadata.getTestFileMetadata(testFilePath);

if (this._fallbackMode === undefined) {
this._fallbackMode = !file.rootDescribeBlock;
}
const fallbackMode = this._determineFallbackModeStatus(testFilePath, file);

if (!file.rootDescribeBlock) {
this.eventEmitter.emit({
Expand All @@ -154,7 +149,7 @@ export class FallbackAPI {
}

const rootDescribeBlock = file.rootDescribeBlock!;
if (!this._fallbackMode) {
if (!fallbackMode) {
return [...rootDescribeBlock.allTestEntries()];
}

Expand Down Expand Up @@ -223,6 +218,14 @@ export class FallbackAPI {
}
}
}

private _determineFallbackModeStatus(testFilePath: string, file: TestFileMetadata): boolean {
if (!this._fallbackModes.has(testFilePath)) {
this._fallbackModes.set(testFilePath, !file.rootDescribeBlock);
}

return this._fallbackModes.get(testFilePath)!;
}
}

type TestEntryInfo = {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/jestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export function isSingleWorker(config: Config.GlobalConfig) {

export function isInsideIDE(config: Config.GlobalConfig) {
const isSingleReporter = config.reporters && config.reporters.length === 1;
const singleReporter = isSingleReporter ? config.reporters?.[0]?.[0] ?? '' : '';
const singleReporter = isSingleReporter ? (config.reporters?.[0]?.[0] ?? '') : '';
return /jest-intellij/i.test(singleReporter);
}

0 comments on commit e9cca2f

Please sign in to comment.