Unit testing with TypeScript and tsconfig paths #28027
Labels
stale?
Issue that may be closed soon due to the original author not responding any more.
type: documentation
An issue or pull request for improving or updating Gatsby's documentation
Summary
When using TypeScript it is often common to use tsconfig paths option, which maps a module name to its' concrete path.
For example, if I have
models
folder withindex.ts
file and the following tsconfig.json (baseUrl
is required when usingpaths
)Then importing from models becomes
The problem is a missing configuration in unit-testing#using-typescript which causes paths to not be resolved.
Motivation
Including this documentation will help keep consistent way to integrate paths with jest.
Steps to resolve this issue
There are various ways to support paths with jest, I find the most simple way with ts-jest.
After adding ts-jest with
npm i -D ts-jest
we need to add the following code injest.config.js
:Draft the doc
Using Typescript
...
Using tsconfig paths
If you are using tsconfig paths there is a single change to your config.
jest.config.js
to import and maptsconfig.json
pathsjest.config.js
moduleNameMapperOpen a pull request
The text was updated successfully, but these errors were encountered: