-
Notifications
You must be signed in to change notification settings - Fork 200
Closed
Description
- Decide on a good, standard framework for testing in Typescript (vitest, jest, use bun and use bun:test?)
- Convert as many functions as possible to pure, deterministic functions
- Generate unit tests for these functions that cover reasonable edge cases
Markdownify could be refactored into a set of pure functions (no more class) and those functions could be good candidates for unit tests.
What makes a good candidate for a unit test?
- We don't want to test anything that is primarily logic in another library
- We don't want to test anything that includes "integration" logic such as HTTP calls, database writes, file system I/O etc. In these cases we want to extract out the elements that are "pure" (i.e. that do not make HTTP calls, db writes, file system I/O) and test those specifically.
Let's imagine we have a class like the following:
BEFORE:
import fetchMyApi from "myApi";
class TestClass {
myMethod(param: string) {
const response = await fetchMyApi(param);
return response.json()
}
}AFTER:
// EXPLICIT DEPENDENCY INJECTION FOR EVERYTHING THAT THE FUNCTION NEEDS
export async function myMethod(param: string, fetcher: FetcherFunction<MyResponse>) {
const response = await fetcher(param);
return response.json()
}async function myMockFetch() {
return {
myMockPayloadProperty: 'hello world'
}
}
test(myMethod('hello', myMockFetch), 'my expected output');When we decide to test the function, we can use a mock to simulate the fetch rather than rely on the external dependency that's implicit.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels