Skip to content

Commit

Permalink
feat(jest): dynamic pass paths from tsconfig for aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Боровский Дмитрий authored and Боровский Дмитрий committed Jan 13, 2020
1 parent 8dab139 commit cfaed8f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 3 additions & 0 deletions __tests__/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"paths": {
"Src/*": ["src/*"],
},
"lib": [
"dom",
"es2015",
Expand Down
12 changes: 10 additions & 2 deletions configs/jest/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const configs = require('../app-configs');
const fs = require('fs');
const { pathsToModuleNameMapper } = require('ts-jest/utils');
const { parseConfigFileTextToJson } = require('typescript');
const merge = require('lodash.merge');
const configs = require('../app-configs');
const tsConfigPath = configs.tsconfig;
const tsConfig = fs.readFileSync(configs.tsconfig, 'utf8');
const parseTsConfig = parseConfigFileTextToJson(tsConfigPath, tsConfig);
const parseTsConfigPaths = parseTsConfig.config.compilerOptions.paths || {};

const defaultJestConfig = {
testRegex: 'src/.*(test|spec|/__test__/|/__tests__/).*\\.(jsx?|tsx?)$',
Expand All @@ -16,7 +23,8 @@ const defaultJestConfig = {
},
moduleNameMapper: {
// replace all css files with simple empty exports
'\\.css$': require.resolve('./css-mock')
'\\.css$': require.resolve('./css-mock'),
...pathsToModuleNameMapper(parseTsConfigPaths, { prefix: '<rootDir>/' })
},
setupFiles: [
require.resolve('./setup')
Expand Down

0 comments on commit cfaed8f

Please sign in to comment.