diff --git a/.changeset/twelve-rules-glow.md b/.changeset/twelve-rules-glow.md new file mode 100644 index 00000000000..2f6666688f2 --- /dev/null +++ b/.changeset/twelve-rules-glow.md @@ -0,0 +1,5 @@ +--- +'@itwin/itwinui-react': patch +--- + +iTwinUI will now also check for `vitest` and `mocha` (in addition to `jest`) before running any code that would cause problems in these test runners. diff --git a/packages/itwinui-react/src/core/ThemeProvider/ThemeProvider.tsx b/packages/itwinui-react/src/core/ThemeProvider/ThemeProvider.tsx index a6c7eb4dbca..df1a02cab5c 100644 --- a/packages/itwinui-react/src/core/ThemeProvider/ThemeProvider.tsx +++ b/packages/itwinui-react/src/core/ThemeProvider/ThemeProvider.tsx @@ -13,7 +13,7 @@ import { useControlledState, useLatestRef, importCss, - isJest, + isUnitTest, } from '../utils/index.js'; import type { PolymorphicForwardRefComponent } from '../utils/index.js'; import { ThemeContext } from './ThemeContext.js'; @@ -301,8 +301,8 @@ const FallbackStyles = ({ root }: { root: HTMLElement }) => { return; } - // bail if jest because it doesn't care about CSS 🤷 - if (isJest) { + // bail if isUnitTest because unit tests don't care about CSS 🤷 + if (isUnitTest) { return; } diff --git a/packages/itwinui-react/src/core/utils/functions/dev.ts b/packages/itwinui-react/src/core/utils/functions/dev.ts index 8ba8c76adb4..68c8db2a16a 100644 --- a/packages/itwinui-react/src/core/utils/functions/dev.ts +++ b/packages/itwinui-react/src/core/utils/functions/dev.ts @@ -2,14 +2,22 @@ * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ -// eslint-disable-next-line @typescript-eslint/no-explicit-any +/* eslint-disable @typescript-eslint/no-explicit-any */ + const isJest = typeof (globalThis as any).jest !== 'undefined'; +const isMocha = + typeof (globalThis as any).beforeEach !== 'undefined' && + `${(globalThis as any).beforeEach}`.replace(/\s/g, '') === + 'function(name,fn){suites[0].beforeEach(name,fn);}'; +const isVitest = typeof (globalThis as any).__vitest_index__ !== 'undefined'; + +const isUnitTest = isJest || isVitest || isMocha; let isDev = false; // wrapping in try-catch because process might be undefined try { - isDev = process.env.NODE_ENV !== 'production' && !isJest; + isDev = process.env.NODE_ENV !== 'production' && !isUnitTest; } catch {} /** @@ -31,4 +39,4 @@ const createWarningLogger = !isDev }; }; -export { isJest, isDev, createWarningLogger }; +export { isUnitTest, isDev, createWarningLogger };