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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ".js" endings in import statements #2010

Closed
dospunk opened this issue Oct 6, 2020 · 2 comments
Closed

Support ".js" endings in import statements #2010

dospunk opened this issue Oct 6, 2020 · 2 comments
Labels
Not An Issue Not ts-jest issue

Comments

@dospunk
Copy link

dospunk commented Oct 6, 2020

馃悰 Bug Report

When writing import statements, TypeScript allows adding a ".js" ending to a module's name, even when that module is actually a ".ts" file. This is necessary to write modules TypeScript that target the browser, since the browser does not follow Node's module resolution system.

import {thing, otherThing} from "./module.js";
// will not raise an error even though the module we are actually referencing is "./module.ts"

It seems that ts-jest, however, does not allow this. When I try to test a file that includes this style of import, I get an error saying that the module could not be found. If I remove the ".js" ending from the import it runs fine in ts-jest, but then of course it will not work in the browser.

To Reproduce

import a module (ModuleB) into another module (ModuleA) using the style described above, then try to test ModuleA using ts-jest

Expected behavior

I expect the import to behave as it would when running the TypeScript compiler

Link to repo (highly encouraged)

https://github.com/dospunk/ts-jest-bug-repo

Debug log:

# content of ts-jest.log :
{"context":{"allowJs":false,"logLevel":20,"namespace":"jest-preset","package":"ts-jest","version":"26.4.1"},"message":"creating jest presets not handling JavaScript files","sequence":1,"time":"2020-10-06T19:04:44.936Z"}
{"context":{"allowJs":false,"logLevel":20,"namespace":"jest-preset","package":"ts-jest","version":"26.4.1"},"message":"creating jest presets not handling JavaScript files","sequence":2,"time":"2020-10-06T19:04:44.955Z"}
{"context":{"logLevel":20,"namespace":"Importer","package":"ts-jest","version":"26.4.1"},"message":"creating Importer singleton","sequence":3,"time":"2020-10-06T19:04:46.895Z"}
{"context":{"allowJs":false,"logLevel":20,"namespace":"jest-preset","package":"ts-jest","version":"26.4.1"},"message":"creating jest presets not handling JavaScript files","sequence":4,"time":"2020-10-06T19:04:46.901Z"}
{"context":{"actualVersion":"26.5.2","expectedVersion":">=26 <27","logLevel":20,"namespace":"versions","package":"ts-jest","version":"26.4.1"},"message":"checking version of jest: OK","sequence":5,"time":"2020-10-06T19:04:46.907Z"}
{"context":{"baseOptions":{},"logLevel":20,"namespace":"jest-transformer","package":"ts-jest","transformerId":1,"version":"26.4.1"},"message":"created new transformer","sequence":6,"time":"2020-10-06T19:04:46.908Z"}
{"context":{"fileName":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo\\tests\\ModuleB.test.ts","logLevel":20,"namespace":"jest-transformer","package":"ts-jest","transformOptions":{"config":{"automock":false,"cache":true,"cacheDirectory":"C:\\Users\\lostb\\AppData\\Local\\Temp\\jest","clearMocks":false,"coveragePathIgnorePatterns":["\\\\node_modules\\\\"],"cwd":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{"computeSha1":false,"throwOnModuleCollision":false},"injectGlobals":true,"moduleDirectories":["node_modules"],"moduleFileExtensions":["js","json","jsx","ts","tsx","node"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"name":"42c1e9073f61283abd1c09ad2352d4f1","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo","roots":["C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo"],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo\\node_modules\\jest-environment-jsdom\\build\\index.js","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":["**/__tests__/**/*.[jt]s?(x)","**/?(*.)+(spec|test).[tj]s?(x)"],"testPathIgnorePatterns":["\\\\node_modules\\\\"],"testRegex":[],"testRunner":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo\\node_modules\\jest-jasmine2\\build\\index.js","testURL":"http://localhost","timers":"real","transform":[["^.+\\.tsx?$","C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo\\node_modules\\ts-jest\\dist\\index.js",{}]],"transformIgnorePatterns":["\\\\node_modules\\\\","\\.pnp\\.[^\\\\]+$"],"watchPathIgnorePatterns":[]},"instrument":false,"rootDir":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo","supportsDynamicImport":false,"supportsStaticESM":false},"transformerId":1,"version":"26.4.1"},"message":"computing cache key for C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo\\tests\\ModuleB.test.ts","sequence":7,"time":"2020-10-06T19:04:46.909Z"}
{"context":{"logLevel":30,"namespace":"jest-transformer","package":"ts-jest","transformerId":1,"version":"26.4.1"},"message":"no matching config-set found, creating a new one","sequence":8,"time":"2020-10-06T19:04:46.909Z"}
{"context":{"config":{"automock":false,"cache":true,"cacheDirectory":"C:\\Users\\lostb\\AppData\\Local\\Temp\\jest","clearMocks":false,"coveragePathIgnorePatterns":["\\\\node_modules\\\\"],"cwd":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{"computeSha1":false,"throwOnModuleCollision":false},"injectGlobals":true,"moduleDirectories":["node_modules"],"moduleFileExtensions":["js","json","jsx","ts","tsx","node"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"name":"42c1e9073f61283abd1c09ad2352d4f1","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo","roots":["C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo"],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo\\node_modules\\jest-environment-jsdom\\build\\index.js","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":["**/__tests__/**/*.[jt]s?(x)","**/?(*.)+(spec|test).[tj]s?(x)"],"testPathIgnorePatterns":["\\\\node_modules\\\\"],"testRegex":[],"testRunner":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo\\node_modules\\jest-jasmine2\\build\\index.js","testURL":"http://localhost","timers":"real","transform":[["^.+\\.tsx?$","C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo\\node_modules\\ts-jest\\dist\\index.js",{}]],"transformIgnorePatterns":["\\\\node_modules\\\\","\\.pnp\\.[^\\\\]+$"],"watchPathIgnorePatterns":[]},"logLevel":20,"namespace":"backports","package":"ts-jest","transformerId":1,"version":"26.4.1"},"message":"backporting config","sequence":9,"time":"2020-10-06T19:04:46.910Z"}
{"context":{"jestConfig":{"automock":false,"cache":true,"cacheDirectory":"C:\\Users\\lostb\\AppData\\Local\\Temp\\jest","clearMocks":false,"coveragePathIgnorePatterns":["\\\\node_modules\\\\"],"cwd":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{"ts-jest":{}},"haste":{"computeSha1":false,"throwOnModuleCollision":false},"injectGlobals":true,"moduleDirectories":["node_modules"],"moduleFileExtensions":["js","json","jsx","ts","tsx","node"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"name":"42c1e9073f61283abd1c09ad2352d4f1","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo","roots":["C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo"],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo\\node_modules\\jest-environment-jsdom\\build\\index.js","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":["**/__tests__/**/*.[jt]s?(x)","**/?(*.)+(spec|test).[tj]s?(x)"],"testPathIgnorePatterns":["\\\\node_modules\\\\"],"testRegex":[],"testRunner":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo\\node_modules\\jest-jasmine2\\build\\index.js","testURL":"http://localhost","timers":"real","transform":[["^.+\\.tsx?$","C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo\\node_modules\\ts-jest\\dist\\index.js",{}]],"transformIgnorePatterns":["\\\\node_modules\\\\","\\.pnp\\.[^\\\\]+$"],"watchPathIgnorePatterns":[]},"logLevel":20,"namespace":"config","package":"ts-jest","transformerId":1,"version":"26.4.1"},"message":"normalized jest config","sequence":10,"time":"2020-10-06T19:04:46.910Z"}
{"context":{"logLevel":20,"namespace":"config","package":"ts-jest","transformerId":1,"tsJestConfig":{"compiler":"typescript","diagnostics":{"ignoreCodes":[6059,18002,18003],"pretty":true,"throws":true},"isolatedModules":false,"packageJson":{"kind":"file"},"transformers":{},"tsConfig":{"kind":"file"}},"version":"26.4.1"},"message":"normalized ts-jest config","sequence":11,"time":"2020-10-06T19:04:46.911Z"}
{"context":{"logLevel":20,"namespace":"config","package":"ts-jest","transformerId":1,"version":"26.4.1"},"message":"babel is disabled","sequence":12,"time":"2020-10-06T19:04:46.911Z"}
{"context":{"logLevel":20,"namespace":"Importer","package":"ts-jest","requireResult":{"exists":true,"given":"typescript","path":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo\\node_modules\\typescript\\lib\\typescript.js"},"version":"26.4.1"},"message":"loaded module typescript","sequence":13,"time":"2020-10-06T19:04:46.912Z"}
{"context":{"logLevel":20,"namespace":"Importer","package":"ts-jest","version":"26.4.1"},"message":"patching typescript","sequence":14,"time":"2020-10-06T19:04:46.912Z"}
{"context":{"actualVersion":"4.0.3","expectedVersion":">=3.8 <5","logLevel":20,"namespace":"versions","package":"ts-jest","version":"26.4.1"},"message":"checking version of typescript: OK","sequence":15,"time":"2020-10-06T19:04:46.914Z"}
{"context":{"logLevel":20,"namespace":"config","package":"ts-jest","transformerId":1,"tsConfigFileName":"C:/Users/lostb/Desktop/ts-jest-bug-repo/tsconfig.json","version":"26.4.1"},"message":"readTsConfig(): reading C:/Users/lostb/Desktop/ts-jest-bug-repo/tsconfig.json","sequence":16,"time":"2020-10-06T19:04:46.914Z"}
{"context":{"logLevel":20,"namespace":"config","package":"ts-jest","transformerId":1,"tsconfig":{"compileOnSave":false,"configFileSpecs":{"excludeSpecs":["tests"],"includeSpecs":["**/*"],"validatedExcludeSpecs":["tests"],"validatedIncludeSpecs":["**/*"],"wildcardDirectories":{"c:/users/lostb/desktop/ts-jest-bug-repo":1}},"errors":[],"fileNames":["C:/Users/lostb/Desktop/ts-jest-bug-repo/src/ModuleA.ts","C:/Users/lostb/Desktop/ts-jest-bug-repo/src/ModuleB.ts"],"options":{"alwaysStrict":true,"configFilePath":"C:/Users/lostb/Desktop/ts-jest-bug-repo/tsconfig.json","declaration":false,"esModuleInterop":true,"forceConsistentCasingInFileNames":true,"inlineSourceMap":false,"inlineSources":true,"module":1,"noEmit":false,"outDir":"C:/Users/lostb/Desktop/ts-jest-bug-repo/js","removeComments":false,"rootDir":"C:/Users/lostb/Desktop/ts-jest-bug-repo/src","skipLibCheck":true,"sourceMap":true,"strict":true,"target":4},"raw":{"compileOnSave":false,"compilerOptions":{"alwaysStrict":true,"esModuleInterop":true,"forceConsistentCasingInFileNames":true,"module":"es6","outDir":"./js","rootDir":"./src","skipLibCheck":true,"strict":true,"target":"es2017"},"exclude":["tests"]},"typeAcquisition":{"enable":false,"exclude":[],"include":[]},"wildcardDirectories":{"c:/users/lostb/desktop/ts-jest-bug-repo":1}},"version":"26.4.1"},"message":"normalized typescript config","sequence":17,"time":"2020-10-06T19:04:46.929Z"}
{"context":{"fileName":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo\\src\\ModuleA.ts","logLevel":20,"namespace":"jest-transformer","package":"ts-jest","transformOptions":{"config":{"automock":false,"cache":true,"cacheDirectory":"C:\\Users\\lostb\\AppData\\Local\\Temp\\jest","clearMocks":false,"coveragePathIgnorePatterns":["\\\\node_modules\\\\"],"cwd":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{"computeSha1":false,"throwOnModuleCollision":false},"injectGlobals":true,"moduleDirectories":["node_modules"],"moduleFileExtensions":["js","json","jsx","ts","tsx","node"],"moduleNameMapper":[],"modulePathIgnorePatterns":[],"name":"42c1e9073f61283abd1c09ad2352d4f1","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo","roots":["C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo"],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo\\node_modules\\jest-environment-jsdom\\build\\index.js","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":["**/__tests__/**/*.[jt]s?(x)","**/?(*.)+(spec|test).[tj]s?(x)"],"testPathIgnorePatterns":["\\\\node_modules\\\\"],"testRegex":[],"testRunner":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo\\node_modules\\jest-jasmine2\\build\\index.js","testURL":"http://localhost","timers":"real","transform":[["^.+\\.tsx?$","C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo\\node_modules\\ts-jest\\dist\\index.js",{}]],"transformIgnorePatterns":["\\\\node_modules\\\\","\\.pnp\\.[^\\\\]+$"],"watchPathIgnorePatterns":[]},"instrument":false,"rootDir":"C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo","supportsDynamicImport":false,"supportsStaticESM":false},"transformerId":1,"version":"26.4.1"},"message":"computing cache key for C:\\Users\\lostb\\Desktop\\ts-jest-bug-repo\\src\\ModuleA.ts","sequence":18,"time":"2020-10-06T19:04:46.933Z"}

envinfo

System:
    OS: Windows 10

Npm packages:
    "@types/jest": "^26.0.14",
    "jest": "^26.5.2",
    "ts-jest": "^26.4.1",
    "typescript": "^4.0.3"
@dospunk dospunk added Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels Oct 6, 2020
@ahnpnl
Copy link
Collaborator

ahnpnl commented Oct 6, 2020

Hi, thank you for reporting the issue. This issue is similar to #1960

I鈥檝e checked your repo and I saw you use outDir to instruct typescript to look for compiled js files. This feature hasn鈥檛 been supported yet for ts-jest which is reported in #1960

@ahnpnl ahnpnl added 馃悰 Bug Confirmed Bug is confirmed and removed Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels Oct 6, 2020
@ahnpnl
Copy link
Collaborator

ahnpnl commented Oct 17, 2020

actually, module not found is caused by jest. Because in this case, the transpile output is var B = require("./ModuleB.js");. ts-jest doesn't do module resolution itself, that is jest does.

Your scenario is you are trying to reference a js file which doesn't exist next to the original ts file, so that's why jest can't find it.

The solution is either:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not An Issue Not ts-jest issue
Projects
None yet
Development

No branches or pull requests

2 participants