Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report the number of evaluated tests alongside with diagnostics #127

Closed
wants to merge 45 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
0122488
Remove VSCode config folder and add source code generation in TS build
smartclash Jun 24, 2020
e706b63
Allow passing of path to type definiton file manually
smartclash Jun 24, 2020
43ac091
Update README.md
smartclash Jun 24, 2020
1282ce6
Refactor code to check definition file existence if specified manually
smartclash Jun 24, 2020
8185162
Fix the checking of type file existence
smartclash Jun 24, 2020
35063ce
Use expect error instead of ignoring it
smartclash Jun 24, 2020
4d23908
Allow specifing test files manually
smartclash Jun 25, 2020
c7e427b
Allow passing relative path of typing def file
smartclash Jul 2, 2020
4cf8cb4
Allow passing relative path to test files
smartclash Jul 2, 2020
1bf9d15
Counts number of tests. Still needs to pass it.
smartclash Jul 8, 2020
544c401
[WIP] Verbose mode
smartclash Jul 9, 2020
039d8e9
Remove verbose mode option
smartclash Jul 22, 2020
8a9c90e
Support for ExtendedDiagnostics
smartclash Jul 22, 2020
ea3961a
Make changes according to suggestions
smartclash Jul 23, 2020
2f7e26a
Merge branch 'relative-paths' into feature/verbose-reporting
smartclash Jul 24, 2020
134b88c
Fixed README alignment
smartclash Jul 24, 2020
a447c7d
Remove counting custom rules as a test
smartclash Jul 29, 2020
da0741a
Provide docs for new changes
smartclash Aug 6, 2020
a477f69
Merge branch 'relative-paths' into feature/verbose-reporting
smartclash Aug 6, 2020
64409b3
Add tests for new typingsFile option
smartclash Aug 7, 2020
6d24bca
Fixed tests file resolving issue when typingsFile option is set
smartclash Aug 7, 2020
1d391ae
Add tests for testFiles option
smartclash Aug 7, 2020
d925296
Add tests to check cases where typings file is not found in specified…
smartclash Aug 7, 2020
414e242
Merge branch 'relative-paths' into feature/verbose-reporting
smartclash Aug 9, 2020
59670d6
Use globby for testFiles option
smartclash Aug 9, 2020
eb4a6ea
Merge branch 'relative-paths' into feature/verbose-reporting
smartclash Aug 9, 2020
9eb0912
Add unit test to check numTests parameter
SaurabhAgarwala Aug 10, 2020
41e9e0c
Remove .vscode entry in gitignore
smartclash Oct 4, 2020
02927f0
Update docs
smartclash Oct 4, 2020
285c6cb
Make testFiles property readonly
smartclash Oct 4, 2020
59c5f4b
Fix grammar
smartclash Oct 4, 2020
d27c0df
Merge branch 'relative-paths' into feature/verbose-reporting
smartclash Oct 4, 2020
641e5cd
Grammar and typo fix
smartclash Oct 4, 2020
caf42f2
Merge branch 'feature/verbose-reporting' of github.com:MLH-Fellowship…
smartclash Oct 4, 2020
9ece40e
Grammar in README
smartclash Oct 5, 2020
7b8d323
Merge branch 'relative-paths' into feature/verbose-reporting
smartclash Oct 5, 2020
44c066e
Update readme.md
smartclash Dec 3, 2020
c549587
Improve readme
smartclash Dec 3, 2020
cb3da21
Merge branch 'relative-paths' into feature/verbose-reporting
smartclash Dec 3, 2020
9424f46
Refactor numTests into testCount
smartclash Dec 3, 2020
90c0bf4
rebase and clean up
mrazauskas Oct 13, 2021
15915f4
simplify cli logic
mrazauskas Oct 13, 2021
e3fd1cc
fix lost typings
mrazauskas Oct 13, 2021
312d8b8
factor out `countAssertions`
mrazauskas Oct 13, 2021
3570e0d
move test
mrazauskas Oct 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,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 {diagnostics, testCount} = await tsd();

