1515 * limitations under the License.
1616 */
1717import { deleteApp , initializeApp , initializeServerApp } from 'firebase/app' ;
18- import { deleteUser , getAuth , onAuthStateChanged , signInAnonymously , User } from 'firebase/auth' ;
18+ import { deleteUser , getAuth , onAuthStateChanged , signInAnonymously } from 'firebase/auth' ;
1919import { firebaseConfig } from 'lib/firebase' ;
20- import { OK , OK_SKIPPED , FAILED , sleep } from 'lib/util' ;
20+ import { OK , OK_SKIPPED , FAILED } from 'lib/util' ;
2121
2222export type TestResults = {
2323 initializeAppResult : string ,
@@ -29,6 +29,7 @@ export type TestResults = {
2929 getServerAppUserResult : string ,
3030 deleteServerAppResult : string
3131 deleteUserResult : string ,
32+ userSignedOutResult : string ,
3233 deleteAppResult : string
3334} ;
3435
@@ -43,20 +44,28 @@ export function initializeTestResults(): TestResults {
4344 getServerAppUserResult : FAILED ,
4445 deleteServerAppResult : FAILED ,
4546 deleteUserResult : FAILED ,
47+ userSignedOutResult : FAILED ,
4648 deleteAppResult : FAILED
4749 } ;
4850}
4951
50- async function authStateChangedUserSignedIn ( auth ) : Promise < User > {
51- const promise : Promise < User > = new Promise ( ( resolve , reject ) => {
52+ async function waitForUserSignedIn ( auth ) : Promise < void > {
53+ const promise : Promise < void > = new Promise < void > ( ( resolve , reject ) => {
54+ let completed : boolean = false ;
5255 const unsubscribe = onAuthStateChanged ( auth , ( user ) => {
53- unsubscribe ( ) ;
5456 if ( user ) {
55- resolve ( user ) ;
56- } else {
57- reject ( ) ;
57+ completed = true ;
58+ unsubscribe ( ) ;
59+ resolve ( ) ;
5860 }
5961 } ) ;
62+ setTimeout ( ( ) => {
63+ if ( ! completed ) {
64+ completed = true ;
65+ unsubscribe ( ) ;
66+ reject ( ) ;
67+ }
68+ } , 3000 ) ;
6069 } ) ;
6170 return promise ;
6271}
@@ -70,7 +79,7 @@ export async function testAuth(isServer: boolean = false): Promise<TestResults>
7079 await auth . authStateReady ( ) ;
7180 result . initializeAuthResult = OK ;
7281 await signInAnonymously ( auth ) ;
73- await authStateChangedUserSignedIn ( auth ) ;
82+ await waitForUserSignedIn ( auth ) ;
7483 if ( auth . currentUser !== null ) {
7584 result . signInAnonymouslyResult = OK ;
7685 const idToken = await auth . currentUser . getIdToken ( ) ;
@@ -97,12 +106,12 @@ export async function testAuth(isServer: boolean = false): Promise<TestResults>
97106 result . deleteServerAppResult = OK ;
98107 }
99108 await deleteUser ( auth . currentUser ) ;
100- sleep ( 500 ) ;
109+ result . deleteUserResult = OK ;
110+ await auth . signOut ( ) ;
101111 if ( auth . currentUser === null ) {
102- result . deleteUserResult = OK ;
112+ result . userSignedOutResult = OK ;
103113 }
104114 }
105-
106115 deleteApp ( firebaseApp ) ;
107116 result . deleteAppResult = OK ;
108117 } catch ( e ) {
0 commit comments