Skip to content

Commit

Permalink
Merge pull request #342 from kulshekhar/fix-238
Browse files Browse the repository at this point in the history
Find tsconfig.json by traversing up
  • Loading branch information
kulshekhar authored Oct 9, 2017
2 parents 63f85f3 + f98651c commit d33d311
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-jest",
"version": "21.1.0",
"version": "21.1.1",
"main": "index.js",
"types": "./dist/index.d.ts",
"description": "A preprocessor with sourcemap support to help use Typescript with Jest",
Expand Down
44 changes: 42 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,49 @@ function readCompilerOptions(configPath: string) {
return parsedConfig.options;
}

function getStartDir(): string {
// This is needed because of the way our tests are structured.
// If this is being executed as a library (under node_modules)
// we want to start with the project directory that's three
// levels above.
// If t his is being executed from the test suite, we want to start
// in the directory of the test

const grandparent = path.resolve(__dirname, '..', '..');
if (grandparent.endsWith('/node_modules')) {
return path.resolve(grandparent, '..');
}
return '.';
}

function getPathToClosestTSConfig(
startDir?: string,
previousDir?: string,
): string {
// Starting with the startDir directory and moving on to the
// parent directory recursively (going no further than the root directory)
// find and return the path to the first encountered tsconfig.json file

if (!startDir) {
return getPathToClosestTSConfig(getStartDir());
}

const tsConfigPath = path.join(startDir, 'tsconfig.json');

const startDirPath = path.resolve(startDir);
const previousDirPath = path.resolve(previousDir || '/');

if (startDirPath === previousDirPath || fs.existsSync(tsConfigPath)) {
return tsConfigPath;
}

return getPathToClosestTSConfig(path.join(startDir, '..'), startDir);
}

export function getTSConfigOptionFromConfig(globals: any) {
const defaultTSConfigFile = getPathToClosestTSConfig();
if (!globals) {
return 'tsconfig.json';
return defaultTSConfigFile;
}

const tsJestConfig = getTSJestConfig(globals);
Expand All @@ -129,7 +169,7 @@ More information at https://github.com/kulshekhar/ts-jest#tsconfig`);
return tsJestConfig.tsConfigFile;
}

return 'tsconfig.json';
return defaultTSConfigFile;
}

export function mockGlobalTSConfigSchema(globals: any) {
Expand Down

0 comments on commit d33d311

Please sign in to comment.