Git Product home page Git Product logo

tsdoc-test-reporter / reporter Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 1.11 MB

TSDoc Test Reporter is a test reporter that attaches TSDoc comments to your test results. It enables you to attach metadata to your unit tests in the form of comments.

Home Page: https://tsdoc-test-reporter.github.io/reporter/

License: MIT License

Shell 0.05% JavaScript 2.23% TypeScript 97.72%
jest jest-reporter test-automation test-reporting testing-tools tsdoc typescript vitest vitest-reporter jsdoc

reporter's Introduction

social_preview

@tsdoc-test-reporter

@tsdoc-test-reporter is a test reporter that attaches TSDoc comments to your test results. It enables you to attach metadata to your unit tests in the form of comments.

Output

Example output from the reporter

Features

  • Supports: Jest and Vitest
  • Parses JSDoc and extended TSDoc tags
  • Outputs: JSON or HTML
  • Extensive configuration of HTML report (See UIOptions)
  • Displays error details
  • Supports using custom Tag Definitions
  • Works with regular JavaScript files as well
  • Works as a regular HTML/JSON reporter if you don't have any JSDoc
  • See limitations on a list of what are some known limitations

Installing

Jest

npm install @tsdoc-test-reporter/jest

Vitest

npm install @tsdoc-test-reporter/vitest

Usage

Basic

Jest

  1. Add reporter to your jest config (jest.config.js)
/** @type {import('@jest/types').Config.InitialOptions} */
module.exports = {
	reporters: ['default', '@tsdoc-test-reporter/jest'],
};
  1. Add TSDoc comments to a test of your choice:
/**
 * @remarks
 * WCAG Criteria
 */
test('get correct background color based on text color', () => {
	expect(true).toBe(true);
});
  1. Run tests
  2. Open the newly generated file tsdoc-test-reporter-report.html in the browser of your choice

Vitest

  1. Add reporter to config (vite.config.ts)
/// <reference types="vitest" />
import { defineConfig } from 'vite';

export default defineConfig({
	test: {
		reporters: ['@tsdoc-test-reporter/vitest'],
	},
});
  1. Add TSDoc comments to a test of your choice:
/**
 * @remarks
 * WCAG Criteria
 */
test('get correct background color based on text color', () => {
	expect(true).toBe(true);
});
  1. Run tests (vitest run)
  2. Open the newly generated file tsdoc-test-reporter-report.html in the browser of your choice

With config

Jest

See the documentation for the config for full docs of possible options

/** @type {import('@tsdoc-test-reporter/jest').TsDocTestReporterConfig} */
const options = {
	outputFileName: 'reports/tsdoc-report',
	uiOptions: {
		htmlTitle: 'Title of HTML Page',
	},
};

/** @type {import('@jest/types').Config.InitialOptions} */
module.exports = {
	reporters: ['default', ['@tsdoc-test-reporter/jest', options]],
};

Vitest

See the documentation for the config for full docs of possible options

  1. Create a file for the custom reporter in your project. Vitest does not allow passing config directly when specifying reporters so we need to call the reporter from our own custom reporter.
// reporter.ts
import TSDocTestReporter from '@tsdoc-test-reporter/vitest';
import { Reporter } from 'vitest/reporters';
import type { File, Vitest } from 'vitest';

export default class MyDefaultReporter extends Reporter {
	private reporter: TSDocTestReporter;
	constructor() {
		this.reporter = new TSDocTestReporter({
			outputFileName: 'reports/tsdoc-report',
			uiOptions: {
				htmlTitle: 'Title of HTML Page',
			},
		});
	}
	onInit(ctx: Vitest) {
		this.reporter.onInit(ctx);
	}
	onFinished(files: File[]) {
		this.reporter.onFinished(files);
	}
}
  1. Link reporter:
/// <reference types="vitest" />
import { defineConfig } from 'vite';

