From 012248871d0731cf2777d8b108f8ae9888017896 Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Wed, 24 Jun 2020 19:28:51 +0530 Subject: [PATCH 01/41] 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/41] 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/41] 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/41] 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/41] 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/41] 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/41] 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/41] 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/41] 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 1bf9d153882a3df9093ebcf1773c944ef1982d21 Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Wed, 8 Jul 2020 18:48:46 +0530 Subject: [PATCH 10/41] Counts number of tests. Still needs to pass it. --- source/lib/compiler.ts | 5 +++++ source/lib/index.ts | 5 ++++- source/lib/interfaces.ts | 1 + source/lib/rules/index.ts | 4 +++- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/source/lib/compiler.ts b/source/lib/compiler.ts index 321d9082..f3388a83 100644 --- a/source/lib/compiler.ts +++ b/source/lib/compiler.ts @@ -64,6 +64,7 @@ const ignoreDiagnostic = (diagnostic: TSDiagnostic, expectedErrors: Map { + let numTests: number = 0; const diagnostics: Diagnostic[] = []; const program = createProgram(context.testFiles, context.config.compilerOptions); @@ -74,6 +75,10 @@ export const getDiagnostics = (context: Context): Diagnostic[] => { const assertions = extractAssertions(program); + for (const assertion of assertions) { + numTests = numTests + assertion[1].size; + } + diagnostics.push(...handle(program.getTypeChecker() as TypeChecker, assertions)); const expectedErrors = parseErrorAssertionToLocation(assertions); diff --git a/source/lib/index.ts b/source/lib/index.ts index d71b66c1..518b0057 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -11,6 +11,7 @@ export interface Options { cwd: string; typingsFile?: string; testFiles?: string[]; + verbose?: boolean; } const findTypingsFile = async (pkg: any, options: Options) => { @@ -53,6 +54,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()}) => { + const isVerbose = options.verbose ? true : false; const pkgResult = await readPkgUp({cwd: options.cwd}); if (!pkgResult) { @@ -76,7 +78,8 @@ export default async (options: Options = {cwd: process.cwd()}) => { config, testFiles, typingsFile, - cwd: options.cwd + cwd: options.cwd, + verbose: isVerbose }; return [ diff --git a/source/lib/interfaces.ts b/source/lib/interfaces.ts index bb289339..2341ea21 100644 --- a/source/lib/interfaces.ts +++ b/source/lib/interfaces.ts @@ -17,6 +17,7 @@ export interface Context { typingsFile: string; testFiles: string[]; config: Config; + verbose: boolean; } export enum DiagnosticCode { diff --git a/source/lib/rules/index.ts b/source/lib/rules/index.ts index a4229e0a..b2464e05 100644 --- a/source/lib/rules/index.ts +++ b/source/lib/rules/index.ts @@ -16,10 +16,12 @@ const rules = new Set([ * @param context - The context object. * @returns List of diagnostics */ -export default (context: Context) => { +export default (context: Context): Diagnostic[] => { + let numTests: number = 0; const diagnostics: Diagnostic[] = []; for (const rule of rules) { + numTests = numTests + 1; diagnostics.push(...rule(context)); } From 544c40159e00bc5364ed941ee4ace1d402d9dec4 Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Thu, 9 Jul 2020 13:26:03 +0530 Subject: [PATCH 11/41] [WIP] Verbose mode --- source/lib/compiler.ts | 6 +++--- source/lib/index.ts | 23 +++++++++++++++++++---- source/lib/interfaces.ts | 5 +++++ source/lib/rules/index.ts | 6 +++--- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/source/lib/compiler.ts b/source/lib/compiler.ts index f3388a83..a6528318 100644 --- a/source/lib/compiler.ts +++ b/source/lib/compiler.ts @@ -6,7 +6,7 @@ import { } from '../../libraries/typescript'; import {TypeChecker} from './entities/typescript'; import {extractAssertions, parseErrorAssertionToLocation} from './parser'; -import {Diagnostic, DiagnosticCode, Context, Location} from './interfaces'; +import {Diagnostic, DiagnosticCode, Context, Location, ExtendedDiagnostic} from './interfaces'; import {handle} from './assertions'; // List of diagnostic codes that should be ignored in general @@ -63,7 +63,7 @@ const ignoreDiagnostic = (diagnostic: TSDiagnostic, expectedErrors: Map { +export const getDiagnostics = (context: Context): ExtendedDiagnostic => { let numTests: number = 0; const diagnostics: Diagnostic[] = []; @@ -107,5 +107,5 @@ export const getDiagnostics = (context: Context): Diagnostic[] => { }); } - return diagnostics; + return {numTests, diagnostics}; }; diff --git a/source/lib/index.ts b/source/lib/index.ts index 518b0057..2a327975 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -82,8 +82,23 @@ export default async (options: Options = {cwd: process.cwd()}) => { verbose: isVerbose }; - return [ - ...getCustomDiagnostics(context), - ...getTSDiagnostics(context) - ]; + const tsDiagnostics = getTSDiagnostics(context); + const customDiagnostics = getCustomDiagnostics(context); + + if (!isVerbose) { + return [ + ...customDiagnostics.diagnostics, + ...tsDiagnostics.diagnostics + ]; + } + + const numTests = tsDiagnostics.numTests + customDiagnostics.numTests; + + return { + numTests, + diagnostics: [ + ...customDiagnostics.diagnostics, + ...tsDiagnostics.diagnostics + ] + }; }; diff --git a/source/lib/interfaces.ts b/source/lib/interfaces.ts index 2341ea21..6003adb2 100644 --- a/source/lib/interfaces.ts +++ b/source/lib/interfaces.ts @@ -39,6 +39,11 @@ export interface Diagnostic { column?: number; } +export interface ExtendedDiagnostic { + numTests: number; + diagnostics: Diagnostic[]; +} + export interface Location { fileName: string; start: number; diff --git a/source/lib/rules/index.ts b/source/lib/rules/index.ts index b2464e05..46c5a9f2 100644 --- a/source/lib/rules/index.ts +++ b/source/lib/rules/index.ts @@ -1,6 +1,6 @@ import filesProperty from './files-property'; import typesProperty from './types-property'; -import {Diagnostic, Context} from '../interfaces'; +import {Diagnostic, Context, ExtendedDiagnostic} from '../interfaces'; type RuleFunction = (context: Context) => Diagnostic[]; @@ -16,7 +16,7 @@ const rules = new Set([ * @param context - The context object. * @returns List of diagnostics */ -export default (context: Context): Diagnostic[] => { +export default (context: Context): ExtendedDiagnostic => { let numTests: number = 0; const diagnostics: Diagnostic[] = []; @@ -25,5 +25,5 @@ export default (context: Context): Diagnostic[] => { diagnostics.push(...rule(context)); } - return diagnostics; + return {numTests, diagnostics}; }; From 039d8e9c8107702c032b14efec40914c74a4faf5 Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Wed, 22 Jul 2020 16:00:47 +0530 Subject: [PATCH 12/41] Remove verbose mode option --- source/lib/index.ts | 13 +------------ source/lib/interfaces.ts | 1 - 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/source/lib/index.ts b/source/lib/index.ts index 2a327975..644e6cd9 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -11,7 +11,6 @@ export interface Options { cwd: string; typingsFile?: string; testFiles?: string[]; - verbose?: boolean; } const findTypingsFile = async (pkg: any, options: Options) => { @@ -54,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()}) => { - const isVerbose = options.verbose ? true : false; const pkgResult = await readPkgUp({cwd: options.cwd}); if (!pkgResult) { @@ -78,20 +76,11 @@ export default async (options: Options = {cwd: process.cwd()}) => { config, testFiles, typingsFile, - cwd: options.cwd, - verbose: isVerbose + cwd: options.cwd }; const tsDiagnostics = getTSDiagnostics(context); const customDiagnostics = getCustomDiagnostics(context); - - if (!isVerbose) { - return [ - ...customDiagnostics.diagnostics, - ...tsDiagnostics.diagnostics - ]; - } - const numTests = tsDiagnostics.numTests + customDiagnostics.numTests; return { diff --git a/source/lib/interfaces.ts b/source/lib/interfaces.ts index 6003adb2..d7cfac94 100644 --- a/source/lib/interfaces.ts +++ b/source/lib/interfaces.ts @@ -17,7 +17,6 @@ export interface Context { typingsFile: string; testFiles: string[]; config: Config; - verbose: boolean; } export enum DiagnosticCode { From 8a9c90ee7f6e25e2108a87f5ac85338237404201 Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Wed, 22 Jul 2020 17:14:00 +0530 Subject: [PATCH 13/41] Support for ExtendedDiagnostics --- source/cli.ts | 5 +++-- source/lib/compiler.ts | 2 +- source/lib/formatter.ts | 5 +++-- source/lib/rules/index.ts | 2 +- source/test/fixtures/utils.ts | 7 ++++--- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/source/cli.ts b/source/cli.ts index 6712b892..e9d6cc6f 100644 --- a/source/cli.ts +++ b/source/cli.ts @@ -23,10 +23,11 @@ const cli = meow(` try { const options = cli.input.length > 0 ? {cwd: cli.input[0]} : undefined; - const diagnostics = await tsd(options); + const extendedDiagnostics = await tsd(options); + const diagnostics = extendedDiagnostics.diagnostics; if (diagnostics.length > 0) { - throw new Error(formatter(diagnostics)); + throw new Error(formatter(extendedDiagnostics)); } } catch (error) { console.error(error.message); diff --git a/source/lib/compiler.ts b/source/lib/compiler.ts index a6528318..d71440e9 100644 --- a/source/lib/compiler.ts +++ b/source/lib/compiler.ts @@ -64,7 +64,7 @@ const ignoreDiagnostic = (diagnostic: TSDiagnostic, expectedErrors: Map { - let numTests: number = 0; + let numTests = 0; const diagnostics: Diagnostic[] = []; const program = createProgram(context.testFiles, context.config.compilerOptions); diff --git a/source/lib/formatter.ts b/source/lib/formatter.ts index 3fdb23eb..9bafb36f 100644 --- a/source/lib/formatter.ts +++ b/source/lib/formatter.ts @@ -1,5 +1,5 @@ import * as formatter from 'eslint-formatter-pretty'; -import {Diagnostic} from './interfaces'; +import {ExtendedDiagnostic} from './interfaces'; /** * Format the TypeScript diagnostics to a human readable output. @@ -7,7 +7,8 @@ import {Diagnostic} from './interfaces'; * @param diagnostics - List of TypeScript diagnostics. * @returns Beautiful diagnostics output */ -export default (diagnostics: Diagnostic[]) => { +export default (extendedDiagnostics: ExtendedDiagnostic) => { + const diagnostics = extendedDiagnostics.diagnostics; const fileMap = new Map(); for (const diagnostic of diagnostics) { diff --git a/source/lib/rules/index.ts b/source/lib/rules/index.ts index 46c5a9f2..ab352a54 100644 --- a/source/lib/rules/index.ts +++ b/source/lib/rules/index.ts @@ -17,7 +17,7 @@ const rules = new Set([ * @returns List of diagnostics */ export default (context: Context): ExtendedDiagnostic => { - let numTests: number = 0; + let numTests = 0; const diagnostics: Diagnostic[] = []; for (const rule of rules) { diff --git a/source/test/fixtures/utils.ts b/source/test/fixtures/utils.ts index cab9e6e7..12e2e168 100644 --- a/source/test/fixtures/utils.ts +++ b/source/test/fixtures/utils.ts @@ -1,5 +1,5 @@ import {ExecutionContext} from 'ava'; -import {Diagnostic} from '../../lib/interfaces'; +import {ExtendedDiagnostic} from '../../lib/interfaces'; type Expectation = [number, number, 'error' | 'warning', string, (string | RegExp)?]; @@ -7,10 +7,11 @@ type Expectation = [number, number, 'error' | 'warning', string, (string | RegEx * Verify a list of diagnostics. * * @param t - The AVA execution context. - * @param diagnostics - List of diagnostics to verify. + * @param extendedDiagnostics - Object containing numTests and list of diagnostics to verify * @param expectations - Expected diagnostics. */ -export const verify = (t: ExecutionContext, diagnostics: Diagnostic[], expectations: Expectation[]) => { +export const verify = (t: ExecutionContext, extendedDiagnostics: ExtendedDiagnostic, expectations: Expectation[]) => { + const diagnostics = extendedDiagnostics.diagnostics; t.true(diagnostics.length === expectations.length); for (const [index, diagnostic] of diagnostics.entries()) { From ea3961ac501ab5b5c57ff5a3d202e809df946d0d Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Thu, 23 Jul 2020 16:38:25 +0530 Subject: [PATCH 14/41] 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 15/41] 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 a447c7d41b67237f7bae879133366c8f968eee1d Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Wed, 29 Jul 2020 13:34:47 +0530 Subject: [PATCH 16/41] Remove counting custom rules as a test --- source/lib/index.ts | 4 ++-- source/lib/rules/index.ts | 8 +++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/source/lib/index.ts b/source/lib/index.ts index 915559ff..21edb420 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -80,12 +80,12 @@ export default async (options: Options = {cwd: process.cwd()}) => { const tsDiagnostics = getTSDiagnostics(context); const customDiagnostics = getCustomDiagnostics(context); - const numTests = tsDiagnostics.numTests + customDiagnostics.numTests; + const numTests = tsDiagnostics.numTests; return { numTests, diagnostics: [ - ...customDiagnostics.diagnostics, + ...customDiagnostics, ...tsDiagnostics.diagnostics ] }; diff --git a/source/lib/rules/index.ts b/source/lib/rules/index.ts index ab352a54..d4f71634 100644 --- a/source/lib/rules/index.ts +++ b/source/lib/rules/index.ts @@ -1,6 +1,6 @@ import filesProperty from './files-property'; import typesProperty from './types-property'; -import {Diagnostic, Context, ExtendedDiagnostic} from '../interfaces'; +import {Diagnostic, Context} from '../interfaces'; type RuleFunction = (context: Context) => Diagnostic[]; @@ -16,14 +16,12 @@ const rules = new Set([ * @param context - The context object. * @returns List of diagnostics */ -export default (context: Context): ExtendedDiagnostic => { - let numTests = 0; +export default (context: Context): Diagnostic[] => { const diagnostics: Diagnostic[] = []; for (const rule of rules) { - numTests = numTests + 1; diagnostics.push(...rule(context)); } - return {numTests, diagnostics}; + return diagnostics; }; From da0741a712b7198dd2fadc99f147ddd5d54d3420 Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Thu, 6 Aug 2020 21:02:05 +0530 Subject: [PATCH 17/41] Provide docs for new changes --- readme.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index 71f2878e..344d0c24 100644 --- a/readme.md +++ b/readme.md @@ -191,10 +191,13 @@ You can use the programmatic API to retrieve the diagnostics and do something wi import tsd from 'tsd'; (async () => { - const diagnostics = await tsd(); + const diagnoser = await tsd(); - console.log(diagnostics.length); - //=> 2 + // Returns the number of tests evaludated. + console.log(diagnoser.numTests) + + // Returns the diagnostics if any or just an empty array + console.log(diagnoser.diagnostics); })(); ``` From 64409b32303dc8699a5ba3213fc9a8741a5c5406 Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Fri, 7 Aug 2020 11:50:58 +0530 Subject: [PATCH 18/41] 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 19/41] 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 20/41] 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 21/41] 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 22/41] 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 9eb0912f272699af849584a8b0482b9700b7f311 Mon Sep 17 00:00:00 2001 From: SaurabhAgarwala Date: Mon, 10 Aug 2020 23:10:21 +0530 Subject: [PATCH 23/41] Add unit test to check numTests parameter --- source/test/test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/source/test/test.ts b/source/test/test.ts index 320aa507..b4cc9789 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -253,3 +253,9 @@ test('fails if typings file is not found in the specified path', async t => { t.is(error.message, 'The type definition `unknown.d.ts` does not exist. Create one and try again.'); }); + +test('checking numTests', async t => { + const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/failure')}); + + t.is(diagnostics.numTests, 2); +}); From 41e9e0c162e81be1c9ed4e9314dc6b45000a25ae Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Sun, 4 Oct 2020 16:37:02 +0530 Subject: [PATCH 24/41] 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 25/41] 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 26/41] 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 27/41] 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 641e5cda0c2a0c5611158f510661bf5ac52f65fc Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Sun, 4 Oct 2020 17:19:23 +0530 Subject: [PATCH 28/41] Grammar and typo fix --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index a2008228..cb42e3e8 100644 --- a/readme.md +++ b/readme.md @@ -193,10 +193,10 @@ import tsd from 'tsd'; (async () => { const diagnoser = await tsd(); - // Returns the number of tests evaludated. + // Returns the number of tests evaluated. console.log(diagnoser.numTests) - // Returns the diagnostics if any or just an empty array + // Returns the diagnostics if any or just an empty array. console.log(diagnoser.diagnostics); })(); ``` From 9ece40eeaa774d89fe3ff9d04555d230c3e7d2ea Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Mon, 5 Oct 2020 14:48:50 +0530 Subject: [PATCH 29/41] 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 30/41] 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 31/41] 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 From 9424f463e8ff3c161ca0fee39d75e0de19632baf Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Thu, 3 Dec 2020 14:54:23 +0530 Subject: [PATCH 32/41] Refactor numTests into testCount --- readme.md | 2 +- source/lib/compiler.ts | 6 +++--- source/lib/index.ts | 4 ++-- source/lib/interfaces.ts | 2 +- source/test/fixtures/utils.ts | 2 +- source/test/test.ts | 4 ++-- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/readme.md b/readme.md index da6c9d9d..55686df0 100644 --- a/readme.md +++ b/readme.md @@ -194,7 +194,7 @@ import tsd from 'tsd'; const diagnoser = await tsd(); // Returns the number of tests evaluated. - console.log(diagnoser.numTests) + console.log(diagnoser.testCount) // Returns the diagnostics if any or just an empty array. console.log(diagnoser.diagnostics); diff --git a/source/lib/compiler.ts b/source/lib/compiler.ts index d71440e9..8fada40b 100644 --- a/source/lib/compiler.ts +++ b/source/lib/compiler.ts @@ -64,7 +64,7 @@ const ignoreDiagnostic = (diagnostic: TSDiagnostic, expectedErrors: Map { - let numTests = 0; + let testCount = 0; const diagnostics: Diagnostic[] = []; const program = createProgram(context.testFiles, context.config.compilerOptions); @@ -76,7 +76,7 @@ export const getDiagnostics = (context: Context): ExtendedDiagnostic => { const assertions = extractAssertions(program); for (const assertion of assertions) { - numTests = numTests + assertion[1].size; + testCount = testCount + assertion[1].size; } diagnostics.push(...handle(program.getTypeChecker() as TypeChecker, assertions)); @@ -107,5 +107,5 @@ export const getDiagnostics = (context: Context): ExtendedDiagnostic => { }); } - return {numTests, diagnostics}; + return {testCount, diagnostics}; }; diff --git a/source/lib/index.ts b/source/lib/index.ts index 9508e1f3..7e92053e 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -101,10 +101,10 @@ export default async (options: Options = {cwd: process.cwd()}) => { const tsDiagnostics = getTSDiagnostics(context); const customDiagnostics = getCustomDiagnostics(context); - const numTests = tsDiagnostics.numTests; + const testCount = tsDiagnostics.testCount; return { - numTests, + testCount, diagnostics: [ ...customDiagnostics, ...tsDiagnostics.diagnostics diff --git a/source/lib/interfaces.ts b/source/lib/interfaces.ts index d7cfac94..74344d43 100644 --- a/source/lib/interfaces.ts +++ b/source/lib/interfaces.ts @@ -39,7 +39,7 @@ export interface Diagnostic { } export interface ExtendedDiagnostic { - numTests: number; + testCount: number; diagnostics: Diagnostic[]; } diff --git a/source/test/fixtures/utils.ts b/source/test/fixtures/utils.ts index 12e2e168..d915f59a 100644 --- a/source/test/fixtures/utils.ts +++ b/source/test/fixtures/utils.ts @@ -7,7 +7,7 @@ type Expectation = [number, number, 'error' | 'warning', string, (string | RegEx * Verify a list of diagnostics. * * @param t - The AVA execution context. - * @param extendedDiagnostics - Object containing numTests and list of diagnostics to verify + * @param extendedDiagnostics - Object containing testCount and list of diagnostics to verify * @param expectations - Expected diagnostics. */ export const verify = (t: ExecutionContext, extendedDiagnostics: ExtendedDiagnostic, expectations: Expectation[]) => { diff --git a/source/test/test.ts b/source/test/test.ts index b4cc9789..e4501150 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -254,8 +254,8 @@ test('fails if typings file is not found in the specified path', async t => { t.is(error.message, 'The type definition `unknown.d.ts` does not exist. Create one and try again.'); }); -test('checking numTests', async t => { +test('checking testCount', async t => { const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/failure')}); - t.is(diagnostics.numTests, 2); + t.is(diagnostics.testCount, 2); }); From 473f255c8cdc3a11f71e026d7a0f423a8127dd12 Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Wed, 8 Jul 2020 18:48:46 +0530 Subject: [PATCH 33/41] Counts number of tests. Still needs to pass it. --- source/lib/compiler.ts | 5 +++++ source/lib/index.ts | 8 ++++++-- source/lib/interfaces.ts | 1 + source/lib/rules/index.ts | 4 +++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/source/lib/compiler.ts b/source/lib/compiler.ts index 321d9082..f3388a83 100644 --- a/source/lib/compiler.ts +++ b/source/lib/compiler.ts @@ -64,6 +64,7 @@ const ignoreDiagnostic = (diagnostic: TSDiagnostic, expectedErrors: Map { + let numTests: number = 0; const diagnostics: Diagnostic[] = []; const program = createProgram(context.testFiles, context.config.compilerOptions); @@ -74,6 +75,10 @@ export const getDiagnostics = (context: Context): Diagnostic[] => { const assertions = extractAssertions(program); + for (const assertion of assertions) { + numTests = numTests + assertion[1].size; + } + diagnostics.push(...handle(program.getTypeChecker() as TypeChecker, assertions)); const expectedErrors = parseErrorAssertionToLocation(assertions); diff --git a/source/lib/index.ts b/source/lib/index.ts index 5be84b42..e55de744 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -10,7 +10,8 @@ import {Context, Config} from './interfaces'; export interface Options { cwd: string; typingsFile?: string; - testFiles?: readonly string[]; + testFiles?: string[]; + verbose?: boolean; } const findTypingsFile = async (pkg: any, options: Options) => { @@ -74,6 +75,7 @@ const findTestFiles = async (typingsFilePath: string, options: Options & {config * @returns A promise which resolves the diagnostics of the type definition. */ export default async (options: Options = {cwd: process.cwd()}) => { + const isVerbose = options.verbose ? true : false; const pkgResult = await readPkgUp({cwd: options.cwd}); if (!pkgResult) { @@ -96,7 +98,9 @@ export default async (options: Options = {cwd: process.cwd()}) => { pkg, typingsFile, testFiles, - config + typingsFile, + cwd: options.cwd, + verbose: isVerbose }; return [ diff --git a/source/lib/interfaces.ts b/source/lib/interfaces.ts index bb289339..2341ea21 100644 --- a/source/lib/interfaces.ts +++ b/source/lib/interfaces.ts @@ -17,6 +17,7 @@ export interface Context { typingsFile: string; testFiles: string[]; config: Config; + verbose: boolean; } export enum DiagnosticCode { diff --git a/source/lib/rules/index.ts b/source/lib/rules/index.ts index a4229e0a..b2464e05 100644 --- a/source/lib/rules/index.ts +++ b/source/lib/rules/index.ts @@ -16,10 +16,12 @@ const rules = new Set([ * @param context - The context object. * @returns List of diagnostics */ -export default (context: Context) => { +export default (context: Context): Diagnostic[] => { + let numTests: number = 0; const diagnostics: Diagnostic[] = []; for (const rule of rules) { + numTests = numTests + 1; diagnostics.push(...rule(context)); } From 796568e6efefa9369e1adc5b72fdf4b0e21833a7 Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Thu, 9 Jul 2020 13:26:03 +0530 Subject: [PATCH 34/41] [WIP] Verbose mode --- source/lib/compiler.ts | 6 +++--- source/lib/index.ts | 23 +++++++++++++++++++---- source/lib/interfaces.ts | 5 +++++ source/lib/rules/index.ts | 6 +++--- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/source/lib/compiler.ts b/source/lib/compiler.ts index f3388a83..a6528318 100644 --- a/source/lib/compiler.ts +++ b/source/lib/compiler.ts @@ -6,7 +6,7 @@ import { } from '../../libraries/typescript'; import {TypeChecker} from './entities/typescript'; import {extractAssertions, parseErrorAssertionToLocation} from './parser'; -import {Diagnostic, DiagnosticCode, Context, Location} from './interfaces'; +import {Diagnostic, DiagnosticCode, Context, Location, ExtendedDiagnostic} from './interfaces'; import {handle} from './assertions'; // List of diagnostic codes that should be ignored in general @@ -63,7 +63,7 @@ const ignoreDiagnostic = (diagnostic: TSDiagnostic, expectedErrors: Map { +export const getDiagnostics = (context: Context): ExtendedDiagnostic => { let numTests: number = 0; const diagnostics: Diagnostic[] = []; @@ -107,5 +107,5 @@ export const getDiagnostics = (context: Context): Diagnostic[] => { }); } - return diagnostics; + return {numTests, diagnostics}; }; diff --git a/source/lib/index.ts b/source/lib/index.ts index e55de744..ccf89d1d 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -103,8 +103,23 @@ export default async (options: Options = {cwd: process.cwd()}) => { verbose: isVerbose }; - return [ - ...getCustomDiagnostics(context), - ...getTSDiagnostics(context) - ]; + const tsDiagnostics = getTSDiagnostics(context); + const customDiagnostics = getCustomDiagnostics(context); + + if (!isVerbose) { + return [ + ...customDiagnostics.diagnostics, + ...tsDiagnostics.diagnostics + ]; + } + + const numTests = tsDiagnostics.numTests + customDiagnostics.numTests; + + return { + numTests, + diagnostics: [ + ...customDiagnostics.diagnostics, + ...tsDiagnostics.diagnostics + ] + }; }; diff --git a/source/lib/interfaces.ts b/source/lib/interfaces.ts index 2341ea21..6003adb2 100644 --- a/source/lib/interfaces.ts +++ b/source/lib/interfaces.ts @@ -39,6 +39,11 @@ export interface Diagnostic { column?: number; } +export interface ExtendedDiagnostic { + numTests: number; + diagnostics: Diagnostic[]; +} + export interface Location { fileName: string; start: number; diff --git a/source/lib/rules/index.ts b/source/lib/rules/index.ts index b2464e05..46c5a9f2 100644 --- a/source/lib/rules/index.ts +++ b/source/lib/rules/index.ts @@ -1,6 +1,6 @@ import filesProperty from './files-property'; import typesProperty from './types-property'; -import {Diagnostic, Context} from '../interfaces'; +import {Diagnostic, Context, ExtendedDiagnostic} from '../interfaces'; type RuleFunction = (context: Context) => Diagnostic[]; @@ -16,7 +16,7 @@ const rules = new Set([ * @param context - The context object. * @returns List of diagnostics */ -export default (context: Context): Diagnostic[] => { +export default (context: Context): ExtendedDiagnostic => { let numTests: number = 0; const diagnostics: Diagnostic[] = []; @@ -25,5 +25,5 @@ export default (context: Context): Diagnostic[] => { diagnostics.push(...rule(context)); } - return diagnostics; + return {numTests, diagnostics}; }; From 015277ef8b019bb3d9c7049160ddbaa64750de37 Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Wed, 22 Jul 2020 16:00:47 +0530 Subject: [PATCH 35/41] Remove verbose mode option --- source/lib/index.ts | 13 +------------ source/lib/interfaces.ts | 1 - 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/source/lib/index.ts b/source/lib/index.ts index ccf89d1d..1b27385e 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -11,7 +11,6 @@ export interface Options { cwd: string; typingsFile?: string; testFiles?: string[]; - verbose?: boolean; } const findTypingsFile = async (pkg: any, options: Options) => { @@ -75,7 +74,6 @@ const findTestFiles = async (typingsFilePath: string, options: Options & {config * @returns A promise which resolves the diagnostics of the type definition. */ export default async (options: Options = {cwd: process.cwd()}) => { - const isVerbose = options.verbose ? true : false; const pkgResult = await readPkgUp({cwd: options.cwd}); if (!pkgResult) { @@ -99,20 +97,11 @@ export default async (options: Options = {cwd: process.cwd()}) => { typingsFile, testFiles, typingsFile, - cwd: options.cwd, - verbose: isVerbose + cwd: options.cwd }; const tsDiagnostics = getTSDiagnostics(context); const customDiagnostics = getCustomDiagnostics(context); - - if (!isVerbose) { - return [ - ...customDiagnostics.diagnostics, - ...tsDiagnostics.diagnostics - ]; - } - const numTests = tsDiagnostics.numTests + customDiagnostics.numTests; return { diff --git a/source/lib/interfaces.ts b/source/lib/interfaces.ts index 6003adb2..d7cfac94 100644 --- a/source/lib/interfaces.ts +++ b/source/lib/interfaces.ts @@ -17,7 +17,6 @@ export interface Context { typingsFile: string; testFiles: string[]; config: Config; - verbose: boolean; } export enum DiagnosticCode { From 67662cd862b8fa26a2d8a6eac1e444524554dcfb Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Wed, 22 Jul 2020 17:14:00 +0530 Subject: [PATCH 36/41] Support for ExtendedDiagnostics --- source/cli.ts | 5 +++-- source/lib/compiler.ts | 2 +- source/lib/formatter.ts | 5 +++-- source/lib/rules/index.ts | 2 +- source/test/fixtures/utils.ts | 7 ++++--- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/source/cli.ts b/source/cli.ts index 6712b892..e9d6cc6f 100644 --- a/source/cli.ts +++ b/source/cli.ts @@ -23,10 +23,11 @@ const cli = meow(` try { const options = cli.input.length > 0 ? {cwd: cli.input[0]} : undefined; - const diagnostics = await tsd(options); + const extendedDiagnostics = await tsd(options); + const diagnostics = extendedDiagnostics.diagnostics; if (diagnostics.length > 0) { - throw new Error(formatter(diagnostics)); + throw new Error(formatter(extendedDiagnostics)); } } catch (error) { console.error(error.message); diff --git a/source/lib/compiler.ts b/source/lib/compiler.ts index a6528318..d71440e9 100644 --- a/source/lib/compiler.ts +++ b/source/lib/compiler.ts @@ -64,7 +64,7 @@ const ignoreDiagnostic = (diagnostic: TSDiagnostic, expectedErrors: Map { - let numTests: number = 0; + let numTests = 0; const diagnostics: Diagnostic[] = []; const program = createProgram(context.testFiles, context.config.compilerOptions); diff --git a/source/lib/formatter.ts b/source/lib/formatter.ts index 3fdb23eb..9bafb36f 100644 --- a/source/lib/formatter.ts +++ b/source/lib/formatter.ts @@ -1,5 +1,5 @@ import * as formatter from 'eslint-formatter-pretty'; -import {Diagnostic} from './interfaces'; +import {ExtendedDiagnostic} from './interfaces'; /** * Format the TypeScript diagnostics to a human readable output. @@ -7,7 +7,8 @@ import {Diagnostic} from './interfaces'; * @param diagnostics - List of TypeScript diagnostics. * @returns Beautiful diagnostics output */ -export default (diagnostics: Diagnostic[]) => { +export default (extendedDiagnostics: ExtendedDiagnostic) => { + const diagnostics = extendedDiagnostics.diagnostics; const fileMap = new Map(); for (const diagnostic of diagnostics) { diff --git a/source/lib/rules/index.ts b/source/lib/rules/index.ts index 46c5a9f2..ab352a54 100644 --- a/source/lib/rules/index.ts +++ b/source/lib/rules/index.ts @@ -17,7 +17,7 @@ const rules = new Set([ * @returns List of diagnostics */ export default (context: Context): ExtendedDiagnostic => { - let numTests: number = 0; + let numTests = 0; const diagnostics: Diagnostic[] = []; for (const rule of rules) { diff --git a/source/test/fixtures/utils.ts b/source/test/fixtures/utils.ts index cab9e6e7..12e2e168 100644 --- a/source/test/fixtures/utils.ts +++ b/source/test/fixtures/utils.ts @@ -1,5 +1,5 @@ import {ExecutionContext} from 'ava'; -import {Diagnostic} from '../../lib/interfaces'; +import {ExtendedDiagnostic} from '../../lib/interfaces'; type Expectation = [number, number, 'error' | 'warning', string, (string | RegExp)?]; @@ -7,10 +7,11 @@ type Expectation = [number, number, 'error' | 'warning', string, (string | RegEx * Verify a list of diagnostics. * * @param t - The AVA execution context. - * @param diagnostics - List of diagnostics to verify. + * @param extendedDiagnostics - Object containing numTests and list of diagnostics to verify * @param expectations - Expected diagnostics. */ -export const verify = (t: ExecutionContext, diagnostics: Diagnostic[], expectations: Expectation[]) => { +export const verify = (t: ExecutionContext, extendedDiagnostics: ExtendedDiagnostic, expectations: Expectation[]) => { + const diagnostics = extendedDiagnostics.diagnostics; t.true(diagnostics.length === expectations.length); for (const [index, diagnostic] of diagnostics.entries()) { From d8e73015e869f012297303d89ac9dd9f00c0907a Mon Sep 17 00:00:00 2001 From: xXAlphaManXx Date: Wed, 29 Jul 2020 13:34:47 +0530 Subject: [PATCH 37/41] Remove counting custom rules as a test --- source/lib/index.ts | 4 ++-- source/lib/rules/index.ts | 8 +++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/source/lib/index.ts b/source/lib/index.ts index 1b27385e..7e002596 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -102,12 +102,12 @@ export default async (options: Options = {cwd: process.cwd()}) => { const tsDiagnostics = getTSDiagnostics(context); const customDiagnostics = getCustomDiagnostics(context); - const numTests = tsDiagnostics.numTests + customDiagnostics.numTests; + const numTests = tsDiagnostics.numTests; return { numTests, diagnostics: [ - ...customDiagnostics.diagnostics, + ...customDiagnostics, ...tsDiagnostics.diagnostics ] }; diff --git a/source/lib/rules/index.ts b/source/lib/rules/index.ts index ab352a54..d4f71634 100644 --- a/source/lib/rules/index.ts +++ b/source/lib/rules/index.ts @@ -1,6 +1,6 @@ import filesProperty from './files-property'; import typesProperty from './types-property'; -import {Diagnostic, Context, ExtendedDiagnostic} from '../interfaces'; +import {Diagnostic, Context} from '../interfaces'; type RuleFunction = (context: Context) => Diagnostic[]; @@ -16,14 +16,12 @@ const rules = new Set([ * @param context - The context object. * @returns List of diagnostics */ -export default (context: Context): ExtendedDiagnostic => { - let numTests = 0; +export default (context: Context): Diagnostic[] => { const diagnostics: Diagnostic[] = []; for (const rule of rules) { - numTests = numTests + 1; diagnostics.push(...rule(context)); } - return {numTests, diagnostics}; + return diagnostics; }; From 6e7c49c79f45511c56730ddcd9c60b960a7d9336 Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Thu, 6 Aug 2020 21:02:05 +0530 Subject: [PATCH 38/41] Provide docs for new changes --- readme.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index 5e1dcf9a..f43817fa 100644 --- a/readme.md +++ b/readme.md @@ -191,10 +191,13 @@ You can use the programmatic API to retrieve the diagnostics and do something wi import tsd from 'tsd'; (async () => { - const diagnostics = await tsd(); + const diagnoser = await tsd(); - console.log(diagnostics.length); - //=> 2 + // Returns the number of tests evaludated. + console.log(diagnoser.numTests) + + // Returns the diagnostics if any or just an empty array + console.log(diagnoser.diagnostics); })(); ``` From 101eb4fa0f930f9d85aff9112ab2ab84a570859b Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Sun, 4 Oct 2020 17:19:23 +0530 Subject: [PATCH 39/41] Grammar and typo fix --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index f43817fa..da6c9d9d 100644 --- a/readme.md +++ b/readme.md @@ -193,10 +193,10 @@ import tsd from 'tsd'; (async () => { const diagnoser = await tsd(); - // Returns the number of tests evaludated. + // Returns the number of tests evaluated. console.log(diagnoser.numTests) - // Returns the diagnostics if any or just an empty array + // Returns the diagnostics if any or just an empty array. console.log(diagnoser.diagnostics); })(); ``` From de2b54904ecd1f0aa1e71d10d488f78e3a08bfa1 Mon Sep 17 00:00:00 2001 From: SaurabhAgarwala Date: Mon, 10 Aug 2020 23:10:21 +0530 Subject: [PATCH 40/41] Add unit test to check numTests parameter --- source/test/test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/source/test/test.ts b/source/test/test.ts index 320aa507..b4cc9789 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -253,3 +253,9 @@ test('fails if typings file is not found in the specified path', async t => { t.is(error.message, 'The type definition `unknown.d.ts` does not exist. Create one and try again.'); }); + +test('checking numTests', async t => { + const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/failure')}); + + t.is(diagnostics.numTests, 2); +}); From f6e579a42e8bcc57d84f983666fbb3a0436a6b33 Mon Sep 17 00:00:00 2001 From: Karan Sanjeev Date: Thu, 3 Dec 2020 14:54:23 +0530 Subject: [PATCH 41/41] Refactor numTests into testCount --- readme.md | 2 +- source/lib/compiler.ts | 6 +++--- source/lib/index.ts | 4 ++-- source/lib/interfaces.ts | 2 +- source/test/fixtures/utils.ts | 2 +- source/test/test.ts | 4 ++-- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/readme.md b/readme.md index da6c9d9d..55686df0 100644 --- a/readme.md +++ b/readme.md @@ -194,7 +194,7 @@ import tsd from 'tsd'; const diagnoser = await tsd(); // Returns the number of tests evaluated. - console.log(diagnoser.numTests) + console.log(diagnoser.testCount) // Returns the diagnostics if any or just an empty array. console.log(diagnoser.diagnostics); diff --git a/source/lib/compiler.ts b/source/lib/compiler.ts index d71440e9..8fada40b 100644 --- a/source/lib/compiler.ts +++ b/source/lib/compiler.ts @@ -64,7 +64,7 @@ const ignoreDiagnostic = (diagnostic: TSDiagnostic, expectedErrors: Map { - let numTests = 0; + let testCount = 0; const diagnostics: Diagnostic[] = []; const program = createProgram(context.testFiles, context.config.compilerOptions); @@ -76,7 +76,7 @@ export const getDiagnostics = (context: Context): ExtendedDiagnostic => { const assertions = extractAssertions(program); for (const assertion of assertions) { - numTests = numTests + assertion[1].size; + testCount = testCount + assertion[1].size; } diagnostics.push(...handle(program.getTypeChecker() as TypeChecker, assertions)); @@ -107,5 +107,5 @@ export const getDiagnostics = (context: Context): ExtendedDiagnostic => { }); } - return {numTests, diagnostics}; + return {testCount, diagnostics}; }; diff --git a/source/lib/index.ts b/source/lib/index.ts index 7e002596..cac66d90 100644 --- a/source/lib/index.ts +++ b/source/lib/index.ts @@ -102,10 +102,10 @@ export default async (options: Options = {cwd: process.cwd()}) => { const tsDiagnostics = getTSDiagnostics(context); const customDiagnostics = getCustomDiagnostics(context); - const numTests = tsDiagnostics.numTests; + const testCount = tsDiagnostics.testCount; return { - numTests, + testCount, diagnostics: [ ...customDiagnostics, ...tsDiagnostics.diagnostics diff --git a/source/lib/interfaces.ts b/source/lib/interfaces.ts index d7cfac94..74344d43 100644 --- a/source/lib/interfaces.ts +++ b/source/lib/interfaces.ts @@ -39,7 +39,7 @@ export interface Diagnostic { } export interface ExtendedDiagnostic { - numTests: number; + testCount: number; diagnostics: Diagnostic[]; } diff --git a/source/test/fixtures/utils.ts b/source/test/fixtures/utils.ts index 12e2e168..d915f59a 100644 --- a/source/test/fixtures/utils.ts +++ b/source/test/fixtures/utils.ts @@ -7,7 +7,7 @@ type Expectation = [number, number, 'error' | 'warning', string, (string | RegEx * Verify a list of diagnostics. * * @param t - The AVA execution context. - * @param extendedDiagnostics - Object containing numTests and list of diagnostics to verify + * @param extendedDiagnostics - Object containing testCount and list of diagnostics to verify * @param expectations - Expected diagnostics. */ export const verify = (t: ExecutionContext, extendedDiagnostics: ExtendedDiagnostic, expectations: Expectation[]) => { diff --git a/source/test/test.ts b/source/test/test.ts index b4cc9789..e4501150 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -254,8 +254,8 @@ test('fails if typings file is not found in the specified path', async t => { t.is(error.message, 'The type definition `unknown.d.ts` does not exist. Create one and try again.'); }); -test('checking numTests', async t => { +test('checking testCount', async t => { const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/failure')}); - t.is(diagnostics.numTests, 2); + t.is(diagnostics.testCount, 2); });