Skip to content

Commit c5251c5

Browse files
fix: remove console messages in production
1 parent 6984694 commit c5251c5

File tree

7 files changed

+34
-5
lines changed

7 files changed

+34
-5
lines changed

src/getVisitedPathData/getVisitedPathData.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isRecursiveProxyMock } from "~/isRecursiveProxyMock";
22
import type { ProxyData, ProxyPath, ProxyStackItem } from "~/proxyTypes";
3+
import { developmentLog } from "~/utils/developmentLog";
34
import { doPathsMatch } from "~/utils/doPathsMatch";
45
import { getCurrentPath } from "~/utils/getCurrentPath";
56
import { getProxyStack } from "~/utils/getProxyStack";
@@ -11,7 +12,7 @@ import { getProxyStack } from "~/utils/getProxyStack";
1112
*/
1213
export function getVisitedPathData(proxy: unknown, path: ProxyPath): ProxyData[] | null {
1314
if (!isRecursiveProxyMock(proxy)) {
14-
console.warn("Must pass an object created with `recursiveProxyMock()`. Instead received:", proxy);
15+
developmentLog("Must pass an object created with `recursiveProxyMock()`. Instead received:", proxy);
1516
return null;
1617
}
1718
const proxyStack = getProxyStack(proxy);

src/hasPathBeenCalledWith/hasPathBeenCalledWith.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { getVisitedPathData } from "~/getVisitedPathData";
22
import { isRecursiveProxyMock } from "~/isRecursiveProxyMock";
33
import { ProxySymbol } from "~/ProxySymbol";
44
import type { ProxyCallableItem, ProxyPath } from "~/proxyTypes";
5+
import { developmentLog } from "~/utils/developmentLog";
56
import { doArraysMatch } from "~/utils/doArraysMatch";
67

78
/**
@@ -12,11 +13,11 @@ import { doArraysMatch } from "~/utils/doArraysMatch";
1213
*/
1314
export function hasPathBeenCalledWith(proxy: unknown, path: ProxyPath, args: unknown[]): boolean {
1415
if (!isRecursiveProxyMock(proxy)) {
15-
console.warn("Must pass an object created with `recursiveProxyMock()`. Instead received:", proxy);
16+
developmentLog("Must pass an object created with `recursiveProxyMock()`. Instead received:", proxy);
1617
return false;
1718
}
1819
if (![ProxySymbol.APPLY, ProxySymbol.CONSTRUCT].includes(path[path.length - 1] as symbol)) {
19-
console.warn("The path must end with `ProxySymbol.APPLY` or `ProxySymbol.CONSTRUCT`");
20+
developmentLog("The path must end with `ProxySymbol.APPLY` or `ProxySymbol.CONSTRUCT`");
2021
return false;
2122
}
2223
const visitedPathData = (getVisitedPathData(proxy, path) ?? []) as ProxyCallableItem[];

src/hasPathBeenVisited/hasPathBeenVisited.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { isRecursiveProxyMock } from "~/isRecursiveProxyMock";
22
import type { ProxyPath } from "~/proxyTypes";
33
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- used in JSDoc
44
import { recursiveProxyMock } from "~/recursiveProxyMock";
5+
import { developmentLog } from "~/utils/developmentLog";
56
import { doPathsMatch } from "~/utils/doPathsMatch";
67
import { listAllPaths } from "~/utils/listAllPaths";
78

@@ -12,7 +13,7 @@ import { listAllPaths } from "~/utils/listAllPaths";
1213
*/
1314
export function hasPathBeenVisited(proxy: unknown, path: ProxyPath): boolean {
1415
if (!isRecursiveProxyMock(proxy)) {
15-
console.warn("Must pass an object created with `recursiveProxyMock()`. Instead received:", proxy);
16+
developmentLog("Must pass an object created with `recursiveProxyMock()`. Instead received:", proxy);
1617
return false;
1718
}
1819
const allPaths = listAllPaths(proxy);

src/listAllProxyOperations/listAllProxyOperations.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isRecursiveProxyMock } from "~/isRecursiveProxyMock";
22
import type { ProxyData } from "~/proxyTypes";
3+
import { developmentLog } from "~/utils/developmentLog";
34
import { getProxyStack } from "~/utils/getProxyStack";
45

56
/**
@@ -8,7 +9,7 @@ import { getProxyStack } from "~/utils/getProxyStack";
89
*/
910
export function listAllProxyOperations(proxy: unknown): ProxyData[] {
1011
if (!isRecursiveProxyMock(proxy)) {
11-
console.warn("Must pass an object created with `recursiveProxyMock()`. Instead received:", proxy);
12+
developmentLog("Must pass an object created with `recursiveProxyMock()`. Instead received:", proxy);
1213
return [];
1314
}
1415
return getProxyStack(proxy);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { developmentLog } from "~/utils/developmentLog";
2+
3+
describe("developmentLog", () => {
4+
beforeEach(() => {
5+
process.env.NODE_ENV = "test";
6+
});
7+
test("uses console.warn in non-production mode", () => {
8+
const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation();
9+
developmentLog("message", 123);
10+
expect(consoleWarnSpy).toHaveBeenCalledWith("message", 123);
11+
});
12+
13+
test("does not log in production", () => {
14+
process.env.NODE_ENV = "production";
15+
const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation();
16+
developmentLog("message", 123);
17+
expect(consoleWarnSpy).not.toHaveBeenCalled();
18+
});
19+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function developmentLog(...messages: unknown[]): void {
2+
if (process.env.NODE_ENV !== "production") {
3+
console.warn(...messages);
4+
}
5+
}

src/utils/developmentLog/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { developmentLog } from "./developmentLog";

0 commit comments

Comments
 (0)