> For the complete documentation index, see [llms.txt](https://docs.yemreak.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.yemreak.com/arsiv/programming/running-jest-tests-for-typescript-esm-modules.md).

# Running Jest tests for Typescript ESM modules

typescript, programming, jest, vs code, ESM

> It took me 2:30 hours to set this up and document it, it's incredibly tedious 😅

> 💡 If you're going to create a new npm project, take a look at my [npm-package-template](https://github.com/yemreak/npm-package-template)

## `package.json` configuration

* Add `jest` configuration information to `package.json`
* You don't need to use `jest.config.js`
* We fix the mandatory `import.js` rule that comes from `ESM` with the `moduleNameMapper` setting
* We activate `esm` usage for typescript codes with `^.+\\\\.ts$`

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

```

## `launch.json` configuration for VSCode

```javascript
{
	"name": "Jest",
	"type": "node",
	"request": "launch",
	"runtimeArgs": [
		"--experimental-vm-modules", // <<< important
		"${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/**"]
},

```

## Error Notes

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

* We should use `node` with `experimental-vm-modules` argument for Jest usage, otherwise we get an error

## Plugin Recommendation

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

## References

[Jest Typescript with ES Module in node\_modules error - Must use import to load ES Module:](https://stackoverflow.com/a/69117363/9770490)

[ECMAScript Modules · Jest](https://jestjs.io/docs/ecmascript-modules)

[Typescript, Jest and ECMAScript Modules](https://gist.github.com/danpetitt/37f5c966886f54e457ece4b08d66e404?permalink_comment_id=4710758#gistcomment-4710758)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.yemreak.com/arsiv/programming/running-jest-tests-for-typescript-esm-modules.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
