1- import { channel , tracingChannel } from "node:diagnostics_channel" ;
2- import { describeMatrix } from "./_setup.ts" ;
3- import { H3 } from "../src/h3.ts" ;
4- import {
5- type H3InitPayload ,
6- type H3MountPayload ,
7- type H3THandlerTracePayload ,
8- tracingPlugin ,
9- } from "../src/tracing.ts" ;
1+ import { tracingChannel } from "node:diagnostics_channel" ;
2+ import { describeMatrix , type TestOptions } from "./_setup.ts" ;
3+ import { type H3THandlerTracePayload } from "../src/tracing.ts" ;
104
115type TracingEvent = {
126 start ?: { data : H3THandlerTracePayload } ;
@@ -16,14 +10,6 @@ type TracingEvent = {
1610 error ?: { data : H3THandlerTracePayload ; error : Error } ;
1711} ;
1812
19- function createH3WithTracing ( ) {
20- const app = new H3 ( {
21- plugins : [ tracingPlugin ( ) ] ,
22- } ) ;
23-
24- return app ;
25- }
26-
2713function createTracingListener ( ) {
2814 const events : TracingEvent [ ] = [ ] ;
2915
@@ -69,111 +55,12 @@ function createTracingListener() {
6955 } ;
7056}
7157
58+ // Matrix is configured with tracing plugin enabled
59+ const testOpts : TestOptions = { tracing : true } ;
60+
7261describeMatrix (
7362 "tracing channels" ,
7463 ( t , { it, expect } ) => {
75- it ( "h3.init channel fires when app is initialized" , async ( ) => {
76- const events : H3InitPayload [ ] = [ ] ;
77- const initChannel = channel ( "h3.init" ) ;
78-
79- const handler = ( message : unknown ) => {
80- events . push ( message as H3InitPayload ) ;
81- } ;
82- initChannel . subscribe ( handler ) ;
83-
84- try {
85- const app = createH3WithTracing ( ) ;
86- expect ( events ) . toHaveLength ( 1 ) ;
87- expect ( events [ 0 ] . app ) . toBe ( app ) ;
88- } finally {
89- initChannel . unsubscribe ( handler ) ;
90- }
91- } ) ;
92-
93- it ( "h3.init listener can configure global error handler" , async ( ) => {
94- const initChannel = channel ( "h3.init" ) ;
95-
96- const customError = ( error : any ) => {
97- return new Response (
98- JSON . stringify ( { custom : true , message : error . message } ) ,
99- {
100- status : error . status || 500 ,
101- headers : { "content-type" : "application/json" } ,
102- } ,
103- ) ;
104- } ;
105-
106- const handler = ( message : unknown ) => {
107- const { app } = message as H3InitPayload ;
108- app . config . onError = customError ;
109- } ;
110- initChannel . subscribe ( handler ) ;
111-
112- try {
113- const app = createH3WithTracing ( ) ;
114- app . get ( "/error" , ( ) => {
115- throw new Error ( "Test error" ) ;
116- } ) ;
117-
118- const res = await app . request ( "/error" ) ;
119- expect ( res . status ) . toBe ( 500 ) ;
120- const body = await res . json ( ) ;
121- expect ( body . custom ) . toBe ( true ) ;
122- expect ( body . message ) . toBe ( "Test error" ) ;
123- } finally {
124- initChannel . unsubscribe ( handler ) ;
125- }
126- } ) ;
127-
128- it ( "h3.mount channel fires when nested app is mounted" , async ( ) => {
129- const events : H3MountPayload [ ] = [ ] ;
130- const mountChannel = channel ( "h3.mount" ) ;
131-
132- const handler = ( message : unknown ) => {
133- events . push ( message as H3MountPayload ) ;
134- } ;
135- mountChannel . subscribe ( handler ) ;
136-
137- try {
138- const nestedApp = createH3WithTracing ( ) ;
139- nestedApp . get ( "/test" , ( ) => "nested" ) ;
140-
141- t . app . mount ( "/api" , nestedApp ) ;
142-
143- expect ( events ) . toHaveLength ( 1 ) ;
144- expect ( events [ 0 ] . app ) . toBe ( t . app ) ;
145- expect ( events [ 0 ] . base ) . toBe ( "/api" ) ;
146- expect ( events [ 0 ] . mountedApp ) . toBe ( nestedApp ) ;
147- } finally {
148- mountChannel . unsubscribe ( handler ) ;
149- }
150- } ) ;
151-
152- it ( "h3.mount channel fires for fetchable objects" , async ( ) => {
153- const events : H3MountPayload [ ] = [ ] ;
154- const mountChannel = channel ( "h3.mount" ) ;
155-
156- const handler = ( message : unknown ) => {
157- events . push ( message as H3MountPayload ) ;
158- } ;
159- mountChannel . subscribe ( handler ) ;
160-
161- try {
162- const fetchHandler = {
163- fetch : ( ) => new Response ( "fetchable" ) ,
164- } ;
165-
166- t . app . mount ( "/fetch" , fetchHandler ) ;
167-
168- expect ( events ) . toHaveLength ( 1 ) ;
169- expect ( events [ 0 ] . app ) . toBe ( t . app ) ;
170- expect ( events [ 0 ] . base ) . toBe ( "/fetch" ) ;
171- expect ( events [ 0 ] . mountedApp ) . toBe ( fetchHandler ) ;
172- } finally {
173- mountChannel . unsubscribe ( handler ) ;
174- }
175- } ) ;
176-
17764 it ( "tracing channels fire for handlers" , async ( ) => {
17865 const listener = createTracingListener ( ) ;
17966
@@ -466,5 +353,5 @@ describeMatrix(
466353 }
467354 } ) ;
468355 } ,
469- { tracing : true } ,
356+ testOpts ,
470357) ;
0 commit comments