Skip to content

Commit

Permalink
refactor: update to match latest eslint best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
Xunnamius committed Oct 27, 2021
1 parent 14a4c43 commit c7f5a82
Show file tree
Hide file tree
Showing 12 changed files with 359 additions and 345 deletions.
2 changes: 1 addition & 1 deletion external-scripts/ban-hammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export default async function main() {

log('execution complete');
} catch (e) {
throw new ExternalError(e.message || e.toString());
throw new ExternalError(`${e instanceof Error ? e.message : e}`);
}
}

Expand Down
6 changes: 4 additions & 2 deletions external-scripts/initialize-data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ export default async function main() {
};

log('shuffling corpus data');
const usernames = fastShuffle(rawUsernames).slice(INIT_DATA_USERS);
const usernames = fastShuffle<string>(Date.now(), rawUsernames).slice(
INIT_DATA_USERS
);

log(`selecting ${INIT_DATA_USERS}/${rawUsernames.length} usernames`);
log(
Expand Down Expand Up @@ -239,7 +241,7 @@ export default async function main() {
]);
} catch (e) {
errorCount += 1;
logUser.extend('<exception>')(e.message || e);
logUser.extend('<exception>')(e instanceof Error ? e.message : e);
debug(e);

if (errorCount >= MAX_ERROR_THRESHOLD) {
Expand Down
2 changes: 1 addition & 1 deletion external-scripts/prune-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default async function main() {
await closeDb();
log('execution complete');
} catch (e) {
throw new ExternalError(e.message || e.toString());
throw new ExternalError(`${e instanceof Error ? e.message : e}`);
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export async function handleImageUpload(
body
});

const json: ImgurApiResponse = await res.json();
const json = (await res.json()) as ImgurApiResponse;

imageUrl =
json.data.link ||
Expand All @@ -241,7 +241,9 @@ export async function handleImageUpload(
);
} catch (e) {
// eslint-disable-next-line no-console
console.error(`image upload failure reason: ${e.message || e}`);
console.error(
`image upload failure reason: ${e instanceof Error ? e.message : e}`
);
throw new AppError('image upload failed');
}

Expand Down
2 changes: 1 addition & 1 deletion src/backend/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,6 @@ export async function wrapHandler(
}
}
} catch (error) {
await handleError(res, error);
await handleError(res, error as Error);
}
}
4 changes: 3 additions & 1 deletion test/unit-api-info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ describe('api/v1/info', () => {
await testApiHandler({
handler: api.info,
test: async ({ fetch }) => {
expect(await fetch({ headers: { KEY } }).then((r) => r.json())).toStrictEqual({
await expect(
fetch({ headers: { KEY } }).then((r) => r.json())
).resolves.toStrictEqual({
success: true,
totalMemes: dummyDbData.memes.length,
totalUsers: dummyDbData.users.length,
Expand Down
54 changes: 30 additions & 24 deletions test/unit-api-memes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,16 @@ describe('api/v1/memes', () => {
await testApiHandler({
handler: api.memes,
test: async ({ fetch }) => {
expect(
await fetch({
await expect(
fetch({
method: 'POST',
headers: { KEY, 'content-type': 'application/json' },
body: JSON.stringify({})
}).then(async (r) => [r.status, await r.json()])
).toStrictEqual([200, expect.objectContaining({ meme: expect.anything() })]);
).resolves.toStrictEqual([
200,
expect.objectContaining({ meme: expect.anything() })
]);
}
});
});
Expand Down Expand Up @@ -185,9 +188,9 @@ describe('api/v1/memes', () => {
>
);

expect(await fetch({ headers: { KEY } }).then((r) => r.json())).toStrictEqual(
{ success: true, memes: expect.any(Array) }
);
await expect(
fetch({ headers: { KEY } }).then((r) => r.json())
).resolves.toStrictEqual({ success: true, memes: expect.any(Array) });
}
}
});
Expand All @@ -204,7 +207,7 @@ describe('api/v1/memes', () => {
Promise.resolve([]) as unknown as ReturnType<typeof mockedGetMemes>
);

expect(await fetch().then((r) => r.status)).toStrictEqual(404);
await expect(fetch().then((r) => r.status)).resolves.toBe(404);
}
});
});
Expand All @@ -216,7 +219,7 @@ describe('api/v1/memes', () => {
params: { meme_ids: ['invalid-id'] },
handler: api.memesIds,
test: async ({ fetch }) => {
expect(await fetch({ headers: { KEY } }).then((r) => r.status)).toStrictEqual(
await expect(fetch({ headers: { KEY } }).then((r) => r.status)).resolves.toBe(
400
);
}
Expand Down Expand Up @@ -262,9 +265,9 @@ describe('api/v1/memes', () => {
params: { meme_ids: ['invalid-id'] },
handler: api.memesIds,
test: async ({ fetch }) => {
expect(
await fetch({ method: 'PUT', headers: { KEY } }).then((r) => r.status)
).toStrictEqual(400);
await expect(
fetch({ method: 'PUT', headers: { KEY } }).then((r) => r.status)
).resolves.toBe(400);
}
});
});
Expand All @@ -287,11 +290,12 @@ describe('api/v1/memes', () => {
test: async ({ fetch }) => {
for (const [expectedParams, expectedStatus] of factory) {
Object.assign(params, expectedParams);
expect(
await fetch(expectedStatus != 200 ? { headers: { KEY } } : {}).then(
async (r) => [r.status, await r.json()]
)
).toStrictEqual([
await expect(
fetch(expectedStatus != 200 ? { headers: { KEY } } : {}).then(async (r) => [
r.status,
await r.json()
])
).resolves.toStrictEqual([
expectedStatus,
expectedStatus == 200
? { success: true, users: expect.any(Array) }
Expand Down Expand Up @@ -378,7 +382,9 @@ describe('api/v1/memes', () => {
test: async ({ fetch }) => {
for (const [expectedParams, expectedStatus] of factory) {
Object.assign(params, expectedParams);
expect(await fetch().then((r) => r.status)).toStrictEqual(expectedStatus);
await expect(fetch().then((r) => r.status)).resolves.toStrictEqual(
expectedStatus
);
}
}
});
Expand All @@ -396,7 +402,7 @@ describe('api/v1/memes', () => {
},
handler: api.memesIdLikesId,
test: async ({ fetch }) => {
expect(await fetch({ headers: { KEY } }).then((r) => r.status)).toStrictEqual(
await expect(fetch({ headers: { KEY } }).then((r) => r.status)).resolves.toBe(
404
);
}
Expand Down Expand Up @@ -429,9 +435,9 @@ describe('api/v1/memes', () => {
test: async ({ fetch }) => {
for (const [expectedParams, expectedStatus] of factory) {
Object.assign(params, expectedParams);
expect(
await fetch({ method: 'DELETE', headers: { KEY } }).then((r) => r.status)
).toStrictEqual(expectedStatus);
await expect(
fetch({ method: 'DELETE', headers: { KEY } }).then((r) => r.status)
).resolves.toStrictEqual(expectedStatus);
}
}
});
Expand Down Expand Up @@ -463,9 +469,9 @@ describe('api/v1/memes', () => {
test: async ({ fetch }) => {
for (const [expectedParams, expectedStatus] of factory) {
Object.assign(params, expectedParams);
expect(
await fetch({ method: 'PUT', headers: { KEY } }).then((r) => r.status)
).toStrictEqual(expectedStatus);
await expect(
fetch({ method: 'PUT', headers: { KEY } }).then((r) => r.status)
).resolves.toStrictEqual(expectedStatus);
}
}
});
Expand Down
Loading

0 comments on commit c7f5a82

Please sign in to comment.