Skip to content

Commit 6d6ae95

Browse files
committed
This PR adds a --pass-with-no-tests option.
This allows for parity with vitest's [`--passWithNoTests`](https://vitest.dev/guide/cli.html#passwithnotests) and jest's [`--passWithNoTests`](https://jestjs.io/docs/cli#--passwithnotests) options.
1 parent f589fb3 commit 6d6ae95

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

source/cli.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ const cli = meow(`
1111
The given directory must contain a package.json and a typings file.
1212
1313
Info
14-
--help Display help text
15-
--version Display version info
14+
--help Display help text
15+
--version Display version info
1616
1717
Options
18-
--typings -t Type definition file to test [Default: "types" property in package.json]
19-
--files -f Glob of files to test [Default: '/path/test-d/**/*.test-d.ts' or '.tsx']
20-
--show-diff Show type error diffs [Default: don't show]
18+
--typings -t Type definition file to test [Default: "types" property in package.json]
19+
--files -f Glob of files to test [Default: '/path/test-d/**/*.test-d.ts' or '.tsx']
20+
--show-diff Show type error diffs [Default: don't show]
21+
--pass-with-no-tests Pass when no tests are found [Default: false]
2122
2223
Examples
2324
$ tsd /path/to/project
@@ -42,6 +43,10 @@ const cli = meow(`
4243
showDiff: {
4344
type: 'boolean',
4445
},
46+
passWithNoTests: {
47+
default: false,
48+
type: 'boolean',
49+
},
4550
},
4651
});
4752

@@ -64,9 +69,9 @@ const exit = (message: string, {isError = true}: {isError?: boolean} = {}) => {
6469
(async () => {
6570
try {
6671
const cwd = cli.input.length > 0 ? cli.input[0] : process.cwd();
67-
const {typings: typingsFile, files: testFiles, showDiff} = cli.flags;
72+
const {typings: typingsFile, files: testFiles, showDiff, passWithNoTests} = cli.flags;
6873

69-
const diagnostics = await tsd({cwd, typingsFile, testFiles});
74+
const diagnostics = await tsd({cwd, typingsFile, testFiles, passWithNoTests});
7075

7176
if (diagnostics.length > 0) {
7277
const hasErrors = diagnostics.some(diagnostic => diagnostic.severity === 'error');

source/lib/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface Options {
1111
cwd: string;
1212
typingsFile?: string;
1313
testFiles?: readonly string[];
14+
passWithNoTests?: boolean;
1415
}
1516

1617
const findTypingsFile = async (pkg: PackageJsonWithTsdConfig, options: Options): Promise<string> => {
@@ -39,9 +40,13 @@ const normalizeTypingsFilePath = (typingsFilePath: string, options: Options) =>
3940
return typingsFilePath;
4041
};
4142

42-
const findCustomTestFiles = async (testFilesPattern: readonly string[], cwd: string) => {
43+
const findCustomTestFiles = async (testFilesPattern: readonly string[], cwd: string, passWithNoTests: boolean) => {
4344
const testFiles = await globby(testFilesPattern, {cwd});
4445

46+
if (testFiles.length === 0 && passWithNoTests) {
47+
return [];
48+
}
49+
4550
if (testFiles.length === 0) {
4651
throw new TsdError('Could not find any test files with the given pattern(s). Create one and try again.');
4752
}
@@ -50,8 +55,10 @@ const findCustomTestFiles = async (testFilesPattern: readonly string[], cwd: str
5055
};
5156

5257
const findTestFiles = async (typingsFilePath: string, options: Options & {config: Config}) => {
58+
const passWithNoTests = options.passWithNoTests === true;
59+
5360
if (options.testFiles?.length) {
54-
return findCustomTestFiles(options.testFiles, options.cwd);
61+
return findCustomTestFiles(options.testFiles, options.cwd, passWithNoTests);
5562
}
5663

5764
// Return only the filename if the `typingsFile` option is used.
@@ -64,6 +71,10 @@ const findTestFiles = async (typingsFilePath: string, options: Options & {config
6471
let testFiles = await globby([testFile, tsxTestFile], {cwd: options.cwd});
6572
const testDirExists = await pathExists(path.join(options.cwd, testDir));
6673

74+
if (testFiles.length === 0 && passWithNoTests) {
75+
return [];
76+
}
77+
6778
if (testFiles.length === 0 && !testDirExists) {
6879
throw new TsdError(`The test file \`${testFile}\` or \`${tsxTestFile}\` does not exist in \`${options.cwd}\`. Create one and try again.`);
6980
}

source/test/cli.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,32 @@ test('cli typings and files flags', async t => {
123123
]);
124124
});
125125

126+
test('cli passWithNoTests flag', async t => {
127+
const runTest = async (arg: '--passWithNoTests') => {
128+
const {exitCode} = await execa('../../../cli.js', [arg], {
129+
cwd: path.join(__dirname, 'fixtures/no-test')
130+
});
131+
132+
t.is(exitCode, 0);
133+
};
134+
135+
await runTest('--passWithNoTests');
136+
});
137+
138+
test('cli passWithNoTests flag and files', async t => {
139+
const runTest = async (arg: '--pass-with-no-tests') => {
140+
const testFile = path.join(__dirname, 'fixtures/does-not-exist.test-d.ts');
141+
142+
const {exitCode} = await execa('../../../cli.js', [arg, `--files ${testFile}`], {
143+
cwd: path.join(__dirname, 'fixtures/no-test')
144+
});
145+
146+
t.is(exitCode, 0);
147+
};
148+
149+
await runTest('--pass-with-no-tests');
150+
});
151+
126152
test('tsd logs stacktrace on failure', async t => {
127153
const {exitCode, stderr} = await t.throwsAsync<ExecaError>(execa('../../../cli.js', {
128154
cwd: path.join(__dirname, 'fixtures/empty-package-json')

0 commit comments

Comments
 (0)