TS imports with .js extension break Jest-resolve
See original GitHub issueIssue :
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:
- Created 4 years ago
- Reactions:20
- Comments:16
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I set up a custom resolver and just published it to npm:
jest-ts-webcompat-resolver
I got things working with
.js
extensions in typescript imports with the following jest.config.js: