# Typescript ESM icin Jest calistirmak

> Bunu kurmak ve dokumante etmek 2:30 saatimi aldı, gerçekten inanılmaz sıkıcı 😅

> 💡 Sifirdan npm projesi olusturacaksaniz [npm-package-template](https://github.com/yemreak/npm-package-template) template’ime bir bakin

## `package.json` yapilandirmasi

* `jest` yapilandirma bilgilerini `package.json` icine ekleyin
* `jest.config.js` kullanmaniza gerek yoktur
* `moduleNameMapper` ayari ile `ESM`den gelen zorunlu `import.js` kuralini duzenliyoruz
* `^.+\\.ts$` ile de `esm` kullanimini typecript kodlari icin aktif ediyoruz

```javascript
// {...
"jest": {
	"preset": "ts-jest",
	"testEnvironment": "node",
	"silent": true,
	"extensionsToTreatAsEsm": [
		".ts"
	],
	"moduleNameMapper": {
		"^(\\.{1,2}/.*)\\.js$": "$1"
	},
	"transform": {
		"^.+\\.ts$": [
			"ts-jest",
			{
				"useESM": true
			}
		]
	}
},
// ...}
```

## VSCode icin `launch.json` yapilandirmasi

```javascript
{
	"name": "Jest",
	"type": "node",
	"request": "launch",
	"runtimeArgs": [
		"--experimental-vm-modules", // <<< onemli
		"${workspaceRoot}/node_modules/.bin/jest",
		"${input:exchange}",
		"-t",
		"${input:testName}",
		"--runInBand"
	],
	"cwd": "${workspaceRoot}",
	"stopOnEntry": false,
	"console": "integratedTerminal",
	"internalConsoleOptions": "neverOpen",
	"sourceMaps": true,
	"windows": {
		"program": "${workspaceFolder}/node_modules/jest/bin/jest"
	},
	"skipFiles": ["<node_internals>/**", "**/node_modules/**"]
},
```

## Hata Notlari

### SyntaxError: Cannot use import statement outside a module

* Jest kullanimi icin `node` ile `experimental-vm-modules` argumanini kullanmaliyiz aksi halde hatasi aliriz

## Eklenti Tavsiyesi

{% embed url="<https://marketplace.visualstudio.com/items?itemName=vespa-dev-works.jestRunIt>" %}

## References

{% embed url="<https://stackoverflow.com/a/69117363/9770490>" %}

{% embed url="<https://jestjs.io/docs/ecmascript-modules>" %}

{% embed url="<https://gist.github.com/danpetitt/37f5c966886f54e457ece4b08d66e404?permalink_comment_id=4710758#gistcomment-4710758>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.yemreak.com/arsiv/programming/typescript-esm-icin-jest-calistirmak.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
