Explore the docs
·
Report Bug
·
Request Feature/Example
Jest is a JavaScript testing framework that transpiles code on the fly to enable a fast-feedback testing cycle for the developer.
It 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 to take advantage of faster startup times at the cost of not always having 100% feature/behavior parity with the actual target platform.
In some circumstances this is helpful but it can also result in hard to debug errors, particularly around resolving modules and loading them.
Some developers also use Jest to test pure Node.js code which the benefit of familiarity but also has the same issues around transpilation.
Adding TypeScript into the mix adds another layer of misdirection so the error messages are more cryptic than just running plain JS.
Cannot find module 'helia' from '../src/index.ts'
Require stack:
/path/to/project/src/index.ts
index.spec.ts
> 1 | import { createHelia } from 'helia'
2 | import type { Helia } from '@helia/interface'
In this example we are going to cover the config necessary to use Jest with TypeScript that compiles to ESM modules.
Please see the helia-typescript example to ensure your application is building your TypeScript source to ESM correctly.
At 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
.
ts-jest is a Jest transformer for your .ts
source files that are under test.
By default it will transpile code to CommonJS which is undesirable if your codebase is designed to be consumed as ESM.
To get it to transform code to ESM, the useESM option must be set:
jest.config.json
{
"transform": {
"^.+\\.(t|j)s$": ["ts-jest", {
"useESM": true
}]
}
}
By default ts-jest
will not be able to load ES Modules:
FAIL test/index.spec.ts
● Test suite failed to run
Cannot find module 'helia' from '../src/index.ts'
Require stack:
/Users/alex/Documents/Workspaces/ipfs-examples/helia-examples/examples/helia-jest-typescript/src/index.ts
index.spec.ts
> 1 | import { createHelia } from 'helia'
| ^
2 | import type { Helia } from '@helia/interface'
To fix this it is necessary to tell Jest to use the ts-jest
default-esm
preset:
jest.config.json
{
"preset": "ts-jest/presets/default-esm"
}
When authoring ESM, file extensions are mandatory, however ts-jest
will not resolve (for example) ./index.ts
when importing ./index.js
:
Cannot find module '../src/index.js' from 'index.spec.ts'
at Resolver._throwModNotFoundError (../../../node_modules/jest-resolve/build/resolver.js:427:11)
The 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 which will locate index.ts
from an index.js
import:
jest.config.json
{
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.[jt]s$": "$1"
}
}
Your jest.config.json
should look something like this:
{
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "test",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": ["ts-jest", {
"useESM": true
}]
},
"preset": "ts-jest/presets/default-esm",
"moduleNameMapper": {
"^(\\.{1,2}/.*)\\.[jt]s$": "$1"
}
}
We can now run the tests:
% NODE_OPTIONS=--experimental-vm-modules jest
PASS test/index.spec.ts (6.055 s)
Helia
libp2p
✓ should have a peer id (519 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 6.134 s
Ran all test suites.
That's it! You just successfully ran a suite that can test your Helia application.
- Read the docs
- Look into other examples to learn how to spawn a Helia node in Node.js and in the Browser
- Visit https://dweb-primer.ipfs.io to learn about IPFS and the concepts that underpin it
- Head over to https://proto.school to take interactive tutorials that cover core IPFS APIs
- Check out https://docs.ipfs.io for tips, how-tos and more
- See https://blog.ipfs.io for news and more
- Need help? Please ask 'How do I?' questions on https://discuss.ipfs.io
Make sure you have installed all of the following prerequisites on your development machine:
- Git - Download & Install Git. OSX and Linux machines typically have this already installed.
- Node.js - Download & Install Node.js and the npm package manager.
> npm install
> npm test
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the IPFS Project
- Create your Feature Branch (
git checkout -b feature/amazing-feature
) - Commit your Changes (
git commit -a -m 'feat: add some amazing feature'
) - Push to the Branch (
git push origin feature/amazing-feature
) - Open a Pull Request
The IPFS implementation in JavaScript needs your help! There are a few things you can do right now to help out:
Read the Code of Conduct and JavaScript Contributing Guidelines.
- Check out existing issues The issue list has many that are marked as 'help wanted' or 'difficulty:easy' which make great starting points for development, many of which can be tackled with no prior IPFS knowledge
- Look at the Helia Roadmap This are the high priority items being worked on right now
- Perform code reviews More eyes will help a. speed the project along b. ensure quality, and c. reduce possible future bugs
- Add tests. There can never be enough tests