question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TS imports with .js extension break Jest-resolve

See original GitHub issue

Issue :

TypeScript files using web-compatible ES6 imports cause Jest to throw an error about not being able to find the module. This error comes from jest-resolve/build/index.js:229 and happens regardless of the module type specified in ts-jest (commonjs, amd, ES6, etc.)

Expected behavior :

TypeScript supports adding a .js extension to import statements for web-compatibility (since imports require a full path, including the extension).

I would expect ts-jest to allow TypeScript to handle this and convert it to commonjs before it gets passed to jest.

Minimal repo :

/* user.ts */
export class User {
  public name : string;

  constructor(name : string) {
    this.name = name;
  }
}
/* user.spec.ts */
import { User } from './user.js';

describe('User', function() {
});

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:20
  • Comments:16

github_iconTop GitHub Comments

30reactions
dpoguecommented, Apr 9, 2019

I set up a custom resolver and just published it to npm: jest-ts-webcompat-resolver

29reactions
joeldenningcommented, Mar 15, 2022

I got things working with .js extensions in typescript imports with the following jest.config.js:

/**  @type {import('@jest/types').Config.ProjectConfig} */
const config = {
  transform: {
    "\\.[jt]sx?$": "ts-jest",
  },
  "globals": {
    "ts-jest": {
      "useESM": true
    }
  },
  moduleNameMapper: {
    "(.+)\\.js": "$1"
  },
  extensionsToTreatAsEsm: [".ts"],
};

export default config;
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript Jest imports with .js extension cause error
Finally, I've managed to solve this problem after doing some research and updating my jest config file.
Read more >
Configuring Jest
The configuration file should simply export an object: JavaScript; TypeScript. /** @type {import('jest').Config} */
Read more >
Modules: Packages | Node.js v19.3.0 Documentation
Imported CommonJS modules have their URLs converted to absolute paths and are then loaded via the CommonJS module loader. package.json and file extensions#....
Read more >
Announcing TypeScript 4.7
When a .ts file is compiled as an ES module, ECMAScript import / export ... Node.js supports two extensions to help with this:...
Read more >
SyntaxError: Cannot use import statement outside a module
js '? BREAKING CHANGE: The request '@redwoodjs/web/apollo' failed to resolve only because it was resolved as fully specified (probably because ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found