console.log(diagnostics.length);
//=> 2
// The list of diagnostics if any or just an empty array.
console.log(diagnostics);

// The number of tests evaluated.
console.log(testCount)
})();
```

Expand Down
2 changes: 1 addition & 1 deletion source/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const cli = meow(`
try {
const options = cli.input.length > 0 ? {cwd: cli.input[0]} : undefined;

const diagnostics = await tsd(options);
const {diagnostics} = await tsd(options);

if (diagnostics.length > 0) {
throw new Error(formatter(diagnostics));
Expand Down
24 changes: 20 additions & 4 deletions source/lib/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {
flattenDiagnosticMessageText,
createProgram,
CallExpression,
Diagnostic as TSDiagnostic
} from '@tsd/typescript';
import {ExpectedError, extractAssertions, parseErrorAssertionToLocation} from './parser';
import {Diagnostic, DiagnosticCode, Context, Location} from './interfaces';
import {handle} from './assertions';
import {Context, Diagnostic, DiagnosticCode, ExtendedDiagnostic, Location} from './interfaces';
import {Assertion, handle} from './assertions';

// List of diagnostic codes that should be ignored in general
const ignoredDiagnostics = new Set<number>([
Expand Down Expand Up @@ -78,13 +79,27 @@ const ignoreDiagnostic = (
return 'preserve';
};

/**
* Count all assertions in the given map.
*
* @param assertions - The assertions map to count.
* @returns Count of assertions.
*/
const countAssertions = (assertions: Map<Assertion, Set<CallExpression>>) => {
let count = 0;
assertions.forEach(nodes => {
count += nodes.size;
});
return count;
};

/**
* Get a list of TypeScript diagnostics within the current context.
*
* @param context - The context object.
* @returns List of diagnostics
*/
export const getDiagnostics = (context: Context): Diagnostic[] => {
export const getDiagnostics = (context: Context): ExtendedDiagnostic => {
const diagnostics: Diagnostic[] = [];

const program = createProgram(context.testFiles, context.config.compilerOptions);
Expand All @@ -94,6 +109,7 @@ export const getDiagnostics = (context: Context): Diagnostic[] => {
.concat(program.getSyntacticDiagnostics());

const assertions = extractAssertions(program);
const testCount = countAssertions(assertions);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be named assertionCount as well.


diagnostics.push(...handle(program.getTypeChecker(), assertions));

Expand Down Expand Up @@ -142,5 +158,5 @@ export const getDiagnostics = (context: Context): Diagnostic[] => {
});
}

return diagnostics;
return {diagnostics, testCount};
};
18 changes: 12 additions & 6 deletions source/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import globby from 'globby';
import {getDiagnostics as getTSDiagnostics} from './compiler';
import loadConfig from './config';
import getCustomDiagnostics from './rules';
import {Context, Config, Diagnostic, PackageJsonWithTsdConfig} from './interfaces';
import {Config, Context, ExtendedDiagnostic, PackageJsonWithTsdConfig} from './interfaces';

export interface Options {
cwd: string;
Expand Down Expand Up @@ -78,7 +78,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()}): Promise<Diagnostic[]> => {
export default async (options: Options = {cwd: process.cwd()}): Promise<ExtendedDiagnostic> => {
const pkgResult = await readPkgUp({cwd: options.cwd});

if (!pkgResult) {
Expand All @@ -104,8 +104,14 @@ export default async (options: Options = {cwd: process.cwd()}): Promise<Diagnost
config
};

return [
...getCustomDiagnostics(context),
...getTSDiagnostics(context)
];
const {diagnostics: tsDiagnostics, testCount} = getTSDiagnostics(context);
const customDiagnostics = getCustomDiagnostics(context);

return {
testCount,
diagnostics: [
...customDiagnostics,
...tsDiagnostics
]
};
};
5 changes: 5 additions & 0 deletions source/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export interface Diagnostic {
column?: number;
}

export interface ExtendedDiagnostic {
testCount: number;
diagnostics: Diagnostic[];
}

export interface Location {
fileName: string;
start: number;
Expand Down
2 changes: 1 addition & 1 deletion source/lib/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const rules = new Set<RuleFunction>([
* @param context - The context object.
* @returns List of diagnostics
*/
export default (context: Context) => {
export default (context: Context): Diagnostic[] => {
const diagnostics: Diagnostic[] = [];

for (const rule of rules) {
Expand Down
14 changes: 9 additions & 5 deletions source/test/fixtures/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import {ExecutionContext} from 'ava';
import {Diagnostic} from '../../lib/interfaces';
import {ExtendedDiagnostic} from '../../lib/interfaces';

type Expectation = [
line: number,
Expand All @@ -21,10 +21,14 @@ type ExpectationWithFileName = [
* Verify a list of diagnostics.
*
* @param t - The AVA execution context.
* @param diagnostics - List of diagnostics to verify.
* @param extendedDiagnostics - Object containing list of TypeScript diagnostics and test count.
* @param expectations - Expected diagnostics.
*/
export const verify = (t: ExecutionContext, diagnostics: Diagnostic[], expectations: Expectation[]) => {
export const verify = (
t: ExecutionContext,
{diagnostics}: ExtendedDiagnostic,
expectations: Expectation[]
) => {
const diagnosticObjs = diagnostics.map(({line, column, severity, message}) => ({
line,
column,
Expand All @@ -47,13 +51,13 @@ export const verify = (t: ExecutionContext, diagnostics: Diagnostic[], expectati
*
* @param t - The AVA execution context.
* @param cwd - The working directory as passed to `tsd`.
* @param diagnostics - List of diagnostics to verify.
* @param extendedDiagnostics - Object containing list of TypeScript diagnostics and test count.
* @param expectations - Expected diagnostics.
*/
export const verifyWithFileName = (
t: ExecutionContext,
cwd: string,
diagnostics: Diagnostic[],
{diagnostics}: ExtendedDiagnostic,
expectations: ExpectationWithFileName[]
) => {
const diagnosticObjs = diagnostics.map(({line, column, severity, message, fileName}) => ({
Expand Down
8 changes: 5 additions & 3 deletions source/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ test('throw if no test is found', async t => {
await t.throwsAsync(tsd({cwd: path.join(__dirname, 'fixtures/no-test')}), {message: 'The test file `index.test-d.ts` or `index.test-d.tsx` does not exist. Create one and try again.'});
});

test('return diagnostics', async t => {
test('return extended diagnostics object', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/failure')});

t.is(diagnostics.testCount, 2, 'Received test count that is different from expected.');

verify(t, diagnostics, [
[5, 19, 'error', 'Argument of type \'number\' is not assignable to parameter of type \'string\'.']
]);
Expand Down Expand Up @@ -368,7 +370,7 @@ test('includes extended config files along with found ones', async t => {
});

test('errors in libs from node_modules are not reported', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/exclude-node-modules')});
const {diagnostics, testCount} = await tsd({cwd: path.join(__dirname, 'fixtures/exclude-node-modules')});

const [nodeModuleDiagnostics, testFileDiagnostics, otherDiagnostics] = diagnostics.reduce<Diagnostic[][]>(
([nodeModuleDiags, testFileDiags, otherDiags], diagnostic) => {
Expand Down Expand Up @@ -407,7 +409,7 @@ test('errors in libs from node_modules are not reported', async t => {
);
});

verify(t, testFileDiagnostics, [
verify(t, {diagnostics: testFileDiagnostics, testCount}, [
[3, 18, 'error', 'Cannot find name \'Bar\'.']
]);
});
Expand Down