Skip to content

Commit 109f6aa

Browse files
authored
feat: allow project path option
1 parent 4ce870a commit 109f6aa

File tree

7 files changed

+105
-10
lines changed

7 files changed

+105
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ The CLI accepts a list of arguments:
8787
| `-s, --strict [boolean]` | Run the check in strict mode. | false |
8888
| `-d, --debug [boolean]` | Show debug information. | false |
8989
| `-c, --cache [boolean]` | Save and reuse type check result from cache. | false |
90+
| `-p, --project [string]` | File path to tsconfig file, eg: --project "./app/tsconfig.app.json" | . |
9091
| `-i, --ignore-files [boolean]` | Ignore specified files, eg: --ignore-files "demo1/\*.ts" --ignore-files "demo2/foo.ts" | false |
9192
| `-u, --ignore-unread [boolean]` | Allow writes to variables with implicit any types | false |
9293

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"type-coverage": "node ./dist/bin/typescript-coverage-report",
2929
"docs": "yarn type-coverage --outputDir docs",
3030
"prepublish": "yarn lint && yarn build",
31+
"test": "jest",
3132
"release": "gren release"
3233
},
3334
"devDependencies": {

src/bin/typescript-coverage-report.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const {
3131
strict = false,
3232
debug = false,
3333
cache = false,
34+
project = ".",
3435
ignoreFiles = false,
3536
ignoreCatch = false,
3637
ignoreUnread = false
@@ -57,6 +58,11 @@ program
5758
"save and reuse type check result from cache.",
5859
cache
5960
)
61+
.option(
62+
"-p, --project [string]",
63+
'file path to tsconfig file, eg: --project "./app/tsconfig.app.json"',
64+
project
65+
)
6066
.option(
6167
"-i, --ignore-files [string[]]",
6268
'ignore specified files, eg: --ignore-files "demo1/*.ts" --ignore-files "demo2/foo.ts"',
@@ -78,6 +84,7 @@ const options = {
7884
/* camelCase keys matching "long" flags in options above */
7985
outputDir: program.outputDir,
8086
threshold: program.threshold,
87+
tsProjectFile: program.project,
8188
strict: program.strict,
8289
debug: program.debug,
8390
cache: program.cache,

src/lib/__tests__/__snapshots__/getCoverage.test.ts.snap

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,55 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`getCoverage function accepts a tsProjectFile option 1`] = `
4+
Object {
5+
"anys": Array [],
6+
"covered": 100,
7+
"fileCounts": Map {
8+
"index.html" => Object {
9+
"correctCount": 100,
10+
"totalCount": 100,
11+
},
12+
},
13+
"percentage": 100,
14+
"total": 100,
15+
"uncovered": 0,
16+
}
17+
`;
18+
19+
exports[`getCoverage function accepts a tsProjectFile option 2`] = `
20+
Array [
21+
Array [
22+
"./app/tsconfig.json",
23+
Object {
24+
"debug": undefined,
25+
"enableCache": undefined,
26+
"fileCounts": true,
27+
"ignoreCatch": undefined,
28+
"ignoreFiles": undefined,
29+
"ignoreUnreadAnys": undefined,
30+
"strict": undefined,
31+
},
32+
],
33+
]
34+
`;
35+
36+
exports[`getCoverage function defaults to root project when tsProjectFile is not passed 1`] = `
37+
Array [
38+
Array [
39+
".",
40+
Object {
41+
"debug": undefined,
42+
"enableCache": undefined,
43+
"fileCounts": true,
44+
"ignoreCatch": undefined,
45+
"ignoreFiles": undefined,
46+
"ignoreUnreadAnys": undefined,
47+
"strict": undefined,
48+
},
49+
],
50+
]
51+
`;
52+
353
exports[`getCoverage function returns calculated data from type-coverage-core 1`] = `
454
Object {
555
"anys": Array [],
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
1+
import typeCoverageCore from "type-coverage-core";
12
import getCoverage from "../getCoverage";
23

34
describe("getCoverage function", () => {
5+
afterEach(() => {
6+
jest.clearAllMocks();
7+
});
8+
49
it("returns calculated data from type-coverage-core", async (done) => {
510
const data = await getCoverage();
611

712
expect(data).toMatchSnapshot();
813

914
done();
1015
});
16+
17+
it("accepts a tsProjectFile option", async (done) => {
18+
typeCoverageCore.lint = jest.fn().mockImplementation(typeCoverageCore.lint);
19+
20+
const data = await getCoverage({
21+
tsProjectFile: "./app/tsconfig.json"
22+
});
23+
24+
expect(data).toMatchSnapshot();
25+
// @ts-expect-error
26+
expect(typeCoverageCore.lint.mock.calls).toMatchSnapshot();
27+
done();
28+
});
29+
30+
it("defaults to root project when tsProjectFile is not passed", async (done) => {
31+
typeCoverageCore.lint = jest.fn().mockImplementation(typeCoverageCore.lint);
32+
33+
await getCoverage();
34+
35+
// @ts-expect-error
36+
expect(typeCoverageCore.lint.mock.calls).toMatchSnapshot();
37+
done();
38+
});
1139
});

src/lib/getCoverage.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ export type CoverageData = {
2020
};
2121

2222
export type Options = Partial<
23-
Pick<LintOptions, "strict" | "debug" | "ignoreFiles" | "ignoreCatch"> & {
23+
Pick<
24+
LintOptions,
25+
"strict" | "debug" | "ignoreFiles" | "ignoreCatch" | "tsProjectFile"
26+
> & {
2427
cache: LintOptions["enableCache"];
2528
ignoreUnread: LintOptions["ignoreUnreadAnys"];
2629
}
2730
>;
2831

2932
const getCoverage = async (options?: Options): Promise<CoverageData> => {
3033
const {
34+
tsProjectFile = ".",
3135
strict,
3236
debug,
3337
ignoreFiles,
@@ -36,15 +40,18 @@ const getCoverage = async (options?: Options): Promise<CoverageData> => {
3640
ignoreUnread: ignoreUnreadAnys
3741
} = options || {};
3842

39-
const { anys, fileCounts, totalCount, correctCount } = await lint(".", {
40-
strict,
41-
debug,
42-
ignoreFiles,
43-
ignoreCatch,
44-
enableCache,
45-
ignoreUnreadAnys,
46-
fileCounts: true
47-
});
43+
const { anys, fileCounts, totalCount, correctCount } = await lint(
44+
tsProjectFile,
45+
{
46+
strict,
47+
debug,
48+
ignoreFiles,
49+
ignoreCatch,
50+
enableCache,
51+
ignoreUnreadAnys,
52+
fileCounts: true
53+
}
54+
);
4855
const percentage = totalCount === 0 ? 100 : (correctCount * 100) / totalCount;
4956

5057
return {

src/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default async function generateCoverageReport(
2222
const dirPath = path.join(process.cwd(), options.outputDir);
2323

2424
const data = await getCoverage({
25+
tsProjectFile: options.tsProjectFile,
2526
strict: options.strict,
2627
debug: options.debug,
2728
ignoreFiles: options.ignoreFiles,

0 commit comments

Comments
 (0)