Skip to content

Instantly share code, notes, and snippets.

@drwpow
Last active June 21, 2019 17:17
Show Gist options
  • Save drwpow/e7a1fc373b4bc41ec3a461201d2e6e58 to your computer and use it in GitHub Desktop.
Save drwpow/e7a1fc373b4bc41ec3a461201d2e6e58 to your computer and use it in GitHub Desktop.
Jest + TypeScript
module.exports = {
moduleNameMapper: {
'^components(.*)$': '<rootDir>/src/components/$1',
'^data(.*)$': '<rootDir>/src/data/$1',
'^hooks(.*)$': '<rootDir>/src/hooks/$1',
'^lib(.*)$': '<rootDir>/src/lib/$1',
'^pages(.*)$': '<rootDir>/src/pages/$1',
'^types(.*)$': '<rootDir>/src/types/$1',
'^utils(.*)$': '<rootDir>/src/utils/$1',
},
preset: 'ts-jest',
setupFilesAfterEnv: ['jest-dom/extend-expect', '@testing-library/react/cleanup-after-each'],
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
};
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"allowUnreachableCode": false,
"baseUrl": "src",
"declaration": false,
"esModuleInterop": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"jsx": "react",
"lib": ["dom", "es2017"],
"module": "esnext",
"moduleResolution": "node",
"noImplicitAny": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "dist",
"strict": true,
"strictNullChecks": true,
"target": "esnext",
"types": ["node", "jest", "react"],
"paths": {
"*": ["node_modules/*", "*"]
}
},
"include": ["src"],
"exclude": ["node_modules"]
}
@cconstable
Copy link

cconstable commented Jun 21, 2019

🤔

const { pathsToModuleNameMapper } = require('ts-jest/utils');
const { compilerOptions } = require('./tsconfig');

module.exports = {
  roots: ['<rootDir>/lib/', '<rootDir>/test/'],
  transform: {
    '^.+\\.tsx?$': 'ts-jest'
  },
  testRegex: '(/__tests__/.*|\\.(test|spec))\\.[tj]sx?$',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  modulePaths: [
    "<rootDir>/node_modules",
    "<rootDir>/lib/",
  ],
  preset: 'ts-jest',
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths)
}

@drwpow
Copy link
Author

drwpow commented Jun 21, 2019

You shouldn’t need transform, testRegex, or moduleFileExtensions with the ts-jest setup. At least not with Jest 24 (it now supports TS 🎉).

What does console.log(pathsToModuleNameMapper(compilerOptions.paths)) return? Something similar to the above?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment