Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added common reliable check to detect jest,vitest and mocha testing framework. #1807

Merged
merged 13 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
useControlledState,
useLatestRef,
importCss,
isJest,
isTestingFramework,
} from '../utils/index.js';
import type { PolymorphicForwardRefComponent } from '../utils/index.js';
import { ThemeContext } from './ThemeContext.js';
Expand Down Expand Up @@ -301,8 +301,8 @@ const FallbackStyles = ({ root }: { root: HTMLElement }) => {
return;
}

// bail if jest because it doesn't care about CSS 🤷
if (isJest) {
// bail if TestingFramework because it doesn't care about CSS 🤷
if (isTestingFramework()) {
return;
}

Expand Down
15 changes: 11 additions & 4 deletions packages/itwinui-react/src/core/utils/functions/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
* 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
const isJest = typeof (globalThis as any).jest !== 'undefined';
const isTestingFramework = () => {
return (
typeof vitest !== 'undefined' ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
typeof (globalThis as any).jest !== `undefined` ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
typeof (globalThis as any).mocha !== 'undefined'
);
};

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' && !isTestingFramework;
} catch {}

/**
Expand All @@ -31,4 +38,4 @@ const createWarningLogger = !isDev
};
};

export { isJest, isDev, createWarningLogger };
export { isTestingFramework, isDev, createWarningLogger };
Loading