Skip to content

Commit

Permalink
Refactor development-logs removing duplicated code. (#28049)
Browse files Browse the repository at this point in the history
Hi, this is small refactor for the development-logs.js test.
The same code to verify the browser logs is written 3 times.
Actually, the only difference is the path and then the expect result to confirm whether the logs are showing or not.
Moved the triplicated code to a `getLogs$(path)` and matched the expect results.
  • Loading branch information
leitea-google committed Aug 13, 2021
1 parent 0479477 commit f95e5fd
Showing 1 changed file with 27 additions and 54 deletions.
81 changes: 27 additions & 54 deletions test/integration/basic/test/development-logs.js
Expand Up @@ -2,66 +2,39 @@
import webdriver from 'next-webdriver'

export default (context) => {
describe('Development Logs', () => {
it('should warn when prefetch is true', async () => {
let browser
try {
browser = await webdriver(context.appPort, '/development-logs')
const browserLogs = await browser.log('browser')
let foundLog = false
browserLogs.forEach((log) => {
if (log.message.includes('Next.js auto-prefetches automatically')) {
foundLog = true
}
})
expect(foundLog).toBe(true)
} finally {
if (browser) {
await browser.close()
async function getLogs$(path) {
let foundLog = false
let browser
try {
browser = await webdriver(context.appPort, path)
const browserLogs = await browser.log('browser')

browserLogs.forEach((log) => {
if (log.message.includes('Next.js auto-prefetches automatically')) {
foundLog = true
}
})
} finally {
if (browser) {
await browser.close()
}
}
return foundLog
}
describe('Development Logs', () => {
it('should warn when prefetch is true', async () => {
const foundLog = await getLogs$('/development-logs')
expect(foundLog).toBe(true)
})
it('should not warn when prefetch is false', async () => {
let browser
try {
browser = await webdriver(
context.appPort,
'/development-logs/link-with-prefetch-false'
)
const browserLogs = await browser.log('browser')
let found = false
browserLogs.forEach((log) => {
if (log.message.includes('Next.js auto-prefetches automatically')) {
found = true
}
})
expect(found).toBe(false)
} finally {
if (browser) {
await browser.close()
}
}
const foundLog = await getLogs$(
'/development-logs/link-with-prefetch-false'
)
expect(foundLog).toBe(false)
})
it('should not warn when prefetch is not specified', async () => {
let browser
try {
browser = await webdriver(
context.appPort,
'/development-logs/link-with-no-prefetch'
)
const browserLogs = await browser.log('browser')
let found = false
browserLogs.forEach((log) => {
if (log.message.includes('Next.js auto-prefetches automatically')) {
found = true
}
})
expect(found).toBe(false)
} finally {
if (browser) {
await browser.close()
}
}
const foundLog = await getLogs$('/development-logs/link-with-no-prefetch')
expect(foundLog).toBe(false)
})
})
}

0 comments on commit f95e5fd

Please sign in to comment.