From 926077aa364f313e4fc9c6004d6e5709f477aa4c Mon Sep 17 00:00:00 2001 From: Yaroslav Serhieiev Date: Thu, 12 Oct 2023 10:12:01 +0300 Subject: [PATCH] fix: improve Webstorm integration (#53) --- src/environment-hooks.ts | 7 +++++-- src/utils/index.ts | 1 + src/utils/jestUtils.ts | 11 +++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 src/utils/jestUtils.ts diff --git a/src/environment-hooks.ts b/src/environment-hooks.ts index f220755..80148f0 100644 --- a/src/environment-hooks.ts +++ b/src/environment-hooks.ts @@ -3,7 +3,7 @@ import type { EnvironmentContext, JestEnvironment, JestEnvironmentConfig } from import type { Circus } from '@jest/types'; import { JestMetadataError } from './errors'; import { realm, injectRealmIntoSandbox } from './realms'; -import { SemiAsyncEmitter } from './utils'; +import { jestUtils, SemiAsyncEmitter } from './utils'; const emitterMap: WeakMap> = new WeakMap(); @@ -35,7 +35,10 @@ export function onTestEnvironmentCreate( `Please check that at least one of the reporters in your Jest config inherits from "jest-metadata/reporter".\n` + hint; - if (globalConfig && (globalConfig.runInBand || globalConfig.maxWorkers === 1)) { + if ( + globalConfig && + (jestUtils.isSingleWorker(globalConfig) || jestUtils.isInsideIDE(globalConfig)) + ) { console.warn('[jest-metadata] ' + message); } else { throw new JestMetadataError(message); diff --git a/src/utils/index.ts b/src/utils/index.ts index a3391b8..615fd4d 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,4 +1,5 @@ export * from './emitters'; +export * as jestUtils from './jestUtils'; export { getVersion } from './getVersion'; export { aggregateLogs, logger, optimizeForLogger } from './logger'; export { makeDeferred, Deferred } from './makeDeferred'; diff --git a/src/utils/jestUtils.ts b/src/utils/jestUtils.ts new file mode 100644 index 0000000..9126f6a --- /dev/null +++ b/src/utils/jestUtils.ts @@ -0,0 +1,11 @@ +import type { Config } from '@jest/reporters'; + +export function isSingleWorker(config: Config.GlobalConfig) { + return config.runInBand || config.maxWorkers === 1; +} + +export function isInsideIDE(config: Config.GlobalConfig) { + const isSingleReporter = config.reporters && config.reporters.length === 1; + const singleReporter = isSingleReporter ? config.reporters?.[0]?.[0] ?? '' : ''; + return /jest-intellij/i.test(singleReporter); +}