Skip to content

Commit 32181d9

Browse files
committed
fix: fix express tests failures on ci
fix express tests failures on ci
1 parent 447017e commit 32181d9

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

packages/express/src/index.test.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ const getIdTokenClaims = vi.fn(() => ({
2222
const signOut = vi.fn();
2323
const getContext = vi.fn(async () => ({ isAuthenticated: true }));
2424

25+
/**
26+
* All the expect function to be called assertions wrapped in the `end` callback
27+
* in the following test cases are randomly failing under the vitest --coverage mode.
28+
*
29+
* So we manually added a delay to wait for the promise to resolve before making the assertions.
30+
*/
31+
const delay = async (ms: number) =>
32+
new Promise<void>((resolve) => {
33+
setTimeout(resolve, ms);
34+
});
35+
2536
vi.mock('./storage', () => ({
2637
default: vi.fn(() => ({
2738
setItem,
@@ -64,7 +75,8 @@ describe('Express', () => {
6475
testRouter(handleAuthRoutes(configs))
6576
.get('/logto/sign-in')
6677
.expect('Location', signInUrl)
67-
.end(() => {
78+
.end(async () => {
79+
await delay(100);
6880
expect(signIn).toHaveBeenCalled();
6981
});
7082
});
@@ -73,7 +85,8 @@ describe('Express', () => {
7385
testRouter(handleAuthRoutes({ ...configs, authRoutesPrefix: 'custom' }))
7486
.get('/logto/sign-in')
7587
.expect('Location', signInUrl)
76-
.end(() => {
88+
.end(async () => {
89+
await delay(100);
7790
expect(signIn).toHaveBeenCalled();
7891
});
7992
});
@@ -84,7 +97,8 @@ describe('Express', () => {
8497
testRouter(handleAuthRoutes(configs))
8598
.get('/logto/sign-up')
8699
.expect('Location', `${signInUrl}?interactionMode=signUp`)
87-
.end(() => {
100+
.end(async () => {
101+
await delay(100);
88102
expect(signIn).toHaveBeenCalled();
89103
});
90104
});
@@ -95,7 +109,8 @@ describe('Express', () => {
95109
testRouter(handleAuthRoutes(configs))
96110
.get('/logto/sign-in-callback')
97111
.expect('Location', configs.baseUrl)
98-
.end(() => {
112+
.end(async () => {
113+
await delay(100);
99114
expect(handleSignInCallback).toHaveBeenCalled();
100115
});
101116
});
@@ -106,7 +121,8 @@ describe('Express', () => {
106121
testRouter(handleAuthRoutes(configs))
107122
.get('/logto/sign-out')
108123
.expect('Location', configs.baseUrl)
109-
.end(() => {
124+
.end(async () => {
125+
await delay(100);
110126
expect(signOut).toHaveBeenCalled();
111127
});
112128
});

0 commit comments

Comments
 (0)