Skip to content

Commit f5100dd

Browse files
tom-shermanazz
authored andcommitted
feat: add ability to define tsconfig.json path (#19)
1 parent bc566e0 commit f5100dd

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
npm install --save-dev jest-runner-tsc
1515
```
1616

17-
## configure
17+
## usage
1818

1919
Jest configuration:
2020

@@ -29,6 +29,30 @@ module.exports = {
2929
};
3030
```
3131

32+
## options
33+
34+
This project uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig), so you can provide config via:
35+
36+
- a `jest-runner-tsc` property in your package.json
37+
- a `jest-runner-tsc.config.js` JS file
38+
- a `.jest-runner-tscrc` JSON file
39+
40+
### Example in package.json
41+
42+
```json
43+
{
44+
"jest-runner-tsc": {
45+
"tsconfigPath": "./tsconfig.types.json"
46+
}
47+
}
48+
```
49+
50+
### `tsconfigPath`
51+
52+
Default: `./tsconfig.json`
53+
54+
A relative path to your `tsconfig.json` file.
55+
3256
## run
3357

3458
```

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@
3434
},
3535
"dependencies": {
3636
"@babel/code-frame": "^7.0.0",
37+
"cosmiconfig": "^5.2.1",
3738
"create-jest-runner": "~0.4.1"
3839
},
3940
"repository": {
4041
"type": "git",
4142
"url": "https://github.com/azz/jest-runner-tsc.git"
4243
},
43-
"files": ["/dist"]
44+
"files": [
45+
"/dist"
46+
]
4447
}

src/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
import { createJestRunner } from 'create-jest-runner';
2+
import cosmiconfig from 'cosmiconfig';
23

3-
module.exports = createJestRunner(require.resolve('./runTsc'));
4+
const explorer = cosmiconfig('jest-runner-tsc');
5+
6+
const getExtraOptions = () => {
7+
const searchedFor = explorer.searchSync();
8+
if (!searchedFor || typeof searchedFor.config === 'undefined') {
9+
return {};
10+
}
11+
12+
return searchedFor.config;
13+
};
14+
15+
module.exports = createJestRunner(require.resolve('./runTsc'), {
16+
getExtraOptions,
17+
});

src/runTsc.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,20 @@ const appendCodeFrame = ({ filePath, errorMessage, location }) => {
1414
})}`;
1515
};
1616

17-
const runTsc = ({ testPath, config: jestConfig }) => {
17+
const runTsc = ({ testPath, config: jestConfig, extraOptions }) => {
1818
const start = Date.now();
19-
const configPath = path.resolve(jestConfig.rootDir, 'tsconfig.json');
19+
20+
const configPath =
21+
typeof extraOptions.tsconfigPath === 'string'
22+
? path.resolve(extraOptions.tsconfigPath)
23+
: path.resolve(jestConfig.rootDir, 'tsconfig.json');
24+
25+
if (!fs.existsSync(configPath)) {
26+
throw new Error(
27+
'Cannot find tsconfig file. Either create one in the root of your project or define a custom path via the `tsconfigPath` option.'
28+
);
29+
}
30+
2031
const configContents = fs.readFileSync(configPath).toString();
2132
const { config, error } = ts.parseConfigFileTextToJson(
2233
configPath,

yarn.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,16 @@ cosmiconfig@^5.0.0:
14121412
js-yaml "^3.9.0"
14131413
parse-json "^4.0.0"
14141414

1415+
cosmiconfig@^5.2.1:
1416+
version "5.2.1"
1417+
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-5.2.1.tgz#040f726809c591e77a17c0a3626ca45b4f168b1a"
1418+
integrity sha512-H65gsXo1SKjf8zmrJ67eJk8aIRKV5ff2D4uKZIBZShbhGSpEmsQOPW/SKMKYhSTrqR7ufy6RP69rPogdaPh/kA==
1419+
dependencies:
1420+
import-fresh "^2.0.0"
1421+
is-directory "^0.3.1"
1422+
js-yaml "^3.13.1"
1423+
parse-json "^4.0.0"
1424+
14151425
create-jest-runner@^0.4.1, create-jest-runner@~0.4.1:
14161426
version "0.4.1"
14171427
resolved "https://registry.yarnpkg.com/create-jest-runner/-/create-jest-runner-0.4.1.tgz#eeda15bc0f9dc622ef2de5a7fd9a0f1a2c2eb62d"
@@ -3289,6 +3299,14 @@ js-yaml@^3.12.0, js-yaml@^3.9.0:
32893299
argparse "^1.0.7"
32903300
esprima "^4.0.0"
32913301

3302+
js-yaml@^3.13.1:
3303+
version "3.13.1"
3304+
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.13.1.tgz#aff151b30bfdfa8e49e05da22e7415e9dfa37847"
3305+
integrity sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==
3306+
dependencies:
3307+
argparse "^1.0.7"
3308+
esprima "^4.0.0"
3309+
32923310
jsbn@~0.1.0:
32933311
version "0.1.1"
32943312
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513"

0 commit comments

Comments
 (0)