Home

Awesome

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

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

/**
 * @remarks
 * unit,acceptance,whatever
 */
test('get correct background color based on text color', () => {
	expect(true).toBe(true);
});
const enum MyEnum {
	Key = 'Value',
}

/**
 * @see {@link MyEnum.Key}
 */
test('get correct background color based on text color', () => {
	expect(true).toBe(true);
});
/**
 * @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.