export default defineConfig({
	test: {
		reporters: ['./reporter.ts'],
	},
});

With Custom User Supplied tags

Jest

/** @type {import('@tsdoc-test-reporter/jest').TsDocTestReporterConfig} */
const options = {
	customTags: [
		{
			tagName: '@customModifierTag',
			syntaxKind: 2,
		},
	],
};

/** @type {import('@jest/types').Config.InitialOptions} */
module.exports = {
	reporters: ['default', ['@tsdoc-test-reporter/jest', options]],
};

Vitest

// reporter.ts
import TSDocTestReporter from '@tsdoc-test-reporter/vitest';
import { Reporter } from 'vitest/reporters';
import type { File, Vitest } from 'vitest';

export default class MyDefaultReporter extends Reporter {
	private reporter: TSDocTestReporter;

	constructor() {
		this.reporter = new TSDocTestReporter({
			customTags: [
				{
					tagName: '@customModifierTag',
					syntaxKind: 2,
				},
			],
		});
	}
	onInit(ctx: Vitest) {
		this.reporter.onInit(ctx);
	}
	onFinished(files: File[]) {
		this.reporter.onFinished(files);
	}
}

Limitations

  • Does not parse more than regular text in block comments as of writing. This is being worked on. You can create multiple tags when using a block tag. See example below. If you want a custom tagseparator that is not , you can supply it as an option.
/**
 * @remarks
 * unit,acceptance,whatever
 */
test('get correct background color based on text color', () => {
	expect(true).toBe(true);
});
  • Has limited support of inline tags. As of writing the supported case is using @see {@link variableName}. If the linked reference is a variable it will be resolved to a value if it is in scope of the source file (in the source file or imported by the source file). This is limited to string literals and object properties that are string literals (or enums). The example below works, and works if the enum is imported as a named import.
const enum MyEnum {
	Key = 'Value',
}

/**
 * @see {@link MyEnum.Key}
 */
test('get correct background color based on text color', () => {
	expect(true).toBe(true);
});
  • Can not parse test titles that has parameters. If you are using test.each or similar where you are using placeholders in the test title, this test will not be able to match the JSDoc to the test assertion. You will have to wrap that each block with a describe and add a JSDoc to the describe block. If you are not using parameters in the title, test.each will work.
/**
 * @remarks
 * unit,acceptance
 */
test.each([{ name: 'value' }])('this will fail: $name', () => {
	expect(true).toBe(true);
});

How to contribute

See CONTRIBUTING.md on how to contribute and how to setup the project locally.

reporter's People

Contributors

jesperorb avatar

Stargazers

 avatar  avatar

Watchers

 avatar

reporter's Issues

Display error details for failing tests

What needs to be done:

  • Create a common error model in core that each reporter can transform their errors into
  • Display error output in details element on failed test

Notes

  • Ansi converter for stack trace is in place but may need specific handling if error output is from react-testing-library or similar where stacktrace includes html tags that may break the output.

Add build info link

Add option to display link to build so that the user can go to the build on the CI that has generated the report.

Add support for parsing test titles with parameters (such as test.each)

When using parameters in test.each blocks, such as "validate: $name", the lib cannot parse this test title as the resolved name being printed to the user. The reporters main way of connecting tags to test blocks is via the test title. But when the test title is dynamic the matching fails.

Find a way to match a resolved test title to a parameterized test title
validate: $name -> validate: firstName.

This is just one example: full list of how titles can be formatted can be found here:
https://nodejs.org/api/util.html#utilformatformat-args

Parse block content of TSDoc tags as markdown/html/more complex structure

One of the main advantages TSDoc tags have over regular JSDoc tags is that the parser can parse and handle markdown in block tag content, it expects the content to be markdown.

The current parsing in the reporter does not take advantage of this and only outputs raw text.

What needs to be done:

  • Create model for tag content that allows more complex output
  • Parse tag content for TSDoc tags based on this model

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.