Import Absolute Paths in Typescript Jest Tests
In order to avoid this:
// project/__tests__/stuff/someDistantCousin.test.ts
import { thing } from '../../src/stuff/someDistantCousin'
import { wrapFunction } from '../testUtils/firebase'
And to write this instead:
import { thing } from 'src/stuff/someDistantCousin'
import { wrapFunction } from 'tests/testUtils/firebase'
There are 2 things to configure:
-
Jest Config (i.e.
jest.config.js
,package.json
, etc...) -
Typscript Config:
tsconfig.json
jest.config.js
module.exports = {
moduleNameMapper: {
'src/(.*)': '<rootDir>/src/$1',
'tests/(.*)': '<rootDir>/__tests__/$1',
},
}
tsconfig.json
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"src/*": ["src/*"],
"tests/*": ["__tests__/*"]
}
}
}
Tweet