@@ -3,10 +3,6 @@ import { expect } from "@std/expect";
33import { serveMiddleware } from "../test_utils.ts" ;
44import type { MiddlewareFn } from "./mod.ts" ;
55
6- const THROWER = ( ) => {
7- throw new Error ( "fail" ) ;
8- } ;
9-
106Deno . test ( "compileMiddlewares" , async ( ) => {
117 const middlewares : MiddlewareFn < { text : string } > [ ] = [
128 ( ctx ) => {
@@ -28,7 +24,7 @@ Deno.test("compileMiddlewares", async () => {
2824 ] ;
2925
3026 const server = serveMiddleware < { text : string } > (
31- compileMiddlewares ( middlewares , THROWER ) ,
27+ compileMiddlewares ( middlewares ) ,
3228 ) ;
3329
3430 const res = await server . get ( "/" ) ;
@@ -50,7 +46,7 @@ Deno.test("compileMiddlewares - middlewares should only be called once", async (
5046 new Response ( String ( ctx . state . count ) ) ;
5147
5248 const server = serveMiddleware < { count : number } > (
53- compileMiddlewares ( [ A ] , final ) ,
49+ compileMiddlewares ( [ A , final ] ) ,
5450 ) ;
5551
5652 const res = await server . get ( "/" ) ;
@@ -80,8 +76,7 @@ Deno.test("runMiddleware - runs multiple stacks", async () => {
8076 new Response ( String ( ctx . state . text ) ) ;
8177
8278 const server = serveMiddleware < State > ( compileMiddlewares (
83- [ A , B , C , D ] ,
84- final ,
79+ [ A , B , C , D , final ] ,
8580 ) ) ;
8681
8782 const res = await server . get ( "/" ) ;
@@ -118,14 +113,13 @@ Deno.test("runMiddleware - throws errors", async () => {
118113 throw err ;
119114 }
120115 } ,
116+ ( ) => {
117+ throw new Error ( "fail" ) ;
118+ } ,
121119 ] ;
122120
123- const final = ( ) => {
124- throw new Error ( "fail" ) ;
125- } ;
126-
127121 const server = serveMiddleware < { text : string } > (
128- compileMiddlewares ( middlewares , final ) ,
122+ compileMiddlewares ( middlewares ) ,
129123 ) ;
130124
131125 try {
@@ -137,3 +131,19 @@ Deno.test("runMiddleware - throws errors", async () => {
137131 expect ( thrownB ) . toBeInstanceOf ( Error ) ;
138132 expect ( thrownC ) . toBeInstanceOf ( Error ) ;
139133} ) ;
134+
135+ Deno . test ( "compileMiddlewares - calls last next" , async ( ) => {
136+ const middlewares : MiddlewareFn < { text : string } > [ ] = [
137+ ( ctx ) => ctx . next ( ) ,
138+ ] ;
139+
140+ const next = ( ) => Promise . resolve ( new Response ( "next" ) ) ;
141+
142+ const server = serveMiddleware < { text : string } > (
143+ compileMiddlewares ( middlewares ) ,
144+ { next } ,
145+ ) ;
146+
147+ const res = await server . get ( "/" ) ;
148+ expect ( await res . text ( ) ) . toEqual ( "next" ) ;
149+ } ) ;
0 commit comments