1
+ import execa from 'execa'
1
2
import { relative } from 'pathe'
2
3
import { getAllPageDependencies } from '../packages/runtime/src/templates/getPageResolver'
3
4
@@ -19,6 +20,7 @@ const {
19
20
readJson,
20
21
pathExists,
21
22
writeFile,
23
+ move,
22
24
} = require ( 'fs-extra' )
23
25
const path = require ( 'path' )
24
26
const process = require ( 'process' )
@@ -1783,6 +1785,8 @@ describe('onPreDev', () => {
1783
1785
it ( 'should compile middleware in the root directory' , async ( ) => {
1784
1786
await moveNextDist ( '.next' , true )
1785
1787
await writeFile ( path . join ( process . cwd ( ) , 'middleware.ts' ) , middlewareSourceTs )
1788
+ expect ( middlewareExists ( ) ) . toBeFalsy ( )
1789
+
1786
1790
await runtime . onPreDev ( defaultArgs )
1787
1791
await wait ( )
1788
1792
@@ -1818,3 +1822,121 @@ describe('onPreDev', () => {
1818
1822
expect ( middlewareExists ( ) ) . toBeTruthy ( )
1819
1823
} )
1820
1824
} )
1825
+
1826
+ const watcherPath = require . resolve ( '@netlify/plugin-nextjs/lib/helpers/watcher' )
1827
+
1828
+ fdescribe ( 'the dev middleware watcher' , ( ) => {
1829
+ it ( 'should compile once and exit if run with the --once flag' , async ( ) => {
1830
+ await moveNextDist ( '.next' , true )
1831
+ await writeFile ( path . join ( process . cwd ( ) , 'middleware.ts' ) , middlewareSourceTs )
1832
+
1833
+ expect ( middlewareExists ( ) ) . toBeFalsy ( )
1834
+
1835
+ const watcher = execa . node ( watcherPath , [ process . cwd ( ) , '--once' ] )
1836
+ const output = await watcher
1837
+ expect ( middlewareExists ( ) ) . toBeTruthy ( )
1838
+ expect ( output . stdout ) . toContain ( 'Building middleware middleware.ts' )
1839
+ expect ( output . stdout ) . toContain ( '...done' )
1840
+ } )
1841
+
1842
+ it ( 'should not compile anything if there is no middleware' , async ( ) => {
1843
+ await moveNextDist ( '.next' , true )
1844
+ const watcher = execa . node ( watcherPath , [ process . cwd ( ) , '--once' ] )
1845
+ const output = await watcher
1846
+ expect ( middlewareExists ( ) ) . toBeFalsy ( )
1847
+ expect ( output . stdout ) . toBe ( 'Initial scan complete. Ready for changes' )
1848
+ } )
1849
+
1850
+ it ( 'should compile a middleware file and then exit when killed' , async ( ) => {
1851
+ await moveNextDist ( '.next' , true )
1852
+ await writeFile ( path . join ( process . cwd ( ) , 'middleware.ts' ) , middlewareSourceTs )
1853
+ expect ( middlewareExists ( ) ) . toBeFalsy ( )
1854
+ const watcher = execa . node ( watcherPath , [ process . cwd ( ) ] )
1855
+ await wait ( )
1856
+ expect ( middlewareExists ( ) ) . toBeTruthy ( )
1857
+ expect ( watcher . kill ( ) ) . toBeTruthy ( )
1858
+ } )
1859
+
1860
+ it ( 'should compile a file if it is written after the watcher starts' , async ( ) => {
1861
+ await moveNextDist ( '.next' , true )
1862
+ const watcher = execa . node ( watcherPath , [ process . cwd ( ) ] )
1863
+ await wait ( )
1864
+ expect ( middlewareExists ( ) ) . toBeFalsy ( )
1865
+ await writeFile ( path . join ( process . cwd ( ) , 'middleware.ts' ) , middlewareSourceTs )
1866
+ await wait ( )
1867
+ expect ( middlewareExists ( ) ) . toBeTruthy ( )
1868
+ expect ( watcher . kill ( ) ) . toBeTruthy ( )
1869
+ } )
1870
+
1871
+ it ( 'should remove the output if the middleware is removed after the watcher starts' , async ( ) => {
1872
+ await moveNextDist ( '.next' , true )
1873
+ const watcher = execa . node ( watcherPath , [ process . cwd ( ) ] )
1874
+ await wait ( )
1875
+ expect ( middlewareExists ( ) ) . toBeFalsy ( )
1876
+ await writeFile ( path . join ( process . cwd ( ) , 'middleware.ts' ) , middlewareSourceTs )
1877
+ await wait ( )
1878
+
1879
+ expect ( middlewareExists ( ) ) . toBeTruthy ( )
1880
+ await unlink ( path . join ( process . cwd ( ) , 'middleware.ts' ) )
1881
+ await wait ( )
1882
+ expect ( middlewareExists ( ) ) . toBeFalsy ( )
1883
+ expect ( watcher . kill ( ) ) . toBeTruthy ( )
1884
+ } )
1885
+
1886
+ it ( 'should remove the output if invalid middleware is written after the watcher starts' , async ( ) => {
1887
+ await moveNextDist ( '.next' , true )
1888
+ const watcher = execa . node ( watcherPath , [ process . cwd ( ) ] )
1889
+ await wait ( )
1890
+ expect ( middlewareExists ( ) ) . toBeFalsy ( )
1891
+ await writeFile ( path . join ( process . cwd ( ) , 'middleware.ts' ) , middlewareSourceTs )
1892
+ await wait ( )
1893
+ expect ( middlewareExists ( ) ) . toBeTruthy ( )
1894
+ await writeFile ( path . join ( process . cwd ( ) , 'middleware.ts' ) , 'this is not valid middleware' )
1895
+ await wait ( )
1896
+ expect ( middlewareExists ( ) ) . toBeFalsy ( )
1897
+ expect ( watcher . kill ( ) ) . toBeTruthy ( )
1898
+ } )
1899
+
1900
+ it ( 'should recompile the middleware if it is moved into the src directory after the watcher starts' , async ( ) => {
1901
+ await moveNextDist ( '.next' , true )
1902
+ const watcher = execa . node ( watcherPath , [ process . cwd ( ) ] )
1903
+ await wait ( )
1904
+ expect ( middlewareExists ( ) ) . toBeFalsy ( )
1905
+ await writeFile ( path . join ( process . cwd ( ) , 'middleware.ts' ) , middlewareSourceTs )
1906
+ await wait ( )
1907
+ expect ( middlewareExists ( ) ) . toBeTruthy ( )
1908
+ await ensureDir ( path . join ( process . cwd ( ) , 'src' ) )
1909
+ await move ( path . join ( process . cwd ( ) , 'middleware.ts' ) , path . join ( process . cwd ( ) , 'src' , 'middleware.ts' ) )
1910
+ await wait ( )
1911
+ expect ( middlewareExists ( ) ) . toBeTruthy ( )
1912
+ expect ( watcher . kill ( ) ) . toBeTruthy ( )
1913
+ } )
1914
+
1915
+ it ( 'should recompile the middleware if it is moved into the root directory after the watcher starts' , async ( ) => {
1916
+ await moveNextDist ( '.next' , true )
1917
+ const watcher = execa . node ( watcherPath , [ process . cwd ( ) ] )
1918
+ await wait ( )
1919
+ await ensureDir ( path . join ( process . cwd ( ) , 'src' ) )
1920
+ await writeFile ( path . join ( process . cwd ( ) , 'src' , 'middleware.ts' ) , middlewareSourceTs )
1921
+ await wait ( )
1922
+ expect ( middlewareExists ( ) ) . toBeTruthy ( )
1923
+ await move ( path . join ( process . cwd ( ) , 'src' , 'middleware.ts' ) , path . join ( process . cwd ( ) , 'middleware.ts' ) )
1924
+ await wait ( )
1925
+ expect ( middlewareExists ( ) ) . toBeTruthy ( )
1926
+ expect ( watcher . kill ( ) ) . toBeTruthy ( )
1927
+ } )
1928
+
1929
+ it ( 'should compile the middleware if invalid source is replaced with valid source after the watcher starts' , async ( ) => {
1930
+ await moveNextDist ( '.next' , true )
1931
+ expect ( middlewareExists ( ) ) . toBeFalsy ( )
1932
+
1933
+ await writeFile ( path . join ( process . cwd ( ) , 'middleware.ts' ) , 'this is not valid middleware' )
1934
+ const watcher = execa . node ( watcherPath , [ process . cwd ( ) ] )
1935
+ await wait ( )
1936
+ expect ( middlewareExists ( ) ) . toBeFalsy ( )
1937
+ await writeFile ( path . join ( process . cwd ( ) , 'middleware.ts' ) , middlewareSourceTs )
1938
+ await wait ( )
1939
+ expect ( middlewareExists ( ) ) . toBeTruthy ( )
1940
+ expect ( watcher . kill ( ) ) . toBeTruthy ( )
1941
+ } )
1942
+ } )
0 commit comments