Skip to content

A resolver for jest that uses the same strategy as TS when resolving files with JavaScript extensions (".js", ".jsx", ".cjs" and ".mjs"). It works pretty well with ts-jest or babel with @babel/preset-typescript.

License

Notifications You must be signed in to change notification settings

scott-lc/ts-jest-resolver

Repository files navigation

ts-jest-resolver

Build Status License

A resolver for jest that uses the same strategy as TS when resolving files with JavaScript extensions (".js", ".cjs" and ".mjs"). 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:
    • It just depends on jest-resolver types.
  • ✅ 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';

It does the same for ".cts" and ".mts" extensions.

Ex.

import * as colors from './colors.mjs'
import { discoverPath } from './getFiles.cjs';
  1. It tries to resolve the import path with ".mts" and ".cts" instead of ".mjs" and ".cjs".

    Ex.

    import * as colors from './colors.mts'
    import { discoverPath } from './getFiles.cts';
  2. If the module wasn't found, it resolves to the original import paths (with ".mjs" and ".cjs").

    Ex.

    import * as colors from './colors.mjs'
    import { discoverPath } from './getFiles.cjs';

License

Released under MIT License.

About

A resolver for jest that uses the same strategy as TS when resolving files with JavaScript extensions (".js", ".jsx", ".cjs" and ".mjs"). It works pretty well with ts-jest or babel with @babel/preset-typescript.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 86.1%
  • JavaScript 13.9%