test: extract dynamic paths test from within other test! (#3860)

This commit is contained in:
Daniel Roe 2022-03-23 14:57:35 +00:00 committed by GitHub
parent a8317830a8
commit a0c4d6e7e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -180,59 +180,59 @@ describe('extends support', () => {
expect(html).toContain('Injected by extended middleware from bar') expect(html).toContain('Injected by extended middleware from bar')
}) })
}) })
})
describe('dynamic paths', () => { describe('dynamic paths', () => {
it('should work with no overrides', async () => { it('should work with no overrides', async () => {
const html = await $fetch('/assets') const html = await $fetch('/assets')
for (const match of html.matchAll(/(href|src)="(.*?)"/g)) { for (const match of html.matchAll(/(href|src)="(.*?)"/g)) {
const url = match[2] const url = match[2]
expect(url.startsWith('/_nuxt/') || url === '/public.svg').toBeTruthy() expect(url.startsWith('/_nuxt/') || url === '/public.svg').toBeTruthy()
} }
}) })
it('adds relative paths to CSS', async () => { it('adds relative paths to CSS', async () => {
const html = await $fetch('/assets') const html = await $fetch('/assets')
const urls = Array.from(html.matchAll(/(href|src)="(.*?)"/g)).map(m => m[2]) const urls = Array.from(html.matchAll(/(href|src)="(.*?)"/g)).map(m => m[2])
const cssURL = urls.find(u => /_nuxt\/entry.*\.css$/.test(u)) const cssURL = urls.find(u => /_nuxt\/entry.*\.css$/.test(u))
if (process.env.TEST_WITH_WEBPACK) { if (process.env.TEST_WITH_WEBPACK) {
// Webpack injects CSS differently // Webpack injects CSS differently
return return
} }
const css = await $fetch(cssURL) const css = await $fetch(cssURL)
const imageUrls = Array.from(css.matchAll(/url\(([^)]*)\)/g)).map(m => m[1].replace(/[-.][\w]{8}\./g, '.')) const imageUrls = Array.from(css.matchAll(/url\(([^)]*)\)/g)).map(m => m[1].replace(/[-.][\w]{8}\./g, '.'))
expect(imageUrls).toMatchInlineSnapshot(` expect(imageUrls).toMatchInlineSnapshot(`
[ [
"./logo.svg", "./logo.svg",
"../public.svg", "../public.svg",
] ]
`) `)
}) })
it('should allow setting base URL and build assets directory', async () => { it('should allow setting base URL and build assets directory', async () => {
process.env.NUXT_APP_BUILD_ASSETS_DIR = '/_other/' process.env.NUXT_APP_BUILD_ASSETS_DIR = '/_other/'
process.env.NUXT_APP_BASE_URL = '/foo/' process.env.NUXT_APP_BASE_URL = '/foo/'
await startServer() await startServer()
const html = await $fetch('/assets') const html = await $fetch('/assets')
for (const match of html.matchAll(/(href|src)="(.*?)"/g)) { for (const match of html.matchAll(/(href|src)="(.*?)"/g)) {
const url = match[2] const url = match[2]
// TODO: webpack does not yet support dynamic static paths // TODO: webpack does not yet support dynamic static paths
expect(url.startsWith('/foo/_other/') || url === '/foo/public.svg' || (process.env.TEST_WITH_WEBPACK && url === '/public.svg')).toBeTruthy() expect(url.startsWith('/foo/_other/') || url === '/foo/public.svg' || (process.env.TEST_WITH_WEBPACK && url === '/public.svg')).toBeTruthy()
} }
}) })
it('should allow setting CDN URL', async () => { it('should allow setting CDN URL', async () => {
process.env.NUXT_APP_BASE_URL = '/foo/' process.env.NUXT_APP_BASE_URL = '/foo/'
process.env.NUXT_APP_CDN_URL = 'https://example.com/' process.env.NUXT_APP_CDN_URL = 'https://example.com/'
process.env.NUXT_APP_BUILD_ASSETS_DIR = '/_cdn/' process.env.NUXT_APP_BUILD_ASSETS_DIR = '/_cdn/'
await startServer() await startServer()
const html = await $fetch('/assets') const html = await $fetch('/assets')
for (const match of html.matchAll(/(href|src)="(.*?)"/g)) { for (const match of html.matchAll(/(href|src)="(.*?)"/g)) {
const url = match[2] const url = match[2]
// TODO: webpack does not yet support dynamic static paths // TODO: webpack does not yet support dynamic static paths
expect(url.startsWith('https://example.com/_cdn/') || url === 'https://example.com/public.svg' || (process.env.TEST_WITH_WEBPACK && url === '/public.svg')).toBeTruthy() expect(url.startsWith('https://example.com/_cdn/') || url === 'https://example.com/public.svg' || (process.env.TEST_WITH_WEBPACK && url === '/public.svg')).toBeTruthy()
} }
})
}) })
}) })