PabloSzx / 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.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ts-jest-resolver

Build Status License

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).
  • ✅ Safe:

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';
  1. It tries to resolve the import path with ".ts" instead of ".js".

    Ex.

    import EventEmitter from './EventEmitter.ts';
  2. If the module wasn't found, it tries to resolve the import path with ".tsx" instead of ".js".

    Ex.

    import EventEmitter from './EventEmitter.tsx';
  3. 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.

About

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.

License:MIT License


Languages

Language:TypeScript 71.7%Language:JavaScript 28.3%