From 012248871d0731cf2777d8b108f8ae9888017896 Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Wed, 24 Jun 2020 19:28:51 +0530 Subject: [PATCH 01/23] Remove VSCode config folder and add source code generation in TS build --- .gitignore | 1 + tsconfig.json | 1 + 2 files changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 4dbf822c..d85e9aa4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules yarn.lock dist/ +.vscode/ diff --git a/tsconfig.json b/tsconfig.json index e7ad6173..50b8f1e0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,6 +18,7 @@ "noUnusedParameters": true, "noFallthroughCasesInSwitch": true, "noEmitOnError": true, + "sourceMap": true, "forceConsistentCasingInFileNames": true }, "exclude": [ From e706b632c313e22308d8652e2f11b51699df56d8 Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Wed, 24 Jun 2020 21:23:43 +0530 Subject: [PATCH 02/23] Allow passing of path to type definiton file manually --- source/lib/index.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/source/lib/index.ts b/source/lib/index.ts index 4e56dbb2..4ae82c2b 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -9,6 +9,7 @@ import {Context, Config} from './interfaces'; export interface Options { cwd: string; + typingsFile?: string; } const findTypingsFile = async (pkg: any, options: Options) => { @@ -28,6 +29,7 @@ const findTestFiles = async (typingsFile: string, options: Options & {config: Co const tsxTestFile = typingsFile.replace(/\.d\.ts$/, '.test-d.tsx'); const testDir = options.config.directory; + //@ts-ignore let testFiles = await globby([testFile, tsxTestFile], {cwd: options.cwd}); const testDirExists = await pathExists(path.join(options.cwd, testDir)); @@ -37,6 +39,7 @@ const findTestFiles = async (typingsFile: string, options: Options & {config: Co } if (testFiles.length === 0) { + //@ts-ignore testFiles = await globby([`${testDir}/**/*.ts`, `${testDir}/**/*.tsx`], {cwd: options.cwd}); } @@ -49,6 +52,7 @@ const findTestFiles = async (typingsFile: string, options: Options & {config: Co * @returns A promise which resolves the diagnostics of the type definition. */ export default async (options: Options = {cwd: process.cwd()}) => { + let typingsFile = options.typingsFile ? options.typingsFile : ''; const pkgResult = await readPkgUp({cwd: options.cwd}); if (!pkgResult) { @@ -60,7 +64,9 @@ export default async (options: Options = {cwd: process.cwd()}) => { const config = loadConfig(pkg as any, options.cwd); // Look for a typings file, otherwise use `index.d.ts` in the root directory. If the file is not found, throw an error. - const typingsFile = await findTypingsFile(pkg, options); + if (!typingsFile) { + typingsFile = await findTypingsFile(pkg, options); + } const testFiles = await findTestFiles(typingsFile, { ...options, @@ -68,11 +74,11 @@ export default async (options: Options = {cwd: process.cwd()}) => { }); const context: Context = { - cwd: options.cwd, pkg, - typingsFile, + config, testFiles, - config + typingsFile, + cwd: options.cwd }; return [ From 43ac09104efe9e19ae15026374060f4486fa0c05 Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Wed, 24 Jun 2020 21:31:24 +0530 Subject: [PATCH 03/23] Update README.md --- readme.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/readme.md b/readme.md index 9f13621a..3f18ceae 100644 --- a/readme.md +++ b/readme.md @@ -213,6 +213,12 @@ Default: `process.cwd()` Current working directory of the project to retrieve the diagnostics for. +##### typingsFile + +Type: `string`
+Default: `''` + +Path to the type definitions of the project. ## License From 1282ce633faf5c7f1f1ee44ffdbfea8ade87b59c Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Wed, 24 Jun 2020 21:46:07 +0530 Subject: [PATCH 04/23] Refactor code to check definition file existence if specified manually --- source/lib/index.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/source/lib/index.ts b/source/lib/index.ts index 4ae82c2b..953c580a 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -12,14 +12,19 @@ export interface Options { typingsFile?: string; } -const findTypingsFile = async (pkg: any, options: Options) => { - const typings = pkg.types || pkg.typings || 'index.d.ts'; - - const typingsExist = await pathExists(path.join(options.cwd, typings)); +const checkTypingsFile = async (typings: string, options: Options) => { + const typingsPath = path.join(options.cwd, typings); + const typingsExist = await pathExists(typingsPath); if (!typingsExist) { throw new Error(`The type definition \`${typings}\` does not exist. Create one and try again.`); } +}; + +const findTypingsFile = async (pkg: any, options: Options) => { + const typings = pkg.types || pkg.typings || 'index.d.ts'; + + await checkTypingsFile(typings, options); return typings; }; @@ -63,11 +68,14 @@ export default async (options: Options = {cwd: process.cwd()}) => { const config = loadConfig(pkg as any, options.cwd); - // Look for a typings file, otherwise use `index.d.ts` in the root directory. If the file is not found, throw an error. + // Look for a typings file if not explicitly specified, otherwise use `index.d.ts` in the root directory. + // If the file is not found, throw an error. if (!typingsFile) { typingsFile = await findTypingsFile(pkg, options); } + await checkTypingsFile(typingsFile, options); + const testFiles = await findTestFiles(typingsFile, { ...options, config From 81851622e503764a871181b770e4edc6c3c1d993 Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Wed, 24 Jun 2020 23:18:50 +0530 Subject: [PATCH 05/23] Fix the checking of type file existence --- source/lib/index.ts | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/source/lib/index.ts b/source/lib/index.ts index 953c580a..a07e931e 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -12,19 +12,20 @@ export interface Options { typingsFile?: string; } -const checkTypingsFile = async (typings: string, options: Options) => { - const typingsPath = path.join(options.cwd, typings); - const typingsExist = await pathExists(typingsPath); +const findTypingsFile = async (pkg: any, options: Options) => { + let typingsExist: boolean; + let typings = pkg.types || pkg.typings || 'index.d.ts'; + + if (options.typingsFile) { + typings = options.typingsFile; + typingsExist = await pathExists(options.typingsFile); + } else { + typingsExist = await pathExists(path.join(options.cwd, typings)); + } if (!typingsExist) { throw new Error(`The type definition \`${typings}\` does not exist. Create one and try again.`); } -}; - -const findTypingsFile = async (pkg: any, options: Options) => { - const typings = pkg.types || pkg.typings || 'index.d.ts'; - - await checkTypingsFile(typings, options); return typings; }; @@ -65,16 +66,11 @@ export default async (options: Options = {cwd: process.cwd()}) => { } const pkg = pkgResult.packageJson; - const config = loadConfig(pkg as any, options.cwd); // Look for a typings file if not explicitly specified, otherwise use `index.d.ts` in the root directory. // If the file is not found, throw an error. - if (!typingsFile) { - typingsFile = await findTypingsFile(pkg, options); - } - - await checkTypingsFile(typingsFile, options); + typingsFile = await findTypingsFile(pkg, options); const testFiles = await findTestFiles(typingsFile, { ...options, From 35063ce920d34067b535451f510b5fe056187057 Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Thu, 25 Jun 2020 00:06:19 +0530 Subject: [PATCH 06/23] Use expect error instead of ignoring it --- source/lib/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/lib/index.ts b/source/lib/index.ts index a07e931e..b0793d62 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -35,7 +35,7 @@ const findTestFiles = async (typingsFile: string, options: Options & {config: Co const tsxTestFile = typingsFile.replace(/\.d\.ts$/, '.test-d.tsx'); const testDir = options.config.directory; - //@ts-ignore + // @ts-expect-error let testFiles = await globby([testFile, tsxTestFile], {cwd: options.cwd}); const testDirExists = await pathExists(path.join(options.cwd, testDir)); @@ -45,7 +45,7 @@ const findTestFiles = async (typingsFile: string, options: Options & {config: Co } if (testFiles.length === 0) { - //@ts-ignore + // @ts-expect-error testFiles = await globby([`${testDir}/**/*.ts`, `${testDir}/**/*.tsx`], {cwd: options.cwd}); } From 4d239088ab4532221052079bf8c001d0820f0e5b Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Thu, 25 Jun 2020 20:51:36 +0530 Subject: [PATCH 07/23] Allow specifing test files manually --- readme.md | 7 +++++++ source/lib/compiler.ts | 5 +---- source/lib/index.ts | 18 ++++++++++-------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/readme.md b/readme.md index 3f18ceae..71f2878e 100644 --- a/readme.md +++ b/readme.md @@ -220,6 +220,13 @@ Default: `''` Path to the type definitions of the project. +##### testFiles + +type: `string[]` +default: `['']` + +An array of test files with their path + ## License MIT © [Sam Verschueren](https://github.com/SamVerschueren) diff --git a/source/lib/compiler.ts b/source/lib/compiler.ts index 07d67582..321d9082 100644 --- a/source/lib/compiler.ts +++ b/source/lib/compiler.ts @@ -1,4 +1,3 @@ -import * as path from 'path'; import { flattenDiagnosticMessageText, createProgram, @@ -65,11 +64,9 @@ const ignoreDiagnostic = (diagnostic: TSDiagnostic, expectedErrors: Map { - const fileNames = context.testFiles.map(fileName => path.join(context.cwd, fileName)); - const diagnostics: Diagnostic[] = []; - const program = createProgram(fileNames, context.config.compilerOptions); + const program = createProgram(context.testFiles, context.config.compilerOptions); const tsDiagnostics = program .getSemanticDiagnostics() diff --git a/source/lib/index.ts b/source/lib/index.ts index b0793d62..95359289 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -10,6 +10,7 @@ import {Context, Config} from './interfaces'; export interface Options { cwd: string; typingsFile?: string; + testFiles?: string[]; } const findTypingsFile = async (pkg: any, options: Options) => { @@ -35,7 +36,6 @@ const findTestFiles = async (typingsFile: string, options: Options & {config: Co const tsxTestFile = typingsFile.replace(/\.d\.ts$/, '.test-d.tsx'); const testDir = options.config.directory; - // @ts-expect-error let testFiles = await globby([testFile, tsxTestFile], {cwd: options.cwd}); const testDirExists = await pathExists(path.join(options.cwd, testDir)); @@ -45,11 +45,10 @@ const findTestFiles = async (typingsFile: string, options: Options & {config: Co } if (testFiles.length === 0) { - // @ts-expect-error testFiles = await globby([`${testDir}/**/*.ts`, `${testDir}/**/*.tsx`], {cwd: options.cwd}); } - return testFiles; + return testFiles.map(fileName => path.join(options.cwd, fileName)); }; /** @@ -58,6 +57,7 @@ const findTestFiles = async (typingsFile: string, options: Options & {config: Co * @returns A promise which resolves the diagnostics of the type definition. */ export default async (options: Options = {cwd: process.cwd()}) => { + let testFiles = options.testFiles ? options.testFiles : ['']; let typingsFile = options.typingsFile ? options.typingsFile : ''; const pkgResult = await readPkgUp({cwd: options.cwd}); @@ -68,14 +68,16 @@ export default async (options: Options = {cwd: process.cwd()}) => { const pkg = pkgResult.packageJson; const config = loadConfig(pkg as any, options.cwd); - // Look for a typings file if not explicitly specified, otherwise use `index.d.ts` in the root directory. + // Look for a typings file if not explicitly specified, otherwise use `index.d.ts` in the root directory. // If the file is not found, throw an error. typingsFile = await findTypingsFile(pkg, options); - const testFiles = await findTestFiles(typingsFile, { - ...options, - config - }); + if (!testFiles[0]) { + testFiles = await findTestFiles(typingsFile, { + ...options, + config + }); + } const context: Context = { pkg, From c7e427b2217083992cb173844109c877d4df2144 Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Thu, 2 Jul 2020 14:40:58 +0530 Subject: [PATCH 08/23] Allow passing relative path of typing def file --- source/lib/index.ts | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/source/lib/index.ts b/source/lib/index.ts index 95359289..aa33af62 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -14,15 +14,8 @@ export interface Options { } const findTypingsFile = async (pkg: any, options: Options) => { - let typingsExist: boolean; - let typings = pkg.types || pkg.typings || 'index.d.ts'; - - if (options.typingsFile) { - typings = options.typingsFile; - typingsExist = await pathExists(options.typingsFile); - } else { - typingsExist = await pathExists(path.join(options.cwd, typings)); - } + let typings = options.typingsFile || pkg.types || pkg.typings || 'index.d.ts'; + const typingsExist = await pathExists(path.join(options.cwd, typings)); if (!typingsExist) { throw new Error(`The type definition \`${typings}\` does not exist. Create one and try again.`); @@ -58,7 +51,6 @@ const findTestFiles = async (typingsFile: string, options: Options & {config: Co */ export default async (options: Options = {cwd: process.cwd()}) => { let testFiles = options.testFiles ? options.testFiles : ['']; - let typingsFile = options.typingsFile ? options.typingsFile : ''; const pkgResult = await readPkgUp({cwd: options.cwd}); if (!pkgResult) { @@ -70,7 +62,7 @@ export default async (options: Options = {cwd: process.cwd()}) => { // Look for a typings file if not explicitly specified, otherwise use `index.d.ts` in the root directory. // If the file is not found, throw an error. - typingsFile = await findTypingsFile(pkg, options); + const typingsFile = await findTypingsFile(pkg, options); if (!testFiles[0]) { testFiles = await findTestFiles(typingsFile, { From 4cf8cb47d5245dbf8475621e5a856814113897dd Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Thu, 2 Jul 2020 15:10:57 +0530 Subject: [PATCH 09/23] Allow passing relative path to test files --- source/lib/index.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/source/lib/index.ts b/source/lib/index.ts index aa33af62..d71b66c1 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -14,7 +14,7 @@ export interface Options { } const findTypingsFile = async (pkg: any, options: Options) => { - let typings = options.typingsFile || pkg.types || pkg.typings || 'index.d.ts'; + const typings = options.typingsFile || pkg.types || pkg.typings || 'index.d.ts'; const typingsExist = await pathExists(path.join(options.cwd, typings)); if (!typingsExist) { @@ -25,12 +25,15 @@ const findTypingsFile = async (pkg: any, options: Options) => { }; const findTestFiles = async (typingsFile: string, options: Options & {config: Config}) => { + if (options.testFiles?.length) { + return options.testFiles.map(fileName => path.join(options.cwd, fileName)); + } + const testFile = typingsFile.replace(/\.d\.ts$/, '.test-d.ts'); const tsxTestFile = typingsFile.replace(/\.d\.ts$/, '.test-d.tsx'); const testDir = options.config.directory; let testFiles = await globby([testFile, tsxTestFile], {cwd: options.cwd}); - const testDirExists = await pathExists(path.join(options.cwd, testDir)); if (testFiles.length === 0 && !testDirExists) { @@ -50,7 +53,6 @@ const findTestFiles = async (typingsFile: string, options: Options & {config: Co * @returns A promise which resolves the diagnostics of the type definition. */ export default async (options: Options = {cwd: process.cwd()}) => { - let testFiles = options.testFiles ? options.testFiles : ['']; const pkgResult = await readPkgUp({cwd: options.cwd}); if (!pkgResult) { @@ -64,12 +66,10 @@ export default async (options: Options = {cwd: process.cwd()}) => { // If the file is not found, throw an error. const typingsFile = await findTypingsFile(pkg, options); - if (!testFiles[0]) { - testFiles = await findTestFiles(typingsFile, { - ...options, - config - }); - } + const testFiles = await findTestFiles(typingsFile, { + ...options, + config + }); const context: Context = { pkg, From ea3961ac501ab5b5c57ff5a3d202e809df946d0d Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Thu, 23 Jul 2020 16:38:25 +0530 Subject: [PATCH 10/23] Make changes according to suggestions --- source/lib/index.ts | 9 ++++----- tsconfig.json | 1 - 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/source/lib/index.ts b/source/lib/index.ts index d71b66c1..6c301ac0 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -62,8 +62,7 @@ export default async (options: Options = {cwd: process.cwd()}) => { const pkg = pkgResult.packageJson; const config = loadConfig(pkg as any, options.cwd); - // Look for a typings file if not explicitly specified, otherwise use `index.d.ts` in the root directory. - // If the file is not found, throw an error. + // Look for a typings file, otherwise use `index.d.ts` in the root directory. If the file is not found, throw an error. const typingsFile = await findTypingsFile(pkg, options); const testFiles = await findTestFiles(typingsFile, { @@ -72,11 +71,11 @@ export default async (options: Options = {cwd: process.cwd()}) => { }); const context: Context = { + cwd: options.cwd, pkg, - config, - testFiles, typingsFile, - cwd: options.cwd + testFiles, + config }; return [ diff --git a/tsconfig.json b/tsconfig.json index 50b8f1e0..e7ad6173 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,7 +18,6 @@ "noUnusedParameters": true, "noFallthroughCasesInSwitch": true, "noEmitOnError": true, - "sourceMap": true, "forceConsistentCasingInFileNames": true }, "exclude": [ From 134b88cfc98543713036ef88c3724125973dfa79 Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Fri, 24 Jul 2020 15:25:42 +0530 Subject: [PATCH 11/23] Fixed README alignment --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 71f2878e..ef5b0bec 100644 --- a/readme.md +++ b/readme.md @@ -222,7 +222,7 @@ Path to the type definitions of the project. ##### testFiles -type: `string[]` +type: `string[]`
default: `['']` An array of test files with their path From 64409b32303dc8699a5ba3213fc9a8741a5c5406 Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Fri, 7 Aug 2020 11:50:58 +0530 Subject: [PATCH 12/23] Add tests for new typingsFile option --- source/test/fixtures/typings-custom-dir/index.js | 3 +++ .../test/fixtures/typings-custom-dir/index.test-d.ts | 5 +++++ source/test/fixtures/typings-custom-dir/package.json | 3 +++ .../test/fixtures/typings-custom-dir/utils/index.d.ts | 6 ++++++ source/test/test.ts | 11 +++++++++++ 5 files changed, 28 insertions(+) create mode 100644 source/test/fixtures/typings-custom-dir/index.js create mode 100644 source/test/fixtures/typings-custom-dir/index.test-d.ts create mode 100644 source/test/fixtures/typings-custom-dir/package.json create mode 100644 source/test/fixtures/typings-custom-dir/utils/index.d.ts diff --git a/source/test/fixtures/typings-custom-dir/index.js b/source/test/fixtures/typings-custom-dir/index.js new file mode 100644 index 00000000..f17717f5 --- /dev/null +++ b/source/test/fixtures/typings-custom-dir/index.js @@ -0,0 +1,3 @@ +module.exports.default = (foo, bar) => { + return foo + bar; +}; diff --git a/source/test/fixtures/typings-custom-dir/index.test-d.ts b/source/test/fixtures/typings-custom-dir/index.test-d.ts new file mode 100644 index 00000000..fcb8dd53 --- /dev/null +++ b/source/test/fixtures/typings-custom-dir/index.test-d.ts @@ -0,0 +1,5 @@ +import {expectType} from '../../..'; +import one from './index'; + +expectType(one('foo', 'bar')); +expectType(one(1, 2)); diff --git a/source/test/fixtures/typings-custom-dir/package.json b/source/test/fixtures/typings-custom-dir/package.json new file mode 100644 index 00000000..de6dc1db --- /dev/null +++ b/source/test/fixtures/typings-custom-dir/package.json @@ -0,0 +1,3 @@ +{ + "name": "foo" +} diff --git a/source/test/fixtures/typings-custom-dir/utils/index.d.ts b/source/test/fixtures/typings-custom-dir/utils/index.d.ts new file mode 100644 index 00000000..0616ebaa --- /dev/null +++ b/source/test/fixtures/typings-custom-dir/utils/index.d.ts @@ -0,0 +1,6 @@ +declare const one: { + (foo: string, bar: string): string; + (foo: number, bar: number): number; +}; + +export default one; diff --git a/source/test/test.ts b/source/test/test.ts index ebe99335..6d72e167 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -220,3 +220,14 @@ test('strict types', async t => { verify(t, diagnostics, []); }); + +test('typings in custom directory', async t => { + const diagnostics = await tsd({ + cwd: path.join(__dirname, 'fixtures/typings-custom-dir'), + typingsFile: 'utils/index.d.ts' + }); + + verify(t, diagnostics, [ + [5, 19, 'error', 'Argument of type \'number\' is not assignable to parameter of type \'string\'.'] + ]); +}); From 6d24bca968883b3da5134b26ced3efd5c9cad5aa Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Fri, 7 Aug 2020 13:03:11 +0530 Subject: [PATCH 13/23] Fixed tests file resolving issue when typingsFile option is set --- source/lib/index.ts | 13 ++++++++++++- .../fixtures/typings-custom-dir/index.test-d.ts | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/source/lib/index.ts b/source/lib/index.ts index 6c301ac0..4b313859 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -24,11 +24,22 @@ const findTypingsFile = async (pkg: any, options: Options) => { return typings; }; -const findTestFiles = async (typingsFile: string, options: Options & {config: Config}) => { +const normalizeTypingsFilePath = (typingsFilePath: string, options: Options) => { + if (options.typingsFile) { + return path.basename(typingsFilePath); + } + + return typingsFilePath; +}; + +const findTestFiles = async (typingsFilePath: string, options: Options & {config: Config}) => { if (options.testFiles?.length) { return options.testFiles.map(fileName => path.join(options.cwd, fileName)); } + // Return only the file name if typingsFile option is used. + const typingsFile = normalizeTypingsFilePath(typingsFilePath, options); + const testFile = typingsFile.replace(/\.d\.ts$/, '.test-d.ts'); const tsxTestFile = typingsFile.replace(/\.d\.ts$/, '.test-d.tsx'); const testDir = options.config.directory; diff --git a/source/test/fixtures/typings-custom-dir/index.test-d.ts b/source/test/fixtures/typings-custom-dir/index.test-d.ts index fcb8dd53..a6fedd96 100644 --- a/source/test/fixtures/typings-custom-dir/index.test-d.ts +++ b/source/test/fixtures/typings-custom-dir/index.test-d.ts @@ -1,5 +1,5 @@ import {expectType} from '../../..'; -import one from './index'; +import one from './utils'; expectType(one('foo', 'bar')); expectType(one(1, 2)); From 1d391ae18daa3577a252f1b91f40e69cc9659d30 Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Fri, 7 Aug 2020 13:23:09 +0530 Subject: [PATCH 14/23] Add tests for testFiles option --- source/test/fixtures/specify-test-files/index.d.ts | 6 ++++++ source/test/fixtures/specify-test-files/index.js | 3 +++ .../test/fixtures/specify-test-files/package.json | 3 +++ .../fixtures/specify-test-files/unknown.test.ts | 5 +++++ source/test/test.ts | 13 +++++++++++++ 5 files changed, 30 insertions(+) create mode 100644 source/test/fixtures/specify-test-files/index.d.ts create mode 100644 source/test/fixtures/specify-test-files/index.js create mode 100644 source/test/fixtures/specify-test-files/package.json create mode 100644 source/test/fixtures/specify-test-files/unknown.test.ts diff --git a/source/test/fixtures/specify-test-files/index.d.ts b/source/test/fixtures/specify-test-files/index.d.ts new file mode 100644 index 00000000..0616ebaa --- /dev/null +++ b/source/test/fixtures/specify-test-files/index.d.ts @@ -0,0 +1,6 @@ +declare const one: { + (foo: string, bar: string): string; + (foo: number, bar: number): number; +}; + +export default one; diff --git a/source/test/fixtures/specify-test-files/index.js b/source/test/fixtures/specify-test-files/index.js new file mode 100644 index 00000000..f17717f5 --- /dev/null +++ b/source/test/fixtures/specify-test-files/index.js @@ -0,0 +1,3 @@ +module.exports.default = (foo, bar) => { + return foo + bar; +}; diff --git a/source/test/fixtures/specify-test-files/package.json b/source/test/fixtures/specify-test-files/package.json new file mode 100644 index 00000000..de6dc1db --- /dev/null +++ b/source/test/fixtures/specify-test-files/package.json @@ -0,0 +1,3 @@ +{ + "name": "foo" +} diff --git a/source/test/fixtures/specify-test-files/unknown.test.ts b/source/test/fixtures/specify-test-files/unknown.test.ts new file mode 100644 index 00000000..080ee4ca --- /dev/null +++ b/source/test/fixtures/specify-test-files/unknown.test.ts @@ -0,0 +1,5 @@ +import {expectType} from '../../..'; +import one from '.'; + +expectType(one('foo', 'bar')); +expectType(one(1, 2)); diff --git a/source/test/test.ts b/source/test/test.ts index 6d72e167..2d8245bd 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -231,3 +231,16 @@ test('typings in custom directory', async t => { [5, 19, 'error', 'Argument of type \'number\' is not assignable to parameter of type \'string\'.'] ]); }); + +test('specify test files manually', async t => { + const diagnostics = await tsd({ + cwd: path.join(__dirname, 'fixtures/specify-test-files'), + testFiles: [ + 'unknown.test.ts' + ] + }); + + verify(t, diagnostics, [ + [5, 19, 'error', 'Argument of type \'number\' is not assignable to parameter of type \'string\'.'] + ]); +}); From d92529637524ab9aa779b9a5671e48fc71aac9b0 Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Fri, 7 Aug 2020 14:12:00 +0530 Subject: [PATCH 15/23] Add tests to check cases where typings file is not found in specified path --- source/test/test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/source/test/test.ts b/source/test/test.ts index 2d8245bd..320aa507 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -244,3 +244,12 @@ test('specify test files manually', async t => { [5, 19, 'error', 'Argument of type \'number\' is not assignable to parameter of type \'string\'.'] ]); }); + +test('fails if typings file is not found in the specified path', async t => { + const error = await t.throwsAsync(tsd({ + cwd: path.join(__dirname, 'fixtures/typings-custom-dir'), + typingsFile: 'unknown.d.ts' + })); + + t.is(error.message, 'The type definition `unknown.d.ts` does not exist. Create one and try again.'); +}); From 59670d68f103de02ceeeb5ff122d09596498f405 Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Sun, 9 Aug 2020 18:38:43 +0530 Subject: [PATCH 16/23] Use globby for testFiles option --- source/lib/index.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/source/lib/index.ts b/source/lib/index.ts index 4b313859..2b843996 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -32,9 +32,19 @@ const normalizeTypingsFilePath = (typingsFilePath: string, options: Options) => return typingsFilePath; }; +const findCustomTestFiles = async (testFilesPattern: string[], cwd: string) => { + const testFiles = await globby(testFilesPattern, {cwd}); + + if (testFiles.length === 0) { + throw new Error('Could not find test files. Create one and try again'); + } + + return testFiles.map(file => path.join(cwd, file)); +}; + const findTestFiles = async (typingsFilePath: string, options: Options & {config: Config}) => { if (options.testFiles?.length) { - return options.testFiles.map(fileName => path.join(options.cwd, fileName)); + return findCustomTestFiles(options.testFiles, options.cwd); } // Return only the file name if typingsFile option is used. From 41e9e0c162e81be1c9ed4e9314dc6b45000a25ae Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Sun, 4 Oct 2020 16:37:02 +0530 Subject: [PATCH 17/23] Remove .vscode entry in gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index d85e9aa4..4dbf822c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ node_modules yarn.lock dist/ -.vscode/ From 02927f08fa5926b11386783a250fa19372cb42d6 Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Sun, 4 Oct 2020 17:08:15 +0530 Subject: [PATCH 18/23] Update docs --- readme.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index ef5b0bec..8c43f099 100644 --- a/readme.md +++ b/readme.md @@ -216,16 +216,16 @@ Current working directory of the project to retrieve the diagnostics for. ##### typingsFile Type: `string`
-Default: `''` +Default: takes value defined in `types` property at `package.json` Path to the type definitions of the project. ##### testFiles type: `string[]`
-default: `['']` +default: finds files with `.test-d.ts` or `.test-d.tsx` extension -An array of test files with their path +An array of test files with their path. ## License From 285c6cb4fc64746c5a2369a7aa9b7866f25aca8e Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Sun, 4 Oct 2020 17:13:13 +0530 Subject: [PATCH 19/23] Make testFiles property readonly --- source/lib/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/lib/index.ts b/source/lib/index.ts index 2b843996..78844fcd 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -10,7 +10,7 @@ import {Context, Config} from './interfaces'; export interface Options { cwd: string; typingsFile?: string; - testFiles?: string[]; + testFiles?: readonly string[]; } const findTypingsFile = async (pkg: any, options: Options) => { @@ -32,7 +32,7 @@ const normalizeTypingsFilePath = (typingsFilePath: string, options: Options) => return typingsFilePath; }; -const findCustomTestFiles = async (testFilesPattern: string[], cwd: string) => { +const findCustomTestFiles = async (testFilesPattern: readonly string[], cwd: string) => { const testFiles = await globby(testFilesPattern, {cwd}); if (testFiles.length === 0) { From 59c5f4b23fe37dae5a915c2bb083bc4d239c4466 Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Sun, 4 Oct 2020 17:14:11 +0530 Subject: [PATCH 20/23] Fix grammar --- source/lib/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/lib/index.ts b/source/lib/index.ts index 78844fcd..5be84b42 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -36,7 +36,7 @@ const findCustomTestFiles = async (testFilesPattern: readonly string[], cwd: str const testFiles = await globby(testFilesPattern, {cwd}); if (testFiles.length === 0) { - throw new Error('Could not find test files. Create one and try again'); + throw new Error('Could not find any test files. Create one and try again'); } return testFiles.map(file => path.join(cwd, file)); @@ -47,7 +47,7 @@ const findTestFiles = async (typingsFilePath: string, options: Options & {config return findCustomTestFiles(options.testFiles, options.cwd); } - // Return only the file name if typingsFile option is used. + // Return only the filename if the `typingsFile` option is used. const typingsFile = normalizeTypingsFilePath(typingsFilePath, options); const testFile = typingsFile.replace(/\.d\.ts$/, '.test-d.ts'); From 9ece40eeaa774d89fe3ff9d04555d230c3e7d2ea Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Mon, 5 Oct 2020 14:48:50 +0530 Subject: [PATCH 21/23] Grammar in README --- readme.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index 8c43f099..2cbf3bd6 100644 --- a/readme.md +++ b/readme.md @@ -216,14 +216,14 @@ Current working directory of the project to retrieve the diagnostics for. ##### typingsFile Type: `string`
-Default: takes value defined in `types` property at `package.json` +Default: The `types` property in `package.json`. Path to the type definitions of the project. ##### testFiles -type: `string[]`
-default: finds files with `.test-d.ts` or `.test-d.tsx` extension +Type: `string[]`
+Default: Finds files with `.test-d.ts` or `.test-d.tsx` extension. An array of test files with their path. From 44c066e9a76680ece82a18aa4952a4c44f08fae2 Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Thu, 3 Dec 2020 00:52:49 -0800 Subject: [PATCH 22/23] Update readme.md Co-authored-by: Sam Verschueren --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 2cbf3bd6..8d6f69c4 100644 --- a/readme.md +++ b/readme.md @@ -218,7 +218,7 @@ Current working directory of the project to retrieve the diagnostics for. Type: `string`
Default: The `types` property in `package.json`. -Path to the type definitions of the project. +Path to the type definition file you want to test. This can be useful when using a test runner to test specific type definitions per test. ##### testFiles From c54958783d9dbc0b2a23aca6f158f7c8d123feed Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Thu, 3 Dec 2020 14:31:34 +0530 Subject: [PATCH 23/23] Improve readme --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 8d6f69c4..5e1dcf9a 100644 --- a/readme.md +++ b/readme.md @@ -225,7 +225,7 @@ Path to the type definition file you want to test. This can be useful when using Type: `string[]`
Default: Finds files with `.test-d.ts` or `.test-d.tsx` extension. -An array of test files with their path. +An array of test files with their path. Uses [globby](https://github.com/sindresorhus/globby) under the hood so that you can fine tune test file discovery. ## License