{"id":18584472,"url":"https://github.com/ipfs-examples/helia-jest-typescript","last_synced_at":"2026-03-02T08:34:11.356Z","repository":{"id":186975341,"uuid":"676087090","full_name":"ipfs-examples/helia-jest-typescript","owner":"ipfs-examples","description":"Test Helia with Jest and TypeScript","archived":false,"fork":false,"pushed_at":"2025-05-01T08:39:11.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-07T15:54:32.069Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ipfs-examples.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"zenodo":null}},"created_at":"2023-08-08T12:02:11.000Z","updated_at":"2025-05-01T07:36:43.000Z","dependencies_parsed_at":"2024-01-19T16:42:43.348Z","dependency_job_id":"22f1c55f-2179-4568-9ada-5a2ed55bcb54","html_url":"https://github.com/ipfs-examples/helia-jest-typescript","commit_stats":null,"previous_names":["ipfs-examples/helia-jest-typescript"],"tags_count":0,"template":false,"template_full_name":"ipfs-examples/example-fork-go-template","purl":"pkg:github/ipfs-examples/helia-jest-typescript","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipfs-examples%2Fhelia-jest-typescript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipfs-examples%2Fhelia-jest-typescript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipfs-examples%2Fhelia-jest-typescript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipfs-examples%2Fhelia-jest-typescript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ipfs-examples","download_url":"https://codeload.github.com/ipfs-examples/helia-jest-typescript/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipfs-examples%2Fhelia-jest-typescript/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29995912,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-02T01:47:34.672Z","status":"online","status_checked_at":"2026-03-02T02:00:07.342Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-11-07T00:27:38.270Z","updated_at":"2026-03-02T08:34:11.339Z","avatar_url":"https://github.com/ipfs-examples.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/ipfs/helia\" title=\"Helia\"\u003e\n    \u003cimg src=\"https://raw.githubusercontent.com/ipfs/helia/main/assets/helia.png\" alt=\"Helia logo\" width=\"300\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n\u003ch3 align=\"center\"\u003e\u003cb\u003eTest Helia with Jest and TypeScript\u003c/b\u003e\u003c/h3\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/jlord/forkngo/gh-pages/badges/cobalt.png\" width=\"200\"\u003e\n  \u003cbr\u003e\n  \u003ca href=\"https://ipfs.github.io/helia/modules/helia.html\"\u003eExplore the docs\u003c/a\u003e\n  ·\n  \u003ca href=\"https://github.com/ipfs-examples/helia-examples/issues\"\u003eReport Bug\u003c/a\u003e\n  ·\n  \u003ca href=\"https://github.com/ipfs-examples/helia-examples/issues\"\u003eRequest Feature/Example\u003c/a\u003e\n\u003c/p\u003e\n\n## About\n\n[Jest](https://jestjs.io/) is a JavaScript testing framework that transpiles code on the fly to enable a fast-feedback testing cycle for the developer.\n\nIt is typically used to test browser code, whereby it injects an artificial DOM implementation into the global scope to enable running the actual tests on [Node.js](https://nodejs.org) to take advantage of faster startup times at the cost of not always having 100% feature/behavior parity with the actual target platform.\n\nIn some circumstances this is helpful but it can also result in hard to debug errors, particularly around resolving modules and loading them.\n\nSome developers also use Jest to test pure Node.js code which the benefit of familiarity but also has the same issues around transpilation.\n\nAdding [TypeScript](https://www.typescriptlang.org/) into the mix adds another layer of misdirection so the error messages are more cryptic than just running plain JS.\n\n```\nCannot find module 'helia' from '../src/index.ts'\n\nRequire stack:\n  /path/to/project/src/index.ts\n  index.spec.ts\n\n\u003e 1 | import { createHelia } from 'helia'\n  2 | import type { Helia } from '@helia/interface'\n```\n\nIn this example we are going to cover the config necessary to use Jest with TypeScript that compiles to ESM modules.\n\n### 1. TypeScript and ESM\n\nPlease see the [helia-typescript](https://github.com/ipfs-examples/helia-typescript) example to ensure your application is building your TypeScript source to ESM correctly.\n\nAt the very minimum you should check the compiled output of your application to ensure no calls to `require` are present and instead all modules are loaded via `import`.\n\n### 2. `ts-jest` Transform\n\n[ts-jest](https://www.npmjs.com/package/ts-jest) is a Jest transformer for your `.ts` source files that are under test.\n\nBy default it will transpile code to [CommonJS](https://en.wikipedia.org/wiki/CommonJS) which is undesirable if your codebase is designed to be consumed as ESM.\n\nTo get it to transform code to ESM, the [useESM](https://kulshekhar.github.io/ts-jest/docs/next/getting-started/options/useESM/) option must be set:\n\n**jest.config.json**\n\n```json\n{\n  \"transform\": {\n    \"^.+\\\\.(t|j)s$\": [\"ts-jest\", {\n      \"useESM\": true\n    }]\n  }\n}\n```\n\n### 3. `ts-jest` Presets\n\nBy default `ts-jest` will not be able to load ES Modules:\n\n```\nFAIL  test/index.spec.ts\n ● Test suite failed to run\n\n  Cannot find module 'helia' from '../src/index.ts'\n\n  Require stack:\n    /Users/alex/Documents/Workspaces/ipfs-examples/helia-examples/examples/helia-jest-typescript/src/index.ts\n    index.spec.ts\n\n  \u003e 1 | import { createHelia } from 'helia'\n      | ^\n    2 | import type { Helia } from '@helia/interface'\n```\n\nTo fix this it is necessary to tell Jest to use the `ts-jest` `default-esm` preset:\n\n**jest.config.json**\n\n```json\n{\n  \"preset\": \"ts-jest/presets/default-esm\"\n}\n```\n\n### 4. Importing other files from the same project\n\nWhen authoring ESM, [file extensions are mandatory](https://nodejs.org/api/esm.html#esm_mandatory_file_extensions), however `ts-jest` will not resolve (for example) `./index.ts` when importing `./index.js`:\n\n```\nCannot find module '../src/index.js' from 'index.spec.ts'\n\n  at Resolver._throwModNotFoundError (../../../node_modules/jest-resolve/build/resolver.js:427:11)\n```\n\nThe solution is to use a module name mapper to remove the file extension from relative imports and then Jest can fall back to it's version of the Node.js [require.resolve algorithm](https://nodejs.org/api/modules.html#all-together) which will locate `index.ts` from an `index.js` import:\n\n**jest.config.json**\n\n```json\n{\n  \"moduleNameMapper\": {\n    \"^(\\\\.{1,2}/.*)\\\\.[jt]s$\": \"$1\"\n  }\n}\n```\n\n## Putting it all together\n\nYour `jest.config.json` should look something like this:\n\n```json\n{\n  \"moduleFileExtensions\": [\n    \"js\",\n    \"json\",\n    \"ts\"\n  ],\n  \"rootDir\": \"test\",\n  \"testRegex\": \".*\\\\.spec\\\\.ts$\",\n  \"transform\": {\n    \"^.+\\\\.(t|j)s$\": [\"ts-jest\", {\n      \"useESM\": true\n    }]\n  },\n  \"preset\": \"ts-jest/presets/default-esm\",\n  \"moduleNameMapper\": {\n    \"^(\\\\.{1,2}/.*)\\\\.[jt]s$\": \"$1\"\n  }\n}\n```\n\nWe can now run the tests:\n\n```console\n% NODE_OPTIONS=--experimental-vm-modules jest\n\n PASS  test/index.spec.ts (6.055 s)\n  Helia\n    libp2p\n      ✓ should have a peer id (519 ms)\n\nTest Suites: 1 passed, 1 total\nTests:       1 passed, 1 total\nSnapshots:   0 total\nTime:        6.134 s\nRan all test suites.\n```\n\nThat's it! You just successfully ran a suite that can test your Helia application.\n\n## About The Project\n\n- Read the [docs](https://ipfs.github.io/helia/modules/helia.html)\n- Look into other [examples](https://github.com/ipfs-examples/helia-examples) to learn how to spawn a Helia node in Node.js and in the Browser\n- Visit https://dweb-primer.ipfs.io to learn about IPFS and the concepts that underpin it\n- Head over to https://proto.school to take interactive tutorials that cover core IPFS APIs\n- Check out https://docs.ipfs.io for tips, how-tos and more\n- See https://blog.ipfs.io for news and more\n- Need help? Please ask 'How do I?' questions on https://discuss.ipfs.io\n\n## Getting Started\n\n### Prerequisites\n\nMake sure you have installed all of the following prerequisites on your development machine:\n\n- Git - [Download \u0026 Install Git](https://git-scm.com/downloads). OSX and Linux machines typically have this already installed.\n- Node.js - [Download \u0026 Install Node.js](https://nodejs.org/en/download/) and the npm package manager.\n\n### Installation and Running example\n\n```console\n\u003e npm install\n\u003e npm test\n```\n\n## Documentation\n\n- [IPFS Primer](https://dweb-primer.ipfs.io/)\n- [IPFS Docs](https://docs.ipfs.io/)\n- [Tutorials](https://proto.school)\n- [More examples](https://github.com/ipfs-examples/helia-examples)\n- [API - Helia](https://ipfs.github.io/helia/modules/helia.html)\n- [API - @helia/unixfs](https://ipfs.github.io/helia-unixfs/modules/helia.html)\n\n## Contributing\n\nContributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**.\n\n1. Fork the IPFS Project\n2. Create your Feature Branch (`git checkout -b feature/amazing-feature`)\n3. Commit your Changes (`git commit -a -m 'feat: add some amazing feature'`)\n4. Push to the Branch (`git push origin feature/amazing-feature`)\n5. Open a Pull Request\n\n## Want to hack on IPFS?\n\n[![](https://cdn.rawgit.com/jbenet/contribute-ipfs-gif/master/img/contribute.gif)](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md)\n\nThe IPFS implementation in JavaScript needs your help! There are a few things you can do right now to help out:\n\nRead the [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md) and [JavaScript Contributing Guidelines](https://github.com/ipfs/community/blob/master/CONTRIBUTING_JS.md).\n\n- **Check out existing issues** The [issue list](https://github.com/ipfs/helia/issues) has many that are marked as ['help wanted'](https://github.com/ipfs/helia/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3A%22help+wanted%22) or ['difficulty:easy'](https://github.com/ipfs/helia/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+label%3Adifficulty%3Aeasy) which make great starting points for development, many of which can be tackled with no prior IPFS knowledge\n- **Look at the [Helia Roadmap](https://github.com/ipfs/helia/blob/main/ROADMAP.md)** This are the high priority items being worked on right now\n- **Perform code reviews** More eyes will help\n  a. speed the project along\n  b. ensure quality, and\n  c. reduce possible future bugs\n- **Add tests**. There can never be enough tests\n\n[cid]: https://docs.ipfs.tech/concepts/content-addressing  \"Content Identifier\"\n[Uint8Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array\n[libp2p]: https://libp2p.io\n[IndexedDB]: https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API\n[S3]: https://aws.amazon.com/s3/","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fipfs-examples%2Fhelia-jest-typescript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fipfs-examples%2Fhelia-jest-typescript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fipfs-examples%2Fhelia-jest-typescript/lists"}