fix: check if process is defined before using so it works in browser (#4497)

* refactor: use isTestEnv() utils where applicable

* check if process is defined
This commit is contained in:
Aakansha Doshi 2021-12-28 17:17:41 +05:30 committed by GitHub
parent 38236bc5e0
commit 11396a21de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 7 deletions

View File

@ -13,7 +13,7 @@ import {
FontFamilyValues, FontFamilyValues,
ExcalidrawRectangleElement, ExcalidrawRectangleElement,
} from "../element/types"; } from "../element/types";
import { getFontString, getUpdatedTimestamp } from "../utils"; import { getFontString, getUpdatedTimestamp, isTestEnv } from "../utils";
import { randomInteger, randomId } from "../random"; import { randomInteger, randomId } from "../random";
import { mutateElement, newElementWith } from "./mutateElement"; import { mutateElement, newElementWith } from "./mutateElement";
import { getNewGroupIdsForDuplication } from "../groups"; import { getNewGroupIdsForDuplication } from "../groups";
@ -369,7 +369,7 @@ export const duplicateElement = <TElement extends Mutable<ExcalidrawElement>>(
overrides?: Partial<TElement>, overrides?: Partial<TElement>,
): TElement => { ): TElement => {
let copy: TElement = deepCopyElement(element); let copy: TElement = deepCopyElement(element);
if (process.env.NODE_ENV === "test") { if (isTestEnv()) {
copy.id = `${copy.id}_copy`; copy.id = `${copy.id}_copy`;
// `window.h` may not be defined in some unit tests // `window.h` may not be defined in some unit tests
if ( if (

View File

@ -1,5 +1,6 @@
import { Random } from "roughjs/bin/math"; import { Random } from "roughjs/bin/math";
import { nanoid } from "nanoid"; import { nanoid } from "nanoid";
import { isTestEnv } from "./utils";
let random = new Random(Date.now()); let random = new Random(Date.now());
let testIdBase = 0; let testIdBase = 0;
@ -11,5 +12,4 @@ export const reseed = (seed: number) => {
testIdBase = 0; testIdBase = 0;
}; };
export const randomId = () => export const randomId = () => (isTestEnv() ? `id${testIdBase++}` : nanoid());
process.env.NODE_ENV === "test" ? `id${testIdBase++}` : nanoid();

View File

@ -443,8 +443,7 @@ export const bytesToHexString = (bytes: Uint8Array) => {
.join(""); .join("");
}; };
export const getUpdatedTimestamp = () => export const getUpdatedTimestamp = () => (isTestEnv() ? 1 : Date.now());
process.env.NODE_ENV === "test" ? 1 : Date.now();
/** /**
* Transforms array of objects containing `id` attribute, * Transforms array of objects containing `id` attribute,
@ -459,4 +458,5 @@ export const arrayToMap = <T extends { id: string } | string>(
}, new Map()); }, new Map());
}; };
export const isTestEnv = () => process?.env?.NODE_ENV === "test"; export const isTestEnv = () =>
typeof process !== "undefined" && process.env?.NODE_ENV === "test";