diff --git a/excalidraw-app/collab/Collab.tsx b/excalidraw-app/collab/Collab.tsx index f10783f40..98463dc2c 100644 --- a/excalidraw-app/collab/Collab.tsx +++ b/excalidraw-app/collab/Collab.tsx @@ -25,6 +25,7 @@ import { withBatchedUpdates } from "@excalidraw/excalidraw/reactUtils"; import { assertNever, isDevEnv, + isTestEnv, preventUnload, resolvablePromise, throttleRAF, @@ -241,7 +242,7 @@ class Collab extends PureComponent { appJotaiStore.set(collabAPIAtom, collabAPI); - if (import.meta.env.MODE === ENV.TEST || isDevEnv()) { + if (isTestEnv() || isDevEnv()) { window.collab = window.collab || ({} as Window["collab"]); Object.defineProperties(window, { collab: { @@ -1014,7 +1015,7 @@ declare global { } } -if (import.meta.env.MODE === ENV.TEST || isDevEnv()) { +if (isTestEnv() || isDevEnv()) { window.collab = window.collab || ({} as Window["collab"]); } diff --git a/packages/excalidraw/change.ts b/packages/excalidraw/change.ts index 1afc6107a..ca77d9838 100644 --- a/packages/excalidraw/change.ts +++ b/packages/excalidraw/change.ts @@ -27,6 +27,7 @@ import { assertNever, isDevEnv, isShallowEqual, + isTestEnv, toBrandedType, } from "./utils"; @@ -515,7 +516,7 @@ export class AppStateChange implements Change { // shouldn't really happen, but just in case console.error(`Couldn't apply appstate change`, e); - if (isDevEnv() || import.meta.env.MODE === ENV.TEST) { + if (isTestEnv() || isDevEnv()) { throw e; } @@ -553,7 +554,7 @@ export class AppStateChange implements Change { // if postprocessing fails it does not make sense to bubble up, but let's make sure we know about it console.error(`Couldn't postprocess appstate change deltas.`); - if (isDevEnv() || import.meta.env.MODE === ENV.TEST) { + if (isTestEnv() || isDevEnv()) { throw e; } } finally { @@ -843,7 +844,7 @@ export class ElementsChange implements Change { change = new ElementsChange(added, removed, updated); } - if (isDevEnv() || import.meta.env.MODE === ENV.TEST) { + if (isTestEnv() || isDevEnv()) { ElementsChange.validate(change, "added", this.satisfiesAddition); ElementsChange.validate(change, "removed", this.satisfiesRemoval); ElementsChange.validate(change, "updated", this.satisfiesUpdate); @@ -1107,7 +1108,7 @@ export class ElementsChange implements Change { } catch (e) { console.error(`Couldn't apply elements change`, e); - if (isDevEnv() || import.meta.env.MODE === ENV.TEST) { + if (isTestEnv() || isDevEnv()) { throw e; } @@ -1138,7 +1139,7 @@ export class ElementsChange implements Change { e, ); - if (isDevEnv() || import.meta.env.MODE === ENV.TEST) { + if (isTestEnv() || isDevEnv()) { throw e; } } finally { @@ -1552,7 +1553,7 @@ export class ElementsChange implements Change { // if postprocessing fails, it does not make sense to bubble up, but let's make sure we know about it console.error(`Couldn't postprocess elements change deltas.`); - if (isDevEnv() || import.meta.env.MODE === ENV.TEST) { + if (isTestEnv() || isDevEnv()) { throw e; } } finally { diff --git a/packages/excalidraw/components/App.tsx b/packages/excalidraw/components/App.tsx index a380d9cea..39165dc5c 100644 --- a/packages/excalidraw/components/App.tsx +++ b/packages/excalidraw/components/App.tsx @@ -2435,7 +2435,7 @@ class App extends React.Component { this.excalidrawContainerValue.container = this.excalidrawContainerRef.current; - if (import.meta.env.MODE === ENV.TEST || isDevEnv()) { + if (isTestEnv() || isDevEnv()) { const setState = this.setState.bind(this); Object.defineProperties(window.h, { state: { @@ -11061,7 +11061,7 @@ declare global { } export const createTestHook = () => { - if (import.meta.env.MODE === ENV.TEST || isDevEnv()) { + if (isTestEnv() || isDevEnv()) { window.h = window.h || ({} as Window["h"]); Object.defineProperties(window.h, { diff --git a/packages/excalidraw/data/reconcile.ts b/packages/excalidraw/data/reconcile.ts index 044d3f645..c009d7c9d 100644 --- a/packages/excalidraw/data/reconcile.ts +++ b/packages/excalidraw/data/reconcile.ts @@ -6,7 +6,7 @@ import { syncInvalidIndices, validateFractionalIndices, } from "../fractionalIndex"; -import { arrayToMap, isDevEnv } from "../utils"; +import { arrayToMap, isDevEnv, isTestEnv } from "../utils"; import type { OrderedExcalidrawElement } from "../element/types"; import type { AppState } from "../types"; @@ -47,11 +47,7 @@ const validateIndicesThrottled = throttle( localElements: readonly OrderedExcalidrawElement[], remoteElements: readonly RemoteExcalidrawElement[], ) => { - if ( - isDevEnv() || - import.meta.env.MODE === ENV.TEST || - window?.DEBUG_FRACTIONAL_INDICES - ) { + if (isDevEnv() || isTestEnv() || window?.DEBUG_FRACTIONAL_INDICES) { // create new instances due to the mutation const elements = syncInvalidIndices( orderedElements.map((x) => ({ ...x })), @@ -59,7 +55,7 @@ const validateIndicesThrottled = throttle( validateFractionalIndices(elements, { // throw in dev & test only, to remain functional on `DEBUG_FRACTIONAL_INDICES` - shouldThrow: isDevEnv() || import.meta.env.MODE === ENV.TEST, + shouldThrow: isTestEnv() || isDevEnv(), includeBoundTextValidation: true, reconciliationContext: { localElements, diff --git a/packages/excalidraw/element/textWrapping.ts b/packages/excalidraw/element/textWrapping.ts index 08a30039c..e571b0af0 100644 --- a/packages/excalidraw/element/textWrapping.ts +++ b/packages/excalidraw/element/textWrapping.ts @@ -1,5 +1,5 @@ import { ENV } from "../constants"; -import { isDevEnv } from "../utils"; +import { isDevEnv, isTestEnv } from "../utils"; import { charWidth, getLineWidth } from "./textMeasurements"; @@ -563,7 +563,7 @@ const isSingleCharacter = (maybeSingleCharacter: string) => { * Invariant for the word wrapping algorithm. */ const satisfiesWordInvariant = (word: string) => { - if (import.meta.env.MODE === ENV.TEST || isDevEnv()) { + if (isTestEnv() || isDevEnv()) { if (/\s/.test(word)) { throw new Error("Word should not contain any whitespaces!"); } diff --git a/packages/excalidraw/scene/Scene.ts b/packages/excalidraw/scene/Scene.ts index 6f711c8dd..80274266f 100644 --- a/packages/excalidraw/scene/Scene.ts +++ b/packages/excalidraw/scene/Scene.ts @@ -10,7 +10,7 @@ import { } from "../fractionalIndex"; import { getElementsInGroup } from "../groups"; import { randomInteger } from "../random"; -import { arrayToMap, isDevEnv } from "../utils"; +import { arrayToMap, isDevEnv, isTestEnv } from "../utils"; import { toBrandedType } from "../utils"; import { getSelectedElements } from "./selection"; @@ -57,14 +57,10 @@ const getNonDeletedElements = ( const validateIndicesThrottled = throttle( (elements: readonly ExcalidrawElement[]) => { - if ( - isDevEnv() || - import.meta.env.MODE === ENV.TEST || - window?.DEBUG_FRACTIONAL_INDICES - ) { + if (isDevEnv() || isTestEnv() || window?.DEBUG_FRACTIONAL_INDICES) { validateFractionalIndices(elements, { // throw only in dev & test, to remain functional on `DEBUG_FRACTIONAL_INDICES` - shouldThrow: isDevEnv() || import.meta.env.MODE === ENV.TEST, + shouldThrow: isDevEnv() || isTestEnv(), includeBoundTextValidation: true, }); } diff --git a/packages/excalidraw/store.ts b/packages/excalidraw/store.ts index 148786577..6191c3d22 100644 --- a/packages/excalidraw/store.ts +++ b/packages/excalidraw/store.ts @@ -3,7 +3,7 @@ import { AppStateChange, ElementsChange } from "./change"; import { ENV } from "./constants"; import { newElementWith } from "./element/mutateElement"; import { Emitter } from "./emitter"; -import { isDevEnv, isShallowEqual } from "./utils"; +import { isDevEnv, isShallowEqual, isTestEnv } from "./utils"; import { deepCopyElement } from "./element/duplicate"; @@ -257,7 +257,7 @@ export class Store implements IStore { const message = `There can be at most three store actions scheduled at the same time, but there are "${this.scheduledActions.size}".`; console.error(message, this.scheduledActions.values()); - if (isDevEnv() || import.meta.env.MODE === ENV.TEST) { + if (isTestEnv() || isDevEnv()) { throw new Error(message); } } diff --git a/packages/excalidraw/utils.ts b/packages/excalidraw/utils.ts index 84f6637f1..18b6f2a93 100644 --- a/packages/excalidraw/utils.ts +++ b/packages/excalidraw/utils.ts @@ -169,7 +169,7 @@ export const throttleRAF = ( }; const ret = (...args: T) => { - if (import.meta.env.MODE === "test") { + if (isTestEnv()) { fn(...args); return; }