Skip to content

Instantly share code, notes, and snippets.

@cjcenizal
Created August 2, 2024 23:26
Show Gist options
  • Save cjcenizal/3ea34390d081bf0a38747988d2d98aa1 to your computer and use it in GitHub Desktop.
Save cjcenizal/3ea34390d081bf0a38747988d2d98aa1 to your computer and use it in GitHub Desktop.
Testability
// What are some things that make this code difficult to test, and how would you address them?
import { config } from "../constants/config";
import { sendRequest } from "../services/sendRequest";
import { notifications } from "../services/notifications";
export async function fetchUser(userModel) {
const {
server: {
api: { baseUrl },
},
} = config;
const {
meta: { id },
} = userModel;
const method = "GET";
const url = `${baseUrl}/users/${id}`;
const startTime = Date.now();
try {
return {
user: await sendRequest[method](url),
roundTripTime: Date.now() - startTime,
};
} catch (e) {
if (e.statusCode === 404) {
notifications.showError(`User with ID ${id} does not exist`);
}
throw e;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment