Skip to content

Commit 2029ff8

Browse files
fix: console.error
1 parent 7e7d4f6 commit 2029ff8

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/useFlagContext.test.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
import { renderHook } from '@testing-library/react-hooks/native';
1+
import { renderHook } from '@testing-library/react';
2+
import { vi, test, expect } from 'vitest';
23
import FlagProvider from "./FlagProvider";
34
import { useFlagContext } from "./useFlagContext";
45

5-
test("throws an error if used outside of a FlagProvider", () => {
6+
test("logs an error if used outside of a FlagProvider", () => {
7+
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
8+
69
const { result } = renderHook(() => useFlagContext());
10+
expect(consoleSpy).toHaveBeenCalledWith("This hook must be used within a FlagProvider");
11+
expect(result.current).toBeNull();
712

8-
expect(result.error).toEqual(
9-
Error("This hook must be used within a FlagProvider")
10-
);
13+
consoleSpy.mockRestore();
1114
});
1215

13-
test("does not throw an error if used inside of a FlagProvider", () => {
16+
test("does not log an error if used inside of a FlagProvider", () => {
17+
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
18+
1419
const { result } = renderHook(() => useFlagContext(), { wrapper: FlagProvider });
20+
expect(consoleSpy).not.toHaveBeenCalled();
21+
expect(result.current).not.toBeNull();
1522

16-
expect(result.error).toBeUndefined();
23+
consoleSpy.mockRestore();
1724
});

src/useFlagContext.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import FlagContext from './FlagContext';
44
export function useFlagContext() {
55
const context = useContext(FlagContext);
66
if (!context) {
7-
throw new Error('This hook must be used within a FlagProvider');
7+
console.error('This hook must be used within a FlagProvider');
8+
return null;
89
}
910
return context;
1011
}

0 commit comments

Comments
 (0)