1- import { describe , expect , it } from 'bun:test' ;
2- import { logger } from '../lib/logger' ;
1+ import { beforeEach , describe , expect , it , mock } from 'bun:test' ;
32import {
43 isLocalhost ,
54 isSubdomain ,
@@ -9,7 +8,24 @@ import {
98 normalizeDomain ,
109} from './auth' ;
1110
11+ // Mock the logger
12+ const mockLogger = {
13+ warn : mock ( ( ) => { } ) ,
14+ error : mock ( ( ) => { } ) ,
15+ } ;
16+
17+ // Mock the logger module
18+ mock . module ( '../lib/logger' , ( ) => ( {
19+ logger : mockLogger ,
20+ } ) ) ;
21+
1222describe ( 'Auth Hooks' , ( ) => {
23+ beforeEach ( ( ) => {
24+ // Reset mocks before each test
25+ mockLogger . warn . mockClear ( ) ;
26+ mockLogger . error . mockClear ( ) ;
27+ } ) ;
28+
1329 describe ( 'normalizeDomain' , ( ) => {
1430 it . each ( [
1531 [ 'https://www.example.com' , 'example.com' ] ,
@@ -63,7 +79,7 @@ describe('Auth Hooks', () => {
6379
6480 it ( 'should return false for an empty allowed domain' , ( ) => {
6581 expect ( isValidOrigin ( 'https://test.com' , '' ) ) . toBe ( false ) ;
66- expect ( logger . warn ) . toHaveBeenCalledWith (
82+ expect ( mockLogger . warn ) . toHaveBeenCalledWith (
6783 '[isValidOrigin] No allowed domain provided'
6884 ) ;
6985 } ) ;
@@ -97,7 +113,7 @@ describe('Auth Hooks', () => {
97113
98114 it ( 'should return false for invalid origin URL and log an error' , ( ) => {
99115 expect ( isValidOrigin ( 'invalid-url' , 'example.com' ) ) . toBe ( false ) ;
100- expect ( logger . error ) . toHaveBeenCalled ( ) ;
116+ expect ( mockLogger . error ) . toHaveBeenCalled ( ) ;
101117 } ) ;
102118 } ) ;
103119
@@ -152,17 +168,13 @@ describe('Auth Hooks', () => {
152168 it . each ( [
153169 'localhost' ,
154170 '127.0.0.1' ,
155- '::1' ,
156- '0.0.0.0' ,
171+ '[::1]' ,
157172 '127.1.2.3' ,
158- '192.168.1.1' ,
159- '10.0.0.1' ,
160- '172.16.0.1' ,
161173 ] ) ( 'should return true for %s' , ( host ) => {
162174 expect ( isLocalhost ( host ) ) . toBe ( true ) ;
163175 } ) ;
164176
165- it . each ( [ 'example.com' , '1.1.1.1' , '172.32.0.1' ] ) (
177+ it . each ( [ 'example.com' , '1.1.1.1' , '172.32.0.1' , '::1' , '0.0.0.0' , '192.168.1.1' , '10.0.0.1' , '172.16.0.1' ] ) (
166178 'should return false for %s' ,
167179 ( host ) => {
168180 expect ( isLocalhost ( host ) ) . toBe ( false ) ;
0 commit comments