diff --git a/src/loader/built-in/module/module.ts b/src/loader/built-in/module/module.ts index 64dabb4e..683a7cfa 100644 --- a/src/loader/built-in/module/module.ts +++ b/src/loader/built-in/module/module.ts @@ -16,12 +16,15 @@ import { } from '../../../locator'; import { handleException, - hasStringProperty, isFilePath, + hasStringProperty, + isFilePath, isObject, + isTsNodeRuntimeEnvironment, + isTypeScriptError, } from '../../../utils'; import type { Loader } from '../../type'; import type { ModuleLoadOptions } from './type'; -import { isTsNodeRuntimeEnvironment, toModuleRecord } from './utils'; +import { toModuleRecord } from './utils'; export class ModuleLoader implements Loader { protected jiti : JITI; @@ -42,7 +45,8 @@ export class ModuleLoader implements Loader { } catch (e) { if ( e instanceof SyntaxError || - e instanceof ReferenceError + e instanceof ReferenceError || + isTypeScriptError(e) ) { throw e; } @@ -67,7 +71,8 @@ export class ModuleLoader implements Loader { } catch (e) { if ( e instanceof SyntaxError || - e instanceof ReferenceError + e instanceof ReferenceError || + isTypeScriptError(e) ) { throw e; } diff --git a/src/loader/built-in/module/utils.ts b/src/loader/built-in/module/utils.ts index f101d3be..11f5e27c 100644 --- a/src/loader/built-in/module/utils.ts +++ b/src/loader/built-in/module/utils.ts @@ -8,17 +8,6 @@ import { hasOwnProperty, isObject } from '../../../utils'; import type { LoaderFilterFn, ModuleExport } from './type'; -export function isJestRuntimeEnvironment() : boolean { - return process.env && - process.env.JEST_WORKER_ID !== undefined; -} - -export function isTsNodeRuntimeEnvironment() : boolean { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - return !!process[Symbol.for('ts-node.register.instance')]; -} - type ESModule = { [key: string]: any, __esModule: boolean }; export function isESModule(input: unknown) : input is ESModule { return isObject(input) && diff --git a/src/utils/index.ts b/src/utils/index.ts index 05a4ac6a..deacf4ad 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -11,3 +11,5 @@ export * from './has-property'; export * from './object'; export * from './to-array'; export * from './file-path'; +export * from './runtime'; +export * from './typescript'; diff --git a/src/utils/runtime.ts b/src/utils/runtime.ts new file mode 100644 index 00000000..b116b2c2 --- /dev/null +++ b/src/utils/runtime.ts @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2024. + * Author Peter Placzek (tada5hi) + * For the full copyright and license information, + * view the LICENSE file that was distributed with this source code. + */ + +export function isJestRuntimeEnvironment() : boolean { + return process.env && + process.env.JEST_WORKER_ID !== undefined; +} + +export function isTsNodeRuntimeEnvironment(): boolean { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + return !!process[Symbol.for('ts-node.register.instance')]; +} diff --git a/src/utils/typescript.ts b/src/utils/typescript.ts new file mode 100644 index 00000000..7ed304e0 --- /dev/null +++ b/src/utils/typescript.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2024. + * Author Peter Placzek (tada5hi) + * For the full copyright and license information, + * view the LICENSE file that was distributed with this source code. + */ + +import { isObject } from './object'; + +export function isTypeScriptError(input: unknown) : input is Error { + if (!isObject(input)) { + return false; + } + + if (typeof input.diagnosticCodes !== 'undefined') { + return true; + } + + return typeof input.message === 'string' && + /TS\d+/.test(input.message); +} diff --git a/test/unit/loader/module.spec.ts b/test/unit/loader/module.spec.ts index 8be6a996..7e2861e9 100644 --- a/test/unit/loader/module.spec.ts +++ b/test/unit/loader/module.spec.ts @@ -8,8 +8,7 @@ import { LoaderManager, getModuleExport, - load, - loadSync, + load, loadSync, } from '../../../src'; import { LoaderId } from '../../../src/loader/constants'; diff --git a/test/unit/utils.spec.ts b/test/unit/utils.spec.ts index 67587e8a..dca28c71 100644 --- a/test/unit/utils.spec.ts +++ b/test/unit/utils.spec.ts @@ -5,7 +5,7 @@ * view the LICENSE file that was distributed with this source code. */ -import { removeFileNameExtension } from '../../src'; +import { isJestRuntimeEnvironment, removeFileNameExtension } from '../../src'; describe('src/utils/*.ts', () => { it('should remove file name extension', () => { @@ -21,4 +21,9 @@ describe('src/utils/*.ts', () => { data = removeFileNameExtension('test.js'); expect(data).toEqual('test'); }); + + it('should detect environment', () => { + const jestEnv = isJestRuntimeEnvironment(); + expect(jestEnv).toEqual(true); + }); });