Skip to content

Commit 9d5a368

Browse files
authored
Fix test type errors (#51)
1 parent aa8c305 commit 9d5a368

File tree

4 files changed

+48
-24
lines changed

4 files changed

+48
-24
lines changed

src/auth.spec.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ describe('auth', () => {
9999
updatedAt: '2021-01-01T00:00:00Z',
100100
lastSignInAt: '2021-01-01T00:00:00Z',
101101
externalId: null,
102-
} as User;
102+
locale: null,
103+
metadata: {},
104+
} satisfies User;
103105

104106
// Mock the return type of refreshSession
105107
const mockAuthResponse = {
@@ -327,7 +329,9 @@ describe('auth', () => {
327329
updatedAt: '2023-01-01T00:00:00Z',
328330
lastSignInAt: '2023-01-01T00:00:00Z',
329331
externalId: null,
330-
},
332+
locale: null,
333+
metadata: {},
334+
} satisfies User,
331335
impersonator: {
332336
333337
reason: 'testing',
@@ -389,7 +393,9 @@ describe('auth', () => {
389393
updatedAt: '2023-01-01T00:00:00Z',
390394
lastSignInAt: '2023-01-01T00:00:00Z',
391395
externalId: null,
392-
},
396+
locale: null,
397+
metadata: {},
398+
} satisfies User,
393399
headers: {},
394400
};
395401

@@ -456,7 +462,9 @@ describe('auth', () => {
456462
updatedAt: '2023-01-01T00:00:00Z',
457463
lastSignInAt: '2023-01-01T00:00:00Z',
458464
externalId: null,
459-
},
465+
locale: null,
466+
metadata: {},
467+
} satisfies User,
460468
refreshToken: 'refresh-token',
461469
headers: {},
462470
accessToken: '', // Empty string to meet type requirement but it will be treated as falsy

src/authkit-callback-route.spec.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { configureSessionStorage } from './sessionStorage.js';
99
import { isDataWithResponseInit } from './utils.js';
1010
import { DataWithResponseInit } from './interfaces.js';
11+
import type { LoaderFunctionArgs } from 'react-router';
1112

1213
// Mock dependencies
1314
const fakeWorkosInstance = {
@@ -51,15 +52,19 @@ describe('authLoader', () => {
5152
request: new Request('https://example.com'),
5253
params: {},
5354
context: {},
54-
});
55+
} as LoaderFunctionArgs);
5556

5657
expect(response).toBeUndefined();
5758
});
5859

5960
it('should handle authentication failure', async () => {
6061
authenticateWithCode.mockRejectedValue(new Error('Auth failed'));
6162
request = createRequestWithSearchParams(request, { code: 'invalid-code' });
62-
const response = (await loader({ request, params: {}, context: {} })) as DataWithResponseInit<unknown>;
63+
const response = (await loader({
64+
request,
65+
params: {},
66+
context: {},
67+
} as LoaderFunctionArgs)) as DataWithResponseInit<unknown>;
6368
expect(isDataWithResponseInit(response)).toBeTruthy();
6469

6570
expect(response?.init?.status).toBe(500);
@@ -68,7 +73,11 @@ describe('authLoader', () => {
6873
it('should handle authentication failure with string error', async () => {
6974
authenticateWithCode.mockRejectedValue('Auth failed');
7075
request = createRequestWithSearchParams(request, { code: 'invalid-code' });
71-
const response = (await loader({ request, params: {}, context: {} })) as DataWithResponseInit<unknown>;
76+
const response = (await loader({
77+
request,
78+
params: {},
79+
context: {},
80+
} as LoaderFunctionArgs)) as DataWithResponseInit<unknown>;
7281
expect(isDataWithResponseInit(response)).toBeTruthy();
7382

7483
expect(response?.init?.status).toBe(500);
@@ -80,7 +89,7 @@ describe('authLoader', () => {
8089
request,
8190
params: {},
8291
context: {},
83-
});
92+
} as LoaderFunctionArgs);
8493

8594
expect(workos.userManagement.authenticateWithCode).toHaveBeenCalledWith({
8695
clientId: process.env.WORKOS_CLIENT_ID,
@@ -98,7 +107,7 @@ describe('authLoader', () => {
98107
request,
99108
params: {},
100109
context: {},
101-
});
110+
} as LoaderFunctionArgs);
102111

103112
assertIsResponse(response);
104113
expect(response.status).toBe(302);
@@ -111,7 +120,7 @@ describe('authLoader', () => {
111120
request,
112121
params: {},
113122
context: {},
114-
});
123+
} as LoaderFunctionArgs);
115124

116125
assertIsResponse(response);
117126
expect(response.status).toBe(302);
@@ -125,7 +134,7 @@ describe('authLoader', () => {
125134
request,
126135
params: {},
127136
context: {},
128-
});
137+
} as LoaderFunctionArgs);
129138

