自定义报告器
🌐 Custom reporters
--test-reporter 可用于指定自定义报告器的路径。自定义报告器是一个模块,它导出一个 流.组合 可接受的值。报告器应转换由 <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.