Skip to content

ipfs-examples/helia-jest-typescript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Helia logo

Test Helia with Jest and TypeScript


Explore the docs · Report Bug · Request Feature/Example

About

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.

1. TypeScript and ESM

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.

2. ts-jest Transform

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
    }]
  }
}

3. ts-jest Presets

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"
}

4. Importing other files from the same project

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"
  }
}

Putting it all together

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.

About The Project

Getting Started

Prerequisites

Make sure you have installed all of the following prerequisites on your development machine:

Installation and Running example

> npm install
> npm test

Documentation

Contributing

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.

  1. Fork the IPFS Project
  2. Create your Feature Branch (git checkout -b feature/amazing-feature)
  3. Commit your Changes (git commit -a -m 'feat: add some amazing feature')
  4. Push to the Branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Want to hack on IPFS?

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

About

Test Helia with Jest and TypeScript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published