{"id":21467925,"url":"https://github.com/danr/doctest-ts","last_synced_at":"2025-07-15T05:32:12.943Z","repository":{"id":29941364,"uuid":"109299014","full_name":"danr/doctest-ts","owner":"danr","description":"doctests for typescript","archived":false,"fork":false,"pushed_at":"2023-01-02T01:09:22.000Z","size":224,"stargazers_count":23,"open_issues_count":8,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-16T06:34:19.916Z","etag":null,"topics":["api","doctest","doctests","documentation","generator","tdd","testing","typescript"],"latest_commit_sha":null,"homepage":"","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/danr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-02T17:45:04.000Z","updated_at":"2023-10-02T07:17:52.000Z","dependencies_parsed_at":"2023-01-14T15:58:14.799Z","dependency_job_id":null,"html_url":"https://github.com/danr/doctest-ts","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danr%2Fdoctest-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danr%2Fdoctest-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danr%2Fdoctest-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danr%2Fdoctest-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danr","download_url":"https://codeload.github.com/danr/doctest-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226019007,"owners_count":17560759,"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":["api","doctest","doctests","documentation","generator","tdd","testing","typescript"],"created_at":"2024-11-23T08:20:36.014Z","updated_at":"2024-11-23T08:20:37.452Z","avatar_url":"https://github.com/danr.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# doctest-ts: doctests for TypeScript\n\nSay you have a file src/hasFoo.ts with a function like hasFoo:\n\n```typescript\nfunction hasFoo(s: string): boolean {\n  return null != s.match(/foo/i)\n}\n```\n\nYou can now make documentation and unit tests for this function in one go:\n\n```typescript\n/** Does this string contain foo, ignoring case?\n\n    hasFoo('___foo__') // =\u003e true\n    hasFoo('   fOO  ') // =\u003e true\n    hasFoo('Foo.') // =\u003e true\n    hasFoo('bar') // =\u003e false\n    hasFoo('fo') // =\u003e false\n    hasFoo('oo') // =\u003e false\n\n*/\nfunction hasFoo(s: string): boolean {\n  return null != s.match(/foo/i)\n}\n```\n\nSince the function is not exported we can only test this by either editing or copying the entire file and gluing on tests at the end.\nThis library goes for the second approach: making a copy of the file with the translated tests at the end. Run it like so:\n\n```sh\n$ doctest-ts src/hasFoo.ts\nWriting src/hasFoo.doctest.ts\n```\n\nThe contents of `src/hasFoo.doctest.ts` is the original file prepended to the doctests rewritten as unit tests.\n\n```typescript\n/** Does this string contain foo, ignoring case?\n\n    hasFoo('___foo__') // =\u003e true\n    hasFoo('   fOO  ') // =\u003e true\n    hasFoo('Foo.') // =\u003e true\n    hasFoo('bar') // =\u003e false\n    hasFoo('fo') // =\u003e false\n    hasFoo('oo') // =\u003e false\n\n*/\nfunction hasFoo(s: string): boolean {\n  return null != s.match(/foo/i)\n}\n\nimport * as __test from \"tape\"\n__test(\"hasFoo\", t =\u003e {t.deepEqual(hasFoo(\"___foo__\"), true, \"true\")\nt.deepEqual(hasFoo(\"   fOO  \"), true, \"true\")\nt.deepEqual(hasFoo(\"Foo.\"), true, \"true\")\nt.deepEqual(hasFoo(\"bar\"), false, \"false\")\nt.deepEqual(hasFoo(\"fo\"), false, \"false\")\nt.deepEqual(hasFoo(\"oo\"), false, \"false\")\n;t.end()})\n```\n\nThis can now be run with the tape runner or ts-node:\n\n```\n$ ts-node src/hasFoo.doctest.ts | tap-diff\n  hasFoo\n    ✔  true\n    ✔  true\n    ✔  true\n    ✔  false\n    ✔  false\n    ✔  false\n        Done in 0.37s.\n\npassed: 6  failed: 0  of 6 tests  (171ms)\n\nAll of 6 tests passed!\n```\n\nThere are four different outputs available:\n\n* tape\n* AVA\n* jest\n* mocha (using chai)\n\nPull requests for other test runners are welcome.\n\n## Watching file changes\n\nWe can tell `doctest-ts` to watch for file changes and report which files it has written.\nIt tries to be a good unix citizen and thus writes the files it has created on stdout (and some info on stderr).\nThis makes it possible to run test runners on each line on stdout like so:\n\n```sh\nts-node src/main.ts --watch src/hasFo.ts |\nwhile read file; do echo running tape on $file; ts-node $file | tap-diff; done\n```\n\nLet's say we remove the ignore case `i` flag from the regex in `hasFoo`. We get this output (automatically):\n```\nWriting src/hasFoo.doctest.ts\nrunning tape on src/hasFoo.doctest.ts\n\n  hasFoo\n    ✔  true\n    ✖  true at Test.t (src/hasFoo.doctest.ts:18:3)\n        [-false-][+true+]\n    ✖  true at Test.t (src/hasFoo.doctest.ts:19:3)\n        [-false-][+true+]\n    ✔  false\n    ✔  false\n    ✔  false\n\npassed: 4  failed: 2  of 6 tests  (264ms)\n\n2 of 6 tests failed.\n```\n\n# License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanr%2Fdoctest-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanr%2Fdoctest-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanr%2Fdoctest-ts/lists"}