Unverified Commit b2f193b0 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

test: Cleanup and refactoring (#90454)

- `expect(hasImageMatchingUrl(browser, "...")).toBe(true)` has very bad error messages if the assertion fails
- there was a `(process.env.TURBOPACK_DEV ? describe.skip : describe)('production mode',` around a dev test
parent affb856d
...@@ -20,17 +20,15 @@ const appDir = join(__dirname, '../') ...@@ -20,17 +20,15 @@ const appDir = join(__dirname, '../')
let appPort let appPort
let app let app
async function hasImageMatchingUrl(browser, url) { async function getImageUrls(browser) {
const links = await browser.elementsByCss('img') return await Promise.all(
let foundMatch = false (await browser.elementsByCss('img')).map(async (link) =>
for (const link of links) { new URL(
const src = await link.getAttribute('src') await link.getAttribute('src'),
if (new URL(src, `http://localhost:${appPort}`).toString() === url) { `http://localhost:${appPort}`
foundMatch = true ).toString()
break )
} )
}
return foundMatch
} }
async function getComputed(browser, id, prop) { async function getComputed(browser, id, prop) {
...@@ -76,12 +74,9 @@ function runTests(mode) { ...@@ -76,12 +74,9 @@ function runTests(mode) {
return 'result-correct' return 'result-correct'
}, /result-correct/) }, /result-correct/)
expect( expect(await getImageUrls(browser)).toContain(
await hasImageMatchingUrl( `http://localhost:${appPort}/docs/_next/image?url=%2Fdocs%2Ftest.jpg&w=828&q=75`
browser, )
`http://localhost:${appPort}/docs/_next/image?url=%2Fdocs%2Ftest.jpg&w=828&q=75`
)
).toBe(true)
} finally { } finally {
await browser.close() await browser.close()
} }
......
...@@ -25,19 +25,16 @@ const appDir = join(__dirname, '../') ...@@ -25,19 +25,16 @@ const appDir = join(__dirname, '../')
let appPort let appPort
let app let app
async function hasImageMatchingUrl(browser, url) { async function getImageUrls(browser) {
const links = await browser.elementsByCss('img') return await Promise.all(
let foundMatch = false (await browser.elementsByCss('img')).map(async (link) =>
for (const link of links) { new URL(
const src = await link.getAttribute('src') await link.getAttribute('src'),
if (new URL(src, `http://localhost:${appPort}`).toString() === url) { `http://localhost:${appPort}`
foundMatch = true ).toString()
break )
} )
}
return foundMatch
} }
async function getComputed(browser, id, prop) { async function getComputed(browser, id, prop) {
const val = await browser.eval(`document.getElementById('${id}').${prop}`) const val = await browser.eval(`document.getElementById('${id}').${prop}`)
if (typeof val === 'number') { if (typeof val === 'number') {
...@@ -89,12 +86,9 @@ function runTests(mode) { ...@@ -89,12 +86,9 @@ function runTests(mode) {
return 'result-correct' return 'result-correct'
}, /result-correct/) }, /result-correct/)
expect( expect(await getImageUrls(browser)).toContain(
await hasImageMatchingUrl( `http://localhost:${appPort}/_next/image?url=%2Ftest.jpg&w=828&q=75`
browser, )
`http://localhost:${appPort}/_next/image?url=%2Ftest.jpg&w=828&q=75`
)
).toBe(true)
} finally { } finally {
if (browser) { if (browser) {
await browser.close() await browser.close()
...@@ -1444,30 +1438,17 @@ function runTests(mode) { ...@@ -1444,30 +1438,17 @@ function runTests(mode) {
return 'result-correct' return 'result-correct'
}, /result-correct/) }, /result-correct/)
expect( const imageUrls = await getImageUrls(browser)
await hasImageMatchingUrl( expect(imageUrls).not.toContain(
browser, `http://localhost:${appPort}/_next/image?url=%2Ftest.jpg&w=828&q=75`
`http://localhost:${appPort}/_next/image?url=%2Ftest.jpg&w=828&q=75` )
) expect(imageUrls).toContain(
).toBe(false) `http://localhost:${appPort}/_next/image?url=%2Ftest.png&w=828&q=75`
expect( )
await hasImageMatchingUrl( expect(imageUrls).toContain(`http://localhost:${appPort}/test.svg`)
browser, expect(imageUrls).not.toContain(
`http://localhost:${appPort}/_next/image?url=%2Ftest.png&w=828&q=75` `http://localhost:${appPort}/_next/image?url=%2Ftest.webp&w=828&q=75`
) )
).toBe(true)
expect(
await hasImageMatchingUrl(
browser,
`http://localhost:${appPort}/test.svg`
)
).toBe(true)
expect(
await hasImageMatchingUrl(
browser,
`http://localhost:${appPort}/_next/image?url=%2Ftest.webp&w=828&q=75`
)
).toBe(false)
} finally { } finally {
if (browser) { if (browser) {
await browser.close() await browser.close()
......
...@@ -27,19 +27,16 @@ const appDir = join(__dirname, '../') ...@@ -27,19 +27,16 @@ const appDir = join(__dirname, '../')
let appPort let appPort
let app let app
async function hasImageMatchingUrl(browser, url) { async function getImageUrls(browser) {
const links = await browser.elementsByCss('img') return await Promise.all(
let foundMatch = false (await browser.elementsByCss('img')).map(async (link) =>
for (const link of links) { new URL(
const src = await link.getAttribute('src') await link.getAttribute('src'),
if (new URL(src, `http://localhost:${appPort}`).toString() === url) { `http://localhost:${appPort}`
foundMatch = true ).toString()
break )
} )
}
return foundMatch
} }
async function getComputed(browser, id, prop) { async function getComputed(browser, id, prop) {
const val = await browser.eval(`document.getElementById('${id}').${prop}`) const val = await browser.eval(`document.getElementById('${id}').${prop}`)
if (typeof val === 'number') { if (typeof val === 'number') {
...@@ -91,12 +88,9 @@ function runTests(mode: 'dev' | 'server') { ...@@ -91,12 +88,9 @@ function runTests(mode: 'dev' | 'server') {
return 'result-correct' return 'result-correct'
}, /result-correct/) }, /result-correct/)
expect( expect(await getImageUrls(browser)).toContain(
await hasImageMatchingUrl( `http://localhost:${appPort}/_next/image?url=%2Ftest.jpg&w=828&q=75`
browser, )
`http://localhost:${appPort}/_next/image?url=%2Ftest.jpg&w=828&q=75`
)
).toBe(true)
} finally { } finally {
if (browser) { if (browser) {
await browser.close() await browser.close()
......
...@@ -20,17 +20,15 @@ const appDir = join(__dirname, '../') ...@@ -20,17 +20,15 @@ const appDir = join(__dirname, '../')
let appPort let appPort
let app let app
async function hasImageMatchingUrl(browser, url) { async function getImageUrls(browser) {
const links = await browser.elementsByCss('img') return await Promise.all(
let foundMatch = false (await browser.elementsByCss('img')).map(async (link) =>
for (const link of links) { new URL(
const src = await link.getAttribute('src') await link.getAttribute('src'),
if (new URL(src, `http://localhost:${appPort}`).toString() === url) { `http://localhost:${appPort}`
foundMatch = true ).toString()
break )
} )
}
return foundMatch
} }
async function getComputed(browser, id, prop) { async function getComputed(browser, id, prop) {
...@@ -70,12 +68,9 @@ function runTests(mode) { ...@@ -70,12 +68,9 @@ function runTests(mode) {
return 'result-correct' return 'result-correct'
}, /result-correct/) }, /result-correct/)
expect( expect(await getImageUrls(browser)).toContain(
await hasImageMatchingUrl( `http://localhost:${appPort}/docs/_next/image?url=%2Fdocs%2Ftest.jpg&w=828&q=75`
browser, )
`http://localhost:${appPort}/docs/_next/image?url=%2Fdocs%2Ftest.jpg&w=828&q=75`
)
).toBe(true)
} finally { } finally {
if (browser) { if (browser) {
await browser.close() await browser.close()
......
...@@ -29,17 +29,15 @@ const appDir = join(__dirname, '../') ...@@ -29,17 +29,15 @@ const appDir = join(__dirname, '../')
let appPort let appPort
let app let app
async function hasImageMatchingUrl(browser, url) { async function getImageUrls(browser) {
const links = await browser.elementsByCss('img') return await Promise.all(
let foundMatch = false (await browser.elementsByCss('img')).map(async (link) =>
for (const link of links) { new URL(
const src = await link.getAttribute('src') await link.getAttribute('src'),
if (new URL(src, `http://localhost:${appPort}`).toString() === url) { `http://localhost:${appPort}`
foundMatch = true ).toString()
break )
} )
}
return foundMatch
} }
async function getComputed(browser, id, prop) { async function getComputed(browser, id, prop) {
...@@ -93,12 +91,9 @@ function runTests(mode) { ...@@ -93,12 +91,9 @@ function runTests(mode) {
return 'result-correct' return 'result-correct'
}, /result-correct/) }, /result-correct/)
expect( expect(await getImageUrls(browser)).toContain(
await hasImageMatchingUrl( `http://localhost:${appPort}/_next/image?url=%2Ftest.jpg&w=828&q=75`
browser, )
`http://localhost:${appPort}/_next/image?url=%2Ftest.jpg&w=828&q=75`
)
).toBe(true)
} finally { } finally {
if (browser) { if (browser) {
await browser.close() await browser.close()
......
...@@ -411,24 +411,11 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) { ...@@ -411,24 +411,11 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) {
} }
describe('404 handling', () => { describe('404 handling', () => {
let nextOpts = {}
beforeAll(async () => {
const hasLocalNext = fs.existsSync(join(appDir, 'node_modules/next'))
if (hasLocalNext) {
nextOpts = {
nextBin: join(appDir, 'node_modules/next/dist/bin/next'),
cwd: appDir,
}
console.log('Using next options', nextOpts)
}
})
const devStartAndExport = (isPages404) => { const devStartAndExport = (isPages404) => {
describe('next dev', () => { describe('next dev', () => {
beforeAll(async () => { beforeAll(async () => {
appPort = await findPort() appPort = await findPort()
app = await launchApp(appDir, appPort, nextOpts) app = await launchApp(appDir, appPort)
// prebuild pages // prebuild pages
await renderViaHTTP(appPort, '/') await renderViaHTTP(appPort, '/')
...@@ -446,21 +433,19 @@ describe('404 handling', () => { ...@@ -446,21 +433,19 @@ describe('404 handling', () => {
() => { () => {
describe('next start', () => { describe('next start', () => {
beforeAll(async () => { beforeAll(async () => {
await nextBuild(appDir, [], nextOpts) await nextBuild(appDir)
appPort = await findPort() appPort = await findPort()
app = await nextStart(appDir, appPort, nextOpts) app = await nextStart(appDir, appPort)
}) })
afterAll(() => killApp(app)) afterAll(() => killApp(app))
runTests({ runTests({
isPages404, isPages404,
}) })
}) })
describe('next export', () => { describe('next export', () => {
beforeAll(async () => { beforeAll(async () => {
nextConfig.write(`module.exports = { output: 'export' }`) nextConfig.write(`module.exports = { output: 'export' }`)
await nextBuild(appDir, [], nextOpts) await nextBuild(appDir)
app = await startStaticServer(outdir, join(outdir, '404.html')) app = await startStaticServer(outdir, join(outdir, '404.html'))
appPort = app.address().port appPort = app.address().port
}) })
...@@ -468,7 +453,6 @@ describe('404 handling', () => { ...@@ -468,7 +453,6 @@ describe('404 handling', () => {
await stopApp(app) await stopApp(app)
nextConfig.restore() nextConfig.restore()
}) })
runTests({ runTests({
isPages404, isPages404,
isExport: true, isExport: true,
...@@ -483,17 +467,14 @@ describe('404 handling', () => { ...@@ -483,17 +467,14 @@ describe('404 handling', () => {
}) })
describe('pages/404', () => { describe('pages/404', () => {
;(process.env.TURBOPACK_DEV ? describe.skip : describe)( const pagesErr = join(appDir, 'pages/_error.js')
'production mode', const pages404 = join(appDir, 'pages/404.js')
() => {
const pagesErr = join(appDir, 'pages/_error.js') beforeAll(async () => {
const pages404 = join(appDir, 'pages/404.js') await fs.move(pagesErr, pagesErr + '.bak')
await fs.writeFile(
beforeAll(async () => { pages404,
await fs.move(pagesErr, pagesErr + '.bak') `
await fs.writeFile(
pages404,
`
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
window.errorLoad = true window.errorLoad = true
} }
...@@ -501,16 +482,13 @@ describe('404 handling', () => { ...@@ -501,16 +482,13 @@ describe('404 handling', () => {
return <p id='error'>custom 404</p> return <p id='error'>custom 404</p>
} }
` `
) )
await nextBuild(appDir, [], nextOpts) })
}) afterAll(async () => {
afterAll(async () => { await fs.move(pagesErr + '.bak', pagesErr)
await fs.move(pagesErr + '.bak', pagesErr) await fs.remove(pages404)
await fs.remove(pages404) })
})
devStartAndExport(true) devStartAndExport(true)
}
)
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment