Skip to content

Commit

Permalink
improve reliability in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsherrard committed Oct 1, 2024
1 parent bc709e6 commit 3ad7d52
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 43 deletions.
88 changes: 46 additions & 42 deletions apps/next/__tests__/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,58 @@ afterEach(() => {
}
})

test('Next.js build completes', async () => {
buildProcess = exec('yarn build', {
cwd: path.resolve(__dirname, '..'),
})
test('Next.js build completes', () => {
return new Promise<void>((resolve, reject) => {
buildProcess = exec('yarn build', {
cwd: path.resolve(__dirname, '..'),
})

let output = ''
buildProcess.stdout?.on('data', (data) => {
output += data.toString()
})
let output = ''
buildProcess.stdout?.on('data', (data) => {
output += data.toString()
})

buildProcess.stderr?.on('data', (data) => {
output += data.toString()
})
buildProcess.stderr?.on('data', (data) => {
output += data.toString()
})

buildProcess.on('close', (code) => {
if (code === 0) {
const buildOutput = output
// Check for yarn build output
expect(buildOutput).toContain('built @my/config')
expect(buildOutput).toContain('built @my/ui')
buildProcess.on('close', (code) => {
if (code === 0) {
const buildOutput = output
// Check for yarn build output
expect(buildOutput).toContain('built @my/config')
expect(buildOutput).toContain('built @my/ui')

// Check for Next.js version and build process
expect(buildOutput).toContain('Next.js 14')
expect(buildOutput).toContain('Creating an optimized production build')
// Check for Next.js version and build process
expect(buildOutput).toContain('Next.js 14')
expect(buildOutput).toContain('Creating an optimized production build')

// Check for route information
expect(buildOutput).toContain('Route (app)')
expect(buildOutput).toContain('Route (pages)')
expect(buildOutput).toContain('First Load JS shared by all')
// Check for route information
expect(buildOutput).toContain('Route (app)')
expect(buildOutput).toContain('Route (pages)')
expect(buildOutput).toContain('First Load JS shared by all')

// Check for specific route patterns
expect(buildOutput).toContain('○ /')
expect(buildOutput).toContain('○ /_not-found')
expect(buildOutput).toContain('ƒ /user/[id]')
expect(buildOutput).toContain('/_app')
expect(buildOutput).toContain('/pages-example')
expect(buildOutput).toContain('/pages-example-user/[id]')
// Check for specific route patterns
expect(buildOutput).toContain('○ /')
expect(buildOutput).toContain('○ /_not-found')
expect(buildOutput).toContain('ƒ /user/[id]')
expect(buildOutput).toContain('/_app')
expect(buildOutput).toContain('/pages-example')
expect(buildOutput).toContain('/pages-example-user/[id]')

// Check for chunk information
expect(buildOutput).toContain('chunks/framework-')
expect(buildOutput).toContain('chunks/main-')
expect(buildOutput).toContain('chunks/pages/_app-')
// Check for chunk information
expect(buildOutput).toContain('chunks/framework-')
expect(buildOutput).toContain('chunks/main-')
expect(buildOutput).toContain('chunks/pages/_app-')

// Check for static and dynamic route indicators
expect(buildOutput).toContain('○ (Static) prerendered as static content')
expect(buildOutput).toContain('ƒ (Dynamic) server-rendered on demand')
} else {
console.error('Build process exited with code', code)
}
// Check for static and dynamic route indicators
expect(buildOutput).toContain('○ (Static) prerendered as static content')
expect(buildOutput).toContain('ƒ (Dynamic) server-rendered on demand')

resolve()
} else {
reject(new Error(`Build process exited with code ${code}`))

Check failure on line 64 in apps/next/__tests__/build.test.ts

View workflow job for this annotation

GitHub Actions / test

apps/next/__tests__/build.test.ts > Next.js build completes

Error: Build process exited with code 1 ❯ ChildProcess.<anonymous> apps/next/__tests__/build.test.ts:64:16

Check failure on line 64 in apps/next/__tests__/build.test.ts

View workflow job for this annotation

GitHub Actions / test

apps/next/__tests__/build.test.ts > Next.js build completes

Error: Build process exited with code 1 ❯ ChildProcess.<anonymous> apps/next/__tests__/build.test.ts:64:16
}
})
})
})
}, 120000) // Set timeout to 120 seconds
8 changes: 7 additions & 1 deletion apps/next/__tests__/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ test('Next.js dev server starts and serves the expected content', async () => {

devProcess.stderr?.on('data', (data) => {
output += data.toString()
// ... existing code ...
// Log errors or warnings
console.error('Dev server error:', data.toString())
// Potentially check for specific error conditions
if (data.toString().includes('EADDRINUSE')) {
console.error('Port is already in use. Please free up the port and try again.')
}
devProcess?.kill()
})

try {
Expand Down

0 comments on commit 3ad7d52

Please sign in to comment.