Skip to content

Commit 6562611

Browse files
committed
chore: add more tests
1 parent 6cab2cc commit 6562611

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

test/index.spec.js

+76-1
Original file line numberDiff line numberDiff line change
@@ -1825,7 +1825,7 @@ describe('onPreDev', () => {
18251825

18261826
const watcherPath = require.resolve('@netlify/plugin-nextjs/lib/helpers/watcher')
18271827

1828-
fdescribe('the dev middleware watcher', () => {
1828+
describe('the dev middleware watcher', () => {
18291829
it('should compile once and exit if run with the --once flag', async () => {
18301830
await moveNextDist('.next', true)
18311831
await writeFile(path.join(process.cwd(), 'middleware.ts'), middlewareSourceTs)
@@ -1939,4 +1939,79 @@ fdescribe('the dev middleware watcher', () => {
19391939
expect(middlewareExists()).toBeTruthy()
19401940
expect(watcher.kill()).toBeTruthy()
19411941
})
1942+
1943+
it('should not compile middleware if more than one middleware file exists', async () => {
1944+
await moveNextDist('.next', true)
1945+
expect(middlewareExists()).toBeFalsy()
1946+
1947+
await writeFile(path.join(process.cwd(), 'middleware.ts'), middlewareSourceTs)
1948+
await writeFile(path.join(process.cwd(), 'middleware.js'), middlewareSourceJs)
1949+
const watcher = execa.node(watcherPath, [process.cwd()])
1950+
await wait()
1951+
expect(middlewareExists()).toBeFalsy()
1952+
expect(watcher.kill()).toBeTruthy()
1953+
})
1954+
1955+
it('should not compile middleware if a second middleware file is added after the watcher starts', async () => {
1956+
await moveNextDist('.next', true)
1957+
expect(middlewareExists()).toBeFalsy()
1958+
1959+
await writeFile(path.join(process.cwd(), 'middleware.ts'), middlewareSourceTs)
1960+
const watcher = execa.node(watcherPath, [process.cwd()])
1961+
await wait()
1962+
expect(middlewareExists()).toBeTruthy()
1963+
await writeFile(path.join(process.cwd(), 'middleware.js'), middlewareSourceJs)
1964+
await wait()
1965+
expect(middlewareExists()).toBeFalsy()
1966+
expect(watcher.kill()).toBeTruthy()
1967+
})
1968+
1969+
it('should compile middleware if a second middleware file is removed after the watcher starts', async () => {
1970+
await moveNextDist('.next', true)
1971+
expect(middlewareExists()).toBeFalsy()
1972+
await writeFile(path.join(process.cwd(), 'middleware.ts'), middlewareSourceTs)
1973+
await writeFile(path.join(process.cwd(), 'middleware.ts'), middlewareSourceTs)
1974+
await writeFile(path.join(process.cwd(), 'middleware.js'), middlewareSourceJs)
1975+
const watcher = execa.node(watcherPath, [process.cwd()])
1976+
await wait()
1977+
expect(middlewareExists()).toBeFalsy()
1978+
await unlink(path.join(process.cwd(), 'middleware.js'))
1979+
await wait()
1980+
expect(middlewareExists()).toBeTruthy()
1981+
expect(watcher.kill()).toBeTruthy()
1982+
})
1983+
1984+
it('should generate the correct output for each case when middleware is compiled, added, removed and for error states', async () => {
1985+
await moveNextDist('.next', true)
1986+
expect(middlewareExists()).toBeFalsy()
1987+
let stdioString = ''
1988+
1989+
const watcher = execa.node(watcherPath, [process.cwd()])
1990+
watcher.stdout.on('data', (data) => {
1991+
stdioString += data
1992+
})
1993+
watcher.stderr.on('data', (data) => {
1994+
stdioString += data
1995+
})
1996+
await wait()
1997+
expect(stdioString).toContain('Initial scan complete')
1998+
stdioString = ''
1999+
await writeFile(path.join(process.cwd(), 'middleware.ts'), middlewareSourceTs)
2000+
await wait()
2001+
expect(stdioString).toContain('Rebuilding middleware middleware.ts')
2002+
stdioString = ''
2003+
await writeFile(path.join(process.cwd(), 'middleware.ts'), 'this is not valid middleware')
2004+
await wait()
2005+
expect(stdioString).toContain('Error: Build failed with 1 error')
2006+
stdioString = ''
2007+
await writeFile(path.join(process.cwd(), 'middleware.ts'), middlewareSourceTs)
2008+
await wait()
2009+
expect(middlewareExists()).toBeTruthy()
2010+
await writeFile(path.join(process.cwd(), 'middleware.js'), middlewareSourceJs)
2011+
await wait()
2012+
expect(stdioString).toContain('Multiple middleware files found')
2013+
expect(middlewareExists()).toBeFalsy()
2014+
2015+
expect(watcher.kill()).toBeTruthy()
2016+
})
19422017
})

0 commit comments

Comments
 (0)