ts-jest-resolver
A resolver for jest
that uses the same strategy as TS when resolving files with JavaScript extension (".js"). It works pretty well with ts-jest
or babel
with @babel/preset-typescript
.
- 📦 Distributions in ESM and CommonJS.
- Supports both Node.js ESM (import/export) and CommonJS (require/module.exports).
- ⚡ Lightweight:
- It's bundled using rollup.js.
- 🔋 Bateries included:
- No dependencies, it just uses
jest-resolve
(which is already a jest dependency).
- No dependencies, it just uses
- ✅ Safe:
- Made with ESLint, TypeScript as strict as possible.
- Unit tests with Jest.
- Like
ts-jest
,ts-jest-resolver
uses itself as jest's resolver.
Usage
This library is published in the NPM registry and can be installed using any compatible package manager.
npm install ts-jest-resolver --save
# For Yarn, use the command below.
yarn add ts-jest-resolver
After installation, you can set "ts-jest-resolver"
as jest's resolver.
// jest.config.js
module.exports = {
preset: "ts-jest",
resolver: "ts-jest-resolver",
};
How it works
It just changes module resolution when the import statement has ".js" extension:
Ex.
import EventEmitter from './EventEmitter.js';
-
It tries to resolve the import path with ".ts" instead of ".js".
Ex.
import EventEmitter from './EventEmitter.ts';
-
If the module wasn't found, it tries to resolve the import path with ".tsx" instead of ".js".
Ex.
import EventEmitter from './EventEmitter.tsx';
-
If the module wasn't found, it resolves to the original import path (with ".js").
Ex.
import EventEmitter from './EventEmitter.js';
License
Released under MIT License.