130139
expect(onSuccess).toHaveBeenCalled();
131140
});
@@ -137,7 +146,7 @@ describe('authLoader', () => {
137146
}),
138147
params: {},
139148
context: {},
140-
});
149+
} as LoaderFunctionArgs);
141150
assertIsResponse(response);
142151
expect(response.status).toBe(302);
143152
expect(response.headers.get('Location')).toBe('http://example.com/profile');
@@ -159,7 +168,7 @@ describe('authLoader', () => {
159168
request,
160169
params: {},
161170
context: {},
162-
});
171+
} as LoaderFunctionArgs);
163172

164173
expect(onSuccess).toHaveBeenCalledWith(expect.objectContaining({ impersonator: { email: '[email protected]' } }));
165174
});
@@ -183,7 +192,7 @@ describe('authLoader', () => {
183192
request,
184193
params: {},
185194
context: {},
186-
});
195+
} as LoaderFunctionArgs);
187196

188197
expect(onSuccess).toHaveBeenCalledWith(
189198
expect.objectContaining({
@@ -207,7 +216,7 @@ describe('authLoader', () => {
207216
request,
208217
params: {},
209218
context: {},
210-
});
219+
} as LoaderFunctionArgs);
211220

212221
// Should be a redirect response
213222
assertIsResponse(response);
@@ -242,7 +251,7 @@ describe('authLoader', () => {
242251
request,
243252
params: {},
244253
context: {},
245-
});
254+
} as LoaderFunctionArgs);
246255

247256
// Should be a redirect response
248257
assertIsResponse(response);

src/session.spec.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LoaderFunctionArgs, Session as ReactRouterSession, redirect } from 'react-router';
2-
import { AuthenticationResponse } from '@workos-inc/node';
2+
import { AuthenticationResponse, type User } from '@workos-inc/node';
33
import * as ironSession from 'iron-session';
44
import * as jose from 'jose';
55
import {
@@ -142,7 +142,9 @@ describe('session', () => {
142142
createdAt: '2021-01-01T00:00:00Z',
143143
updatedAt: '2021-01-01T00:00:00Z',
144144
lastSignInAt: '2021-01-01T00:00:00Z',
145-
},
145+
locale: null,
146+
metadata: {},
147+
} satisfies User,
146148
impersonator: undefined,
147149
headers: {},
148150
} satisfies Session;
@@ -265,11 +267,12 @@ describe('session', () => {
265267
});
266268

267269
describe('authkitLoader', () => {
268-
const createLoaderArgs = (request: Request): LoaderFunctionArgs => ({
269-
request,
270-
params: {},
271-
context: {},
272-
});
270+
const createLoaderArgs = (request: Request): LoaderFunctionArgs =>
271+
({
272+
request,
273+
params: {},
274+
context: {},
275+
}) as LoaderFunctionArgs;
273276

274277
describe('unauthenticated flows', () => {
275278
beforeEach(() => {

src/test-utils/test-helpers.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* istanbul ignore file */
22

3+
import type { User } from '@workos-inc/node';
4+
35
type SearchParamsModifier = Record<string, string> | ((params: URLSearchParams) => void);
46

57
/**
@@ -54,7 +56,9 @@ export function createAuthWithCodeResponse(overrides: Record<string, unknown> =
5456
updatedAt: '2024-01-01T00:00:00Z',
5557
lastSignInAt: '2024-01-01T00:00:00Z',
5658
externalId: null,
57-
},
59+
locale: null,
60+
metadata: {},
61+
} satisfies User,
5862
...overrides,
5963
};
6064
}

0 commit comments

Comments
 (0)