@@ -1788,6 +1788,13 @@ describe('onPreDev', () => {
1788
1788
1789
1789
const watcherPath = require . resolve ( '@netlify/plugin-nextjs/lib/helpers/watcher' )
1790
1790
1791
+ // Kill the process and resolve a promise when the process exits
1792
+ const killProcess = ( process ) =>
1793
+ new Promise ( ( resolve ) => {
1794
+ process . on ( 'exit' , resolve )
1795
+ process . kill ( )
1796
+ } )
1797
+
1791
1798
describe ( 'the dev middleware watcher' , ( ) => {
1792
1799
it . skip ( 'should compile once and exit if run with the --once flag' , async ( ) => {
1793
1800
await moveNextDist ( '.next' , true )
@@ -1817,7 +1824,8 @@ describe('the dev middleware watcher', () => {
1817
1824
const watcher = execa . node ( watcherPath , [ process . cwd ( ) ] )
1818
1825
await wait ( )
1819
1826
expect ( middlewareExists ( ) ) . toBeTruthy ( )
1820
- expect ( watcher . kill ( ) ) . toBeTruthy ( )
1827
+ await killProcess ( watcher )
1828
+ expect ( watcher . killed ) . toBeTruthy ( )
1821
1829
} )
1822
1830
1823
1831
it ( 'should compile a file if it is written after the watcher starts' , async ( ) => {
@@ -1828,7 +1836,8 @@ describe('the dev middleware watcher', () => {
1828
1836
await writeFile ( path . join ( process . cwd ( ) , 'middleware.ts' ) , middlewareSourceTs )
1829
1837
await wait ( )
1830
1838
expect ( middlewareExists ( ) ) . toBeTruthy ( )
1831
- expect ( watcher . kill ( ) ) . toBeTruthy ( )
1839
+ await killProcess ( watcher )
1840
+ expect ( watcher . killed ) . toBeTruthy ( )
1832
1841
} )
1833
1842
1834
1843
it ( 'should remove the output if the middleware is removed after the watcher starts' , async ( ) => {
@@ -1843,7 +1852,8 @@ describe('the dev middleware watcher', () => {
1843
1852
await unlink ( path . join ( process . cwd ( ) , 'middleware.ts' ) )
1844
1853
await wait ( )
1845
1854
expect ( middlewareExists ( ) ) . toBeFalsy ( )
1846
- expect ( watcher . kill ( ) ) . toBeTruthy ( )
1855
+ await killProcess ( watcher )
1856
+ expect ( watcher . killed ) . toBeTruthy ( )
1847
1857
} )
1848
1858
1849
1859
it . skip ( 'should remove the output if invalid middleware is written after the watcher starts' , async ( ) => {
@@ -1857,7 +1867,8 @@ describe('the dev middleware watcher', () => {
1857
1867
await writeFile ( path . join ( process . cwd ( ) , 'middleware.ts' ) , 'this is not valid middleware' )
1858
1868
await wait ( )
1859
1869
expect ( middlewareExists ( ) ) . toBeFalsy ( )
1860
- expect ( watcher . kill ( ) ) . toBeTruthy ( )
1870
+ await killProcess ( watcher )
1871
+ expect ( watcher . killed ) . toBeTruthy ( )
1861
1872
} )
1862
1873
1863
1874
it . skip ( 'should recompile the middleware if it is moved into the src directory after the watcher starts' , async ( ) => {
@@ -1872,7 +1883,8 @@ describe('the dev middleware watcher', () => {
1872
1883
await move ( path . join ( process . cwd ( ) , 'middleware.ts' ) , path . join ( process . cwd ( ) , 'src' , 'middleware.ts' ) )
1873
1884
await wait ( )
1874
1885
expect ( middlewareExists ( ) ) . toBeTruthy ( )
1875
- expect ( watcher . kill ( ) ) . toBeTruthy ( )
1886
+ await killProcess ( watcher )
1887
+ expect ( watcher . killed ) . toBeTruthy ( )
1876
1888
} )
1877
1889
1878
1890
it . skip ( 'should recompile the middleware if it is moved into the root directory after the watcher starts' , async ( ) => {
@@ -1886,7 +1898,8 @@ describe('the dev middleware watcher', () => {
1886
1898
await move ( path . join ( process . cwd ( ) , 'src' , 'middleware.ts' ) , path . join ( process . cwd ( ) , 'middleware.ts' ) )
1887
1899
await wait ( )
1888
1900
expect ( middlewareExists ( ) ) . toBeTruthy ( )
1889
- expect ( watcher . kill ( ) ) . toBeTruthy ( )
1901
+ await killProcess ( watcher )
1902
+ expect ( watcher . killed ) . toBeTruthy ( )
1890
1903
} )
1891
1904
1892
1905
it . skip ( 'should compile the middleware if invalid source is replaced with valid source after the watcher starts' , async ( ) => {
@@ -1900,7 +1913,8 @@ describe('the dev middleware watcher', () => {
1900
1913
await writeFile ( path . join ( process . cwd ( ) , 'middleware.ts' ) , middlewareSourceTs )
1901
1914
await wait ( )
1902
1915
expect ( middlewareExists ( ) ) . toBeTruthy ( )
1903
- expect ( watcher . kill ( ) ) . toBeTruthy ( )
1916
+ await killProcess ( watcher )
1917
+ expect ( watcher . killed ) . toBeTruthy ( )
1904
1918
} )
1905
1919
1906
1920
it . skip ( 'should not compile middleware if more than one middleware file exists' , async ( ) => {
@@ -1912,7 +1926,8 @@ describe('the dev middleware watcher', () => {
1912
1926
const watcher = execa . node ( watcherPath , [ process . cwd ( ) ] )
1913
1927
await wait ( )
1914
1928
expect ( middlewareExists ( ) ) . toBeFalsy ( )
1915
- expect ( watcher . kill ( ) ) . toBeTruthy ( )
1929
+ await killProcess ( watcher )
1930
+ expect ( watcher . killed ) . toBeTruthy ( )
1916
1931
} )
1917
1932
1918
1933
it . skip ( 'should not compile middleware if a second middleware file is added after the watcher starts' , async ( ) => {
@@ -1926,7 +1941,8 @@ describe('the dev middleware watcher', () => {
1926
1941
await writeFile ( path . join ( process . cwd ( ) , 'middleware.js' ) , middlewareSourceJs )
1927
1942
await wait ( )
1928
1943
expect ( middlewareExists ( ) ) . toBeFalsy ( )
1929
- expect ( watcher . kill ( ) ) . toBeTruthy ( )
1944
+ await killProcess ( watcher )
1945
+ expect ( watcher . killed ) . toBeTruthy ( )
1930
1946
} )
1931
1947
1932
1948
it . skip ( 'should compile middleware if a second middleware file is removed after the watcher starts' , async ( ) => {
@@ -1941,7 +1957,8 @@ describe('the dev middleware watcher', () => {
1941
1957
await unlink ( path . join ( process . cwd ( ) , 'middleware.js' ) )
1942
1958
await wait ( )
1943
1959
expect ( middlewareExists ( ) ) . toBeTruthy ( )
1944
- expect ( watcher . kill ( ) ) . toBeTruthy ( )
1960
+ await killProcess ( watcher )
1961
+ expect ( watcher . killed ) . toBeTruthy ( )
1945
1962
} )
1946
1963
1947
1964
it . skip ( 'should generate the correct output for each case when middleware is compiled, added, removed and for error states' , async ( ) => {
@@ -1975,6 +1992,7 @@ describe('the dev middleware watcher', () => {
1975
1992
expect ( stdioString ) . toContain ( 'Multiple middleware files found' )
1976
1993
expect ( middlewareExists ( ) ) . toBeFalsy ( )
1977
1994
1978
- expect ( watcher . kill ( ) ) . toBeTruthy ( )
1995
+ await killProcess ( watcher )
1996
+ expect ( watcher . killed ) . toBeTruthy ( )
1979
1997
} )
1980
1998
} )
0 commit comments