Skip to content

Commit ace59e9

Browse files
committed
add failing integration test
1 parent af6cef4 commit ace59e9

File tree

2 files changed

+138
-1
lines changed

2 files changed

+138
-1
lines changed

integrations/postcss/next.test.ts

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe } from 'vitest'
2-
import { candidate, css, fetchStyles, js, json, retryAssertion, test } from '../utils'
2+
import { candidate, css, fetchStyles, js, json, jsx, retryAssertion, test, txt } from '../utils'
33

44
test(
55
'production build',
@@ -356,3 +356,131 @@ test(
356356
})
357357
},
358358
)
359+
360+
test(
361+
'changes to `public/` should not trigger an infinite loop',
362+
{
363+
fs: {
364+
'package.json': json`
365+
{
366+
"dependencies": {
367+
"react": "^18",
368+
"react-dom": "^18",
369+
"next": "^15",
370+
"@ducanh2912/next-pwa": "^10.2.9"
371+
},
372+
"devDependencies": {
373+
"@tailwindcss/postcss": "workspace:^",
374+
"tailwindcss": "workspace:^"
375+
}
376+
}
377+
`,
378+
'.gitignore': txt`
379+
.next/
380+
public/workbox-*.js
381+
public/sw.js
382+
`,
383+
'postcss.config.mjs': js`
384+
export default {
385+
plugins: {
386+
'@tailwindcss/postcss': {},
387+
},
388+
}
389+
`,
390+
'next.config.mjs': js`
391+
import withPWA from '@ducanh2912/next-pwa'
392+
393+
const pwaConfig = {
394+
dest: 'public',
395+
register: true,
396+
skipWaiting: true,
397+
reloadOnOnline: false,
398+
cleanupOutdatedCaches: true,
399+
clientsClaim: true,
400+
maximumFileSizeToCacheInBytes: 20 * 1024 * 1024,
401+
}
402+
403+
const nextConfig = {}
404+
405+
const configWithPWA = withPWA(pwaConfig)(nextConfig)
406+
407+
export default configWithPWA
408+
`,
409+
'app/layout.js': js`
410+
import './globals.css'
411+
412+
export default function RootLayout({ children }) {
413+
return (
414+
<html>
415+
<body>{children}</body>
416+
</html>
417+
)
418+
}
419+
`,
420+
'app/page.js': js`
421+
export default function Page() {
422+
return <div className="flex"></div>
423+
}
424+
`,
425+
'app/globals.css': css`
426+
@import 'tailwindcss/theme';
427+
@import 'tailwindcss/utilities';
428+
`,
429+
},
430+
},
431+
async ({ spawn, fs, expect }) => {
432+
let process = await spawn('pnpm next dev')
433+
434+
let url = ''
435+
await process.onStdout((m) => {
436+
let match = /Local:\s*(http.*)/.exec(m)
437+
if (match) url = match[1]
438+
return Boolean(url)
439+
})
440+
441+
await process.onStdout((m) => m.includes('Ready in'))
442+
443+
await retryAssertion(async () => {
444+
let css = await fetchStyles(url)
445+
expect(css).toContain(candidate`flex`)
446+
expect(css).not.toContain(candidate`underline`)
447+
})
448+
449+
await fs.write(
450+
'app/page.js',
451+
jsx`
452+
export default function Page() {
453+
return <div className="flex underline"></div>
454+
}
455+
`,
456+
)
457+
await process.onStdout((m) => m.includes('Compiled in'))
458+
459+
await retryAssertion(async () => {
460+
let css = await fetchStyles(url)
461+
expect(css).toContain(candidate`flex`)
462+
expect(css).toContain(candidate`underline`)
463+
})
464+
// Flush all existing messages in the queue
465+
process.flush()
466+
467+
// Fetch the styles one more time, to ensure we see the latest version of
468+
// the CSS
469+
await fetchStyles(url)
470+
471+
// At this point, no changes should triger a compile step. If we see any
472+
// changes, there is an infinite loop because we (the user) didn't write any
473+
// files to disk.
474+
//
475+
// Ensure there are no more changes in stdout (signaling no infinite loop)
476+
let result = await Promise.race([
477+
// If this succeeds, it means that it saw another change which indicates
478+
// an infinite loop.
479+
process.onStdout((m) => m.includes('Compiled in')).then(() => 'infinite loop detected'),
480+
481+
// There should be no changes in stdout
482+
new Promise((resolve) => setTimeout(() => resolve('no infinite loop detected'), 25_000)),
483+
])
484+
expect(result).toBe('no infinite loop detected')
485+
},
486+
)

integrations/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const PUBLIC_PACKAGES = (await fs.readdir(path.join(REPO_ROOT, 'dist'))).map((na
1515

1616
interface SpawnedProcess {
1717
dispose: () => void
18+
flush: () => void
1819
onStdout: (predicate: (message: string) => boolean) => Promise<void>
1920
onStderr: (predicate: (message: string) => boolean) => Promise<void>
2021
}
@@ -253,6 +254,13 @@ export function test(
253254

254255
return {
255256
dispose,
257+
flush() {
258+
stdoutActors.splice(0)
259+
stderrActors.splice(0)
260+
261+
stdoutMessages.splice(0)
262+
stderrMessages.splice(0)
263+
},
256264
onStdout(predicate: (message: string) => boolean) {
257265
return new Promise<void>((resolve) => {
258266
stdoutActors.push({ predicate, resolve })
@@ -504,6 +512,7 @@ export let css = dedent
504512
export let html = dedent
505513
export let ts = dedent
506514
export let js = dedent
515+
export let jsx = dedent
507516
export let json = dedent
508517
export let yaml = dedent
509518
export let txt = dedent

0 commit comments

Comments
 (0)