Skip to content

Commit

Permalink
useAuth should not throw
Browse files Browse the repository at this point in the history
  • Loading branch information
pseidemann committed Aug 23, 2023
1 parent 4f0d303 commit 1afc47e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const useAuth = (): AuthContextProps => {
const context = React.useContext(AuthContext);

if (!context) {
throw new Error("AuthProvider context is undefined, please verify you are calling useAuth() as child of a <AuthProvider> component.");
console.warn("AuthProvider context is undefined, please verify you are calling useAuth() as child of a <AuthProvider> component.");
}

return context;
return context as AuthContextProps;
};
14 changes: 3 additions & 11 deletions test/useAuth.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,8 @@ describe("useAuth", () => {
await waitFor(() => expect(result.current).toBeDefined());
});

it("should throw with no provider", async () => {
// act
try {
renderHook(() => useAuth());
} catch (err) {
//assert
expect(err).toBeInstanceOf(Error);
expect((err as Error).message).toContain(
"AuthProvider context is undefined, please verify you are calling useAuth() as child of a <AuthProvider> component.",
);
}
it("should return undefined with no provider", async () => {
const { result } = renderHook(() => useAuth());
expect(result.current).toBeUndefined();
});
});

0 comments on commit 1afc47e

Please sign in to comment.