{"id":13625747,"url":"https://github.com/total-typescript/shoehorn","last_synced_at":"2025-04-08T09:06:55.790Z","repository":{"id":102344673,"uuid":"609194004","full_name":"total-typescript/shoehorn","owner":"total-typescript","description":"Work seamlessly with partial mocks in TypeScript.","archived":false,"fork":false,"pushed_at":"2024-02-26T15:26:54.000Z","size":95,"stargazers_count":460,"open_issues_count":2,"forks_count":10,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-12-26T16:25:19.649Z","etag":null,"topics":["mock","testing","typescript"],"latest_commit_sha":null,"homepage":"https://totaltypescript.com","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/total-typescript.png","metadata":{"files":{"readme":"rEaDMe.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-03T15:17:24.000Z","updated_at":"2024-12-17T21:23:45.000Z","dependencies_parsed_at":"2024-06-12T12:16:48.243Z","dependency_job_id":null,"html_url":"https://github.com/total-typescript/shoehorn","commit_stats":null,"previous_names":["mattpocock/mock-utils"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/total-typescript%2Fshoehorn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/total-typescript%2Fshoehorn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/total-typescript%2Fshoehorn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/total-typescript%2Fshoehorn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/total-typescript","download_url":"https://codeload.github.com/total-typescript/shoehorn/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247809964,"owners_count":20999816,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mock","testing","typescript"],"created_at":"2024-08-01T21:02:00.974Z","updated_at":"2025-04-08T09:06:55.760Z","avatar_url":"https://github.com/total-typescript.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# `shoehorn`\n\n```\nnpm i @total-typescript/shoehorn\n```\n\n`shoehorn` (meaning \"to force something into a space\") lets you **pass partial data in tests** while keeping TypeScript happy.\n\n### Problem\n\nUsing 'as' in tests feels bad.\n\n```ts\ntype Request = {\n  body: {\n    id: string;\n  };\n  // Imagine oodles of other properties...\n};\n\nit(\"Should get the user\", () =\u003e {\n  // Even though we only care about body.id for\n  // this test, we need to pass in the whole Request\n  // object\n  getUser({\n    body: {\n      id: \"123\",\n    },\n  } as Request);\n});\n```\n\n- You're trained not to use it\n- You need to _manually_ specify the type you want to assert to\n- For testing with incorrect data, you need to 'double-as' (`as unknown as User`)\n\n### Solution\n\n`shoehorn` gives you some first-class primitives for _safely_ providing incomplete data to tests.\n\n```ts\nimport { fromPartial } from \"@total-typescript/shoehorn\";\n\nit(\"Should get the user\", () =\u003e {\n  getUser(\n    fromPartial({\n      body: {\n        id: \"123\",\n      },\n    }),\n  );\n});\n```\n\n### But isn't passing partial data to tests bad?\n\nYes, in general. Having to pass huge objects to tests is a sign that your types are too loose. Ideally, every function should only specify the data it needs.\n\nUnfortunately, we live in the real world. There are many cases where `shoehorn` is the best choice:\n\n- **Legacy codebases**: If you're working on a large codebase, you might not have the time to refactor everything to be perfect.\n- **Third-party libraries**: If you're using a third-party library, you might not be able to alter the types without needless wrapper functions.\n\n## API\n\nFor each example below, imagine that the following types are defined:\n\n```ts\ntype Request = {\n  body: {\n    id: string;\n  };\n  // Imagine oodles of other properties...\n};\n\n// The function we're testing\nconst requiresRequest = (request: Request) =\u003e {};\n```\n\n### `fromPartial`\n\nLets you pass a deep partial to a slot expecting a type.\n\n```ts\nimport { fromPartial } from \"@total-typescript/shoehorn\";\n\nrequiresRequest(\n  fromPartial({\n    body: {\n      id: \"123\",\n    },\n  }),\n);\n```\n\nIt'll fail if you pass a type that doesn't match the one expected:\n\n```ts\n// Type \"1234123\" has no properties in common\n// with type 'PartialObjectDeep\u003cRequest\u003e'\nrequiresRequest(fromPartial(\"1234123\"));\n```\n\n### `fromAny`\n\nLets you pass anything to a slot, while still giving you autocomplete on the original type:\n\n```ts\nimport { fromAny } from \"@total-typescript/shoehorn\";\n\nrequiresRequest(\n  fromAny({\n    body: {\n      id: 124123,\n    },\n  }),\n);\n```\n\nIt WILL NOT FAIL if you pass something that doesn't match.\n\n```ts\n// All good!\nrequiresRequest(fromAny(\"1234123\"));\n```\n\n### `fromExact`\n\nA convenience method for forcing you to pass all the properties of a type. Useful for when you want to swap in and out of `fromPartial`/`fromAny`:\n\n```ts\nimport { fromExact } from \"@total-typescript/shoehorn\";\n\nrequiresRequest(\n  // Will fail! We're not passing all the oodles of\n  // properties of Request\n  fromExact({\n    body: {\n      id: 124123,\n    },\n  }),\n);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftotal-typescript%2Fshoehorn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftotal-typescript%2Fshoehorn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftotal-typescript%2Fshoehorn/lists"}