自定义报告器


\Custom reporters

--test-reporter 可用于指定自定义报告程序的路径。自定义报告器是一个导出 stream.compose 接受的值的模块。报告者应该转换 <TestsStream> 触发的事件

\--test-reporter can be used to specify a path to custom reporter. A custom reporter is a module that exports a value accepted by stream.compose. Reporters should transform events emitted by a <TestsStream>

使用 <stream.Transform> 的自定义报告器示例:

\Example of a custom reporter using <stream.Transform>:

import { Transform } from 'node:stream';
const customReporter = new Transform({
 writableObjectMode: true,
 transform(event, encoding, callback) {
 switch (event.type) {
 case 'test:dequeue':
 callback(null, `test ${event.data.name} dequeued`);
 break;
 case 'test:enqueue':
 callback(null, `test ${event.data.name} enqueued`);
 break;
 case 'test:watch:drained':
 callback(null, 'test watch queue drained');
 break;
 case 'test:watch:restarted':
 callback(null, 'test watch restarted due to file change');
 break;
 case 'test:start':
 callback(null, `test ${event.data.name} started`);
 break;
 case 'test:pass':
 callback(null, `test ${event.data.name} passed`);
 break;
 case 'test:fail':
 callback(null, `test ${event.data.name} failed`);
 break;
 case 'test:plan':
 callback(null, 'test plan');
 break;
 case 'test:diagnostic':
 case 'test:stderr':
 case 'test:stdout':
 callback(null, event.data.message);
 break;
 case 'test:coverage': {
 const { totalLineCount } = event.data.summary.totals;
 callback(null, `total line count: ${totalLineCount}\n`);
 break;
 }
 }
 },
});
export default customReporter;const { Transform } = require('node:stream');
const customReporter = new Transform({
 writableObjectMode: true,
 transform(event, encoding, callback) {
 switch (event.type) {
 case 'test:dequeue':
 callback(null, `test ${event.data.name} dequeued`);
 break;
 case 'test:enqueue':
 callback(null, `test ${event.data.name} enqueued`);
 break;
 case 'test:watch:drained':
 callback(null, 'test watch queue drained');
 break;
 case 'test:watch:restarted':
 callback(null, 'test watch restarted due to file change');
 break;
 case 'test:start':
 callback(null, `test ${event.data.name} started`);
 break;
 case 'test:pass':
 callback(null, `test ${event.data.name} passed`);
 break;
 case 'test:fail':
 callback(null, `test ${event.data.name} failed`);
 break;
 case 'test:plan':
 callback(null, 'test plan');
 break;
 case 'test:diagnostic':
 case 'test:stderr':
 case 'test:stdout':
 callback(null, event.data.message);
 break;
 case 'test:coverage': {
 const { totalLineCount } = event.data.summary.totals;
 callback(null, `total line count: ${totalLineCount}\n`);
 break;
 }
 }
 },
});
module.exports = customReporter;

使用生成器函数的自定义报告器示例:

\Example of a custom reporter using a generator function:

export default async function * customReporter(source) {
 for await (const event of source) {
 switch (event.type) {
 case 'test:dequeue':
 yield `test ${event.data.name} dequeued\n`;
 break;
 case 'test:enqueue':
 yield `test ${event.data.name} enqueued\n`;
 break;
 case 'test:watch:drained':
 yield 'test watch queue drained\n';
 break;
 case 'test:watch:restarted':
 yield 'test watch restarted due to file change\n';
 break;
 case 'test:start':
 yield `test ${event.data.name} started\n`;
 break;
 case 'test:pass':
 yield `test ${event.data.name} passed\n`;
 break;
 case 'test:fail':
 yield `test ${event.data.name} failed\n`;
 break;
 case 'test:plan':
 yield 'test plan\n';
 break;
 case 'test:diagnostic':
 case 'test:stderr':
 case 'test:stdout':
 yield `${event.data.message}\n`;
 break;
 case 'test:coverage': {
 const { totalLineCount } = event.data.summary.totals;
 yield `total line count: ${totalLineCount}\n`;
 break;
 }
 }
 }
}module.exports = async function * customReporter(source) {
 for await (const event of source) {
 switch (event.type) {
 case 'test:dequeue':
 yield `test ${event.data.name} dequeued\n`;
 break;
 case 'test:enqueue':
 yield `test ${event.data.name} enqueued\n`;
 break;
 case 'test:watch:drained':
 yield 'test watch queue drained\n';
 break;
 case 'test:watch:restarted':
 yield 'test watch restarted due to file change\n';
 break;
 case 'test:start':
 yield `test ${event.data.name} started\n`;
 break;
 case 'test:pass':
 yield `test ${event.data.name} passed\n`;
 break;
 case 'test:fail':
 yield `test ${event.data.name} failed\n`;
 break;
 case 'test:plan':
 yield 'test plan\n';
 break;
 case 'test:diagnostic':
 case 'test:stderr':
 case 'test:stdout':
 yield `${event.data.message}\n`;
 break;
 case 'test:coverage': {
 const { totalLineCount } = event.data.summary.totals;
 yield `total line count: ${totalLineCount}\n`;
 break;
 }
 }
 }
};

提供给 --test-reporter 的值应该是一个类似于 JavaScript 代码中的 import() 中使用的字符串,或者提供给 --import 的值。

\The value provided to --test-reporter should be a string like one used in an import() in JavaScript code, or a value provided for --import.

AltStyle によって変換されたページ (->オリジナル) /