Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: TS-Jest or Jest doesnt resolve typescript imports with .js endings from TypeScript files #4162

Closed
Creative-Difficulty opened this issue Jun 12, 2023 · 5 comments

Comments

@Creative-Difficulty
Copy link

Creative-Difficulty commented Jun 12, 2023

Version

29.1.0

Steps to reproduce

Instructions below

Expected behavior

When i write this (pseudocode):

@filename tests/FooBarTest.test.ts
import { foo } from "../src/foo"

...
foo(dosomthingwithit)
...

@filename src/foo
import { bar } from "./foo.js"

useBar(bar)

export function foo(arg1: string) {
    ...
}

@filename src/bar

export const bar = "anyStringRightHere"

I would expect ts-jest/jest to correctly resolve the import from "./foo.js".

Actual behavior

Instead it throws this:

 FAIL  tests/checkEnvWithValidPort.test.ts
  ● Test suite failed to run

    Cannot find module './logger.js' from 'src/lib/checkEnv.ts'

    Require stack:
      src/lib/checkEnv.ts
      tests/checkEnvWithValidPort.test.ts

      2 | import * as fs from "fs/promises"
      3 | import * as dotenv from "dotenv";
    > 4 | import { logger } from "./logger.js";
        | ^
      5 |
      6 | export async function checkEnv(envPath: string): Promise<{ Port: number, JWTSecret: string }> {
      7 |     try {

      at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:427:11)
      at Object.<anonymous> (src/lib/checkEnv.ts:4:1)
      at Object.<anonymous> (tests/checkEnvWithValidPort.test.ts:2:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.309 s
Ran all test suites.

Because it doesnt recognise the import.

Debug log

doesnt work

Additional context

This seems to be related to #4013 and #4127
Also VSCode HATES absolute paths in TypeScript so this doesnt work.
Thanks for any Help!

Environment

System:
    OS: Windows 10 10.0.19045
    CPU: (12) x64 AMD Ryzen 5 3600 6-Core Processor
  Binaries:
    Node: 18.5.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.12.1 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    jest: ^29.5.0 => 29.5.0
@Creative-Difficulty
Copy link
Author

@kulshekhar This is VERY urgent and mission critical, as it makes this package unuseable. PLS FIX

@Creative-Difficulty
Copy link
Author

UPDATE: Fixed by using

"allowImportingTsExtensions": true,
"noEmit": true

in the tsconfig.json.
Im leaving this open, because in a larger codebease it is very tedious to change all the imports from import { bar } from "./foo.js" to import { bar } from "./foo.ts"

@mrazauskas
Copy link
Contributor

Hm.. Might be I miss something, is this really related with #4127?

Did you try the ts-jest-resolver to deal with .js imports?

@Creative-Difficulty
Copy link
Author

trying it rn!

@Creative-Difficulty
Copy link
Author

Thanks @mrazauskas it works now! Maybe we could put a notice on the README or somewhere else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants