@@ -2,6 +2,7 @@ import mock from "mock-fs";
22import { NextJs , detectPagesOrAppDir , detectUseOfSrcDir } from "." ;
33import { getFramework } from ".." ;
44import { pathExists } from "../../utils/fileSystem" ;
5+ import { detectMiddlewareUsage } from "./middleware" ;
56
67afterEach ( ( ) => {
78 mock . restore ( ) ;
@@ -257,3 +258,130 @@ describe("app install", () => {
257258 expect ( await pathExists ( "jobs/examples.ts" ) ) . toEqual ( true ) ;
258259 } ) ;
259260} ) ;
261+
262+ describe ( "Next middleware detection" , ( ) => {
263+ test ( "no middleware" , async ( ) => {
264+ mock ( { } ) ;
265+
266+ const result = await detectMiddlewareUsage ( "" , false ) ;
267+ expect ( result . hasMiddleware ) . toEqual ( false ) ;
268+ } ) ;
269+
270+ test ( "Basic middleware" , async ( ) => {
271+ mock ( {
272+ "middleware.js" : `import { NextResponse } from 'next/server'
273+
274+ export function middleware(request) {
275+ return NextResponse.redirect(new URL('/home', request.url))
276+ }
277+
278+ // See "Matching Paths" below to learn more
279+ export const config = {
280+ matcher: '/about/:path*',
281+ }` ,
282+ } ) ;
283+ const result = await detectMiddlewareUsage ( "" , false ) ;
284+ expect ( result . hasMiddleware ) . toEqual ( true ) ;
285+ if ( ! result . hasMiddleware ) throw "Should have middleware" ;
286+ expect ( result . middlewarePath ) . toEqual ( "middleware.js" ) ;
287+ expect ( result . conflict ) . toEqual ( "possible" ) ;
288+ } ) ;
289+
290+ test ( "Wildcard that throws middleware" , async ( ) => {
291+ mock ( {
292+ "middleware.js" : `export const config = {
293+ matcher: "*",
294+ }` ,
295+ } ) ;
296+
297+ const result = await detectMiddlewareUsage ( "" , false ) ;
298+ expect ( result . hasMiddleware ) . toEqual ( true ) ;
299+ if ( ! result . hasMiddleware ) throw "Should have middleware" ;
300+ expect ( result . middlewarePath ) . toEqual ( "middleware.js" ) ;
301+ expect ( result . conflict ) . toEqual ( "possible" ) ;
302+ } ) ;
303+
304+ test ( "Array middleware" , async ( ) => {
305+ mock ( {
306+ "middleware.js" : `export const config = {
307+ matcher: ['/about/:path*', "/dashboard/:path*"],
308+ }` ,
309+ } ) ;
310+
311+ const result = await detectMiddlewareUsage ( "" , false ) ;
312+ expect ( result . hasMiddleware ) . toEqual ( true ) ;
313+ if ( ! result . hasMiddleware ) throw "Should have middleware" ;
314+ expect ( result . middlewarePath ) . toEqual ( "middleware.js" ) ;
315+ expect ( result . conflict ) . toEqual ( "possible" ) ;
316+ } ) ;
317+
318+ test ( "With dashes middleware" , async ( ) => {
319+ mock ( {
320+ "middleware.js" : `export const config = {
321+ matcher: ["/configurations-test/:path*", "/projects/:path*"],
322+ };` ,
323+ } ) ;
324+
325+ const result = await detectMiddlewareUsage ( "" , false ) ;
326+ expect ( result . hasMiddleware ) . toEqual ( true ) ;
327+ if ( ! result . hasMiddleware ) throw "Should have middleware" ;
328+ expect ( result . middlewarePath ) . toEqual ( "middleware.js" ) ;
329+ expect ( result . conflict ) . toEqual ( "possible" ) ;
330+ } ) ;
331+
332+ test ( "Likely double quoted string" , async ( ) => {
333+ mock ( {
334+ "middleware.js" : `export const config = {
335+ matcher: "/(.*)",
336+ };` ,
337+ } ) ;
338+
339+ const result = await detectMiddlewareUsage ( "" , false ) ;
340+ expect ( result . hasMiddleware ) . toEqual ( true ) ;
341+ if ( ! result . hasMiddleware ) throw "Should have middleware" ;
342+ expect ( result . middlewarePath ) . toEqual ( "middleware.js" ) ;
343+ expect ( result . conflict ) . toEqual ( "likely" ) ;
344+ } ) ;
345+
346+ test ( "Likely single quoted string" , async ( ) => {
347+ mock ( {
348+ "middleware.js" : `export const config = {
349+ matcher: '/(.*)',
350+ };` ,
351+ } ) ;
352+
353+ const result = await detectMiddlewareUsage ( "" , false ) ;
354+ expect ( result . hasMiddleware ) . toEqual ( true ) ;
355+ if ( ! result . hasMiddleware ) throw "Should have middleware" ;
356+ expect ( result . middlewarePath ) . toEqual ( "middleware.js" ) ;
357+ expect ( result . conflict ) . toEqual ( "likely" ) ;
358+ } ) ;
359+
360+ test ( "Likely double quoted array" , async ( ) => {
361+ mock ( {
362+ "middleware.js" : `export const config = {
363+ matcher: ["/pages/", "/(.*)"],
364+ };` ,
365+ } ) ;
366+
367+ const result = await detectMiddlewareUsage ( "" , false ) ;
368+ expect ( result . hasMiddleware ) . toEqual ( true ) ;
369+ if ( ! result . hasMiddleware ) throw "Should have middleware" ;
370+ expect ( result . middlewarePath ) . toEqual ( "middleware.js" ) ;
371+ expect ( result . conflict ) . toEqual ( "likely" ) ;
372+ } ) ;
373+
374+ test ( "Likely single quoted array" , async ( ) => {
375+ mock ( {
376+ "middleware.js" : `export const config = {
377+ matcher: ['/pages/', '/(.*)'],
378+ };` ,
379+ } ) ;
380+
381+ const result = await detectMiddlewareUsage ( "" , false ) ;
382+ expect ( result . hasMiddleware ) . toEqual ( true ) ;
383+ if ( ! result . hasMiddleware ) throw "Should have middleware" ;
384+ expect ( result . middlewarePath ) . toEqual ( "middleware.js" ) ;
385+ expect ( result . conflict ) . toEqual ( "likely" ) ;
386+ } ) ;
387+ } ) ;
0 commit comments