Skip to content

Commit 3d8f497

Browse files
committed
Do _fail without an if statement and relocate test case
1 parent cd1963c commit 3d8f497

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

packages/auth/src/api/index.test.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ describe('api/_performApiRequest', () => {
6060
});
6161

6262
context('with regular requests', () => {
63-
beforeEach(mockFetch.setUp);
6463
afterEach(mockFetch.tearDown);
6564

6665
it('should set the correct request, method and HTTP Headers', async () => {
66+
mockFetch.setUp();
6767
const mock = mockEndpoint(Endpoint.SIGN_UP, serverResponse);
6868
const response = await _performApiRequest<
6969
typeof request,
@@ -82,6 +82,7 @@ describe('api/_performApiRequest', () => {
8282
});
8383

8484
it('should set the device language if available', async () => {
85+
mockFetch.setUp();
8586
auth.languageCode = 'jp';
8687
const mock = mockEndpoint(Endpoint.SIGN_UP, serverResponse);
8788
const response = await _performApiRequest<
@@ -95,6 +96,7 @@ describe('api/_performApiRequest', () => {
9596
});
9697

9798
it('should include whatever headers the auth impl attaches', async () => {
99+
mockFetch.setUp();
98100
sinon.stub(auth, '_getAdditionalHeaders').returns(
99101
Promise.resolve({
100102
'look-at-me-im-a-header': 'header-value',
@@ -118,6 +120,7 @@ describe('api/_performApiRequest', () => {
118120
});
119121

120122
it('should set the framework in clientVersion if logged', async () => {
123+
mockFetch.setUp();
121124
auth._logFramework('Mythical');
122125
const mock = mockEndpoint(Endpoint.SIGN_UP, serverResponse);
123126
const response = await _performApiRequest<
@@ -143,6 +146,7 @@ describe('api/_performApiRequest', () => {
143146
});
144147

145148
it('should translate server errors to auth errors', async () => {
149+
mockFetch.setUp();
146150
const mock = mockEndpoint(
147151
Endpoint.SIGN_UP,
148152
{
@@ -172,6 +176,7 @@ describe('api/_performApiRequest', () => {
172176
});
173177

174178
it('should translate server success with errorMessage into auth error', async () => {
179+
mockFetch.setUp();
175180
const response = {
176181
errorMessage: ServerError.FEDERATED_USER_ID_ALREADY_LINKED,
177182
idToken: 'foo-bar'
@@ -193,6 +198,7 @@ describe('api/_performApiRequest', () => {
193198
});
194199

195200
it('should translate complex server errors to auth errors', async () => {
201+
mockFetch.setUp();
196202
const mock = mockEndpoint(
197203
Endpoint.SIGN_UP,
198204
{
@@ -222,6 +228,7 @@ describe('api/_performApiRequest', () => {
222228
});
223229

224230
it('should pass through server messages if applicable', async () => {
231+
mockFetch.setUp();
225232
mockEndpoint(
226233
Endpoint.SIGN_UP,
227234
{
@@ -247,6 +254,7 @@ describe('api/_performApiRequest', () => {
247254
});
248255

249256
it('should handle unknown server errors', async () => {
257+
mockFetch.setUp();
250258
const mock = mockEndpoint(
251259
Endpoint.SIGN_UP,
252260
{
@@ -276,6 +284,7 @@ describe('api/_performApiRequest', () => {
276284
});
277285

278286
it('should support custom error handling per endpoint', async () => {
287+
mockFetch.setUp();
279288
const mock = mockEndpoint(
280289
Endpoint.SIGN_UP,
281290
{
@@ -306,6 +315,22 @@ describe('api/_performApiRequest', () => {
306315
);
307316
expect(mock.calls[0].request).to.eql(request);
308317
});
318+
319+
it('should handle non-FirebaseErrors', async () => {
320+
mockFetch.setUpWithOverride(() => {
321+
return new Promise<never>((_, reject) => reject(new Error('error')));
322+
});
323+
const promise = _performApiRequest<typeof request, never>(
324+
auth,
325+
HttpMethod.POST,
326+
Endpoint.SIGN_UP,
327+
request
328+
);
329+
await expect(promise).to.be.rejectedWith(
330+
FirebaseError,
331+
'auth/internal-error'
332+
);
333+
});
309334
});
310335

311336
context('with network issues', () => {
@@ -345,22 +370,6 @@ describe('api/_performApiRequest', () => {
345370
expect(clock.clearTimeout).to.have.been.called;
346371
clock.restore();
347372
});
348-
349-
it('should handle failures', async () => {
350-
mockFetch.setUpWithOverride(() => {
351-
return new Promise<never>((_, reject) => reject(new Error('error')));
352-
});
353-
const promise = _performApiRequest<typeof request, never>(
354-
auth,
355-
HttpMethod.POST,
356-
Endpoint.SIGN_UP,
357-
request
358-
);
359-
await expect(promise).to.be.rejectedWith(
360-
FirebaseError,
361-
'auth/internal-error'
362-
);
363-
});
364373
});
365374

366375
context('edgcase error mapping', () => {

packages/auth/src/api/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,7 @@ export async function _performFetchWithErrorHandling<V>(
181181
if (e instanceof FirebaseError) {
182182
throw e;
183183
}
184-
if (e instanceof Error) {
185-
_fail(auth, AuthErrorCode.INTERNAL_ERROR, e);
186-
} else {
187-
_fail(auth, AuthErrorCode.INTERNAL_ERROR, String(e));
188-
}
184+
_fail(auth, AuthErrorCode.INTERNAL_ERROR, { 'message': String(e) });
189185
}
190186
}
191187

packages/auth/src/core/util/assert.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ export function _fail<K extends AuthErrorCode>(
5858
code: K,
5959
...data: {} extends LessAppName<K> ? [LessAppName<K>?] : [LessAppName<K>]
6060
): never;
61-
export function _fail<K extends AuthErrorCode>(
62-
auth: Auth,
63-
code: K,
64-
errorMessage: string,
65-
...data: {} extends LessAppName<K> ? [LessAppName<K>?] : [LessAppName<K>]
66-
): never;
6761
export function _fail<K extends AuthErrorCode>(
6862
authOrCode: Auth | K,
6963
...rest: unknown[]

0 commit comments

Comments
 (0)