Skip to content

Commit e5ad58e

Browse files
committed
Fix problem with tsconfig from node_modules
* Use Jest rootDir in parseJsonConfigFileContent as basePath * Add tests
1 parent a650260 commit e5ad58e

6 files changed

Lines changed: 90 additions & 1 deletion

File tree

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function readCompilerOptions(configPath: string, rootDir: string) {
3737
}
3838

3939
// Second step: Parse the config, resolving all potential references.
40-
const basePath = path.dirname(configPath); // equal to "getDirectoryPath" from ts, at least in our case.
40+
const basePath = path.resolve(rootDir);
4141
const parsedConfig = tsc.parseJsonConfigFileContent(
4242
loaded.config,
4343
tsc.sys,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as fs from 'fs-extra';
2+
import * as path from 'path';
3+
import runJest from '../__helpers__/runJest';
4+
5+
describe('Use config from node_modules', () => {
6+
it('Should run all tests', () => {
7+
const testDir = path.resolve(__dirname, '../use-config-from-node-modules');
8+
const configPath = path.resolve(testDir, 'tsconfig.json');
9+
const targetDir = path.resolve(testDir, 'node_modules', 'common-tsconfig');
10+
const targetConfigPath = path.resolve(targetDir, 'tsconfig.json');
11+
fs.ensureDirSync(targetDir);
12+
fs.copyFileSync(configPath, targetConfigPath);
13+
14+
const result = runJest('../use-config-from-node-modules', ['--no-cache']);
15+
const stderr = result.stderr;
16+
17+
expect(result.status).toBe(1);
18+
expect(stderr).toContain('1 failed, 1 total');
19+
expect(stderr).toContain('Hello Class');
20+
expect(stderr).toContain('should throw an error on line 18');
21+
});
22+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
interface FooInterface {
2+
foo: string;
3+
bar: number; //This interface should be stripped and the line numbers should still fit.
4+
}
5+
6+
export class Hello {
7+
constructor() {
8+
const greeting = `
9+
this
10+
is
11+
a
12+
multiline
13+
greeting
14+
`;
15+
16+
this.unexcuted(() => {});
17+
18+
throw new Error('Hello error!');
19+
}
20+
21+
public unexcuted(action: () => void = () => {}): void {
22+
if (action) {
23+
action();
24+
} else {
25+
console.log('unexcuted');
26+
}
27+
}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
declare var jest, describe, it, expect;
2+
3+
import { Hello } from '../Hello';
4+
5+
describe('Hello Class', () => {
6+
it('should throw an error on line 18', () => {
7+
const hello = new Hello();
8+
});
9+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"jest": {
3+
"globals": {
4+
"ts-jest": {
5+
"tsConfigFile": "./node_modules/common-tsconfig/tsconfig.json"
6+
}
7+
},
8+
"transform": {
9+
"^.+\\.tsx?$": "ts-jest"
10+
},
11+
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
12+
"moduleFileExtensions": [
13+
"ts",
14+
"tsx",
15+
"js",
16+
"jsx",
17+
"json"
18+
]
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES5",
4+
"module": "commonjs",
5+
"moduleResolution": "node",
6+
"noEmitOnError": false,
7+
"jsx": "react",
8+
"allowJs": true
9+
}
10+
}

0 commit comments

Comments
 (0)