2022-04-15 15:19:05 +00:00
|
|
|
import { fileURLToPath } from 'node:url'
|
2022-02-18 18:14:57 +00:00
|
|
|
import { describe, expect, it } from 'vitest'
|
2022-11-24 12:24:14 +00:00
|
|
|
import { joinURL, withQuery } from 'ufo'
|
2022-09-12 08:56:04 +00:00
|
|
|
import { isWindows } from 'std-env'
|
2022-12-08 15:16:22 +00:00
|
|
|
import { normalize } from 'pathe'
|
2022-08-31 18:34:17 +00:00
|
|
|
// eslint-disable-next-line import/order
|
2022-11-24 12:24:14 +00:00
|
|
|
import { setup, fetch, $fetch, startServer, createPage, url } from '@nuxt/test-utils'
|
|
|
|
import type { NuxtIslandResponse } from '../packages/nuxt/src/core/runtime/nitro/renderer'
|
2022-10-08 14:18:57 +00:00
|
|
|
import { expectNoClientErrors, renderPage, withLogs } from './utils'
|
2022-02-18 18:14:57 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
await setup({
|
|
|
|
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
|
2022-04-05 18:38:23 +00:00
|
|
|
server: true,
|
2022-11-15 14:33:43 +00:00
|
|
|
browser: true,
|
|
|
|
setupTimeout: (isWindows ? 240 : 120) * 1000
|
2022-03-22 18:12:54 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('server api', () => {
|
|
|
|
it('should serialize', async () => {
|
|
|
|
expect(await $fetch('/api/hello')).toBe('Hello API')
|
|
|
|
expect(await $fetch('/api/hey')).toEqual({
|
|
|
|
foo: 'bar',
|
|
|
|
baz: 'qux'
|
2022-03-08 18:03:21 +00:00
|
|
|
})
|
2022-03-22 18:12:54 +00:00
|
|
|
})
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
it('should preserve states', async () => {
|
|
|
|
expect(await $fetch('/api/counter')).toEqual({ count: 0 })
|
|
|
|
expect(await $fetch('/api/counter')).toEqual({ count: 1 })
|
|
|
|
expect(await $fetch('/api/counter')).toEqual({ count: 2 })
|
|
|
|
expect(await $fetch('/api/counter')).toEqual({ count: 3 })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-10-11 16:03:52 +00:00
|
|
|
describe('route rules', () => {
|
|
|
|
it('should enable spa mode', async () => {
|
|
|
|
expect(await $fetch('/route-rules/spa')).toContain('serverRendered:false')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
describe('pages', () => {
|
|
|
|
it('render index', async () => {
|
|
|
|
const html = await $fetch('/')
|
|
|
|
|
|
|
|
// Snapshot
|
|
|
|
// expect(html).toMatchInlineSnapshot()
|
|
|
|
|
|
|
|
// should render text
|
|
|
|
expect(html).toContain('Hello Nuxt 3!')
|
|
|
|
// should inject runtime config
|
|
|
|
expect(html).toContain('RuntimeConfig | testConfig: 123')
|
|
|
|
// composables auto import
|
|
|
|
expect(html).toContain('Composable | foo: auto imported from ~/components/foo.ts')
|
|
|
|
expect(html).toContain('Composable | bar: auto imported from ~/components/useBar.ts')
|
2022-06-08 20:09:31 +00:00
|
|
|
expect(html).toContain('Composable | template: auto imported from ~/components/template.ts')
|
2022-03-22 18:12:54 +00:00
|
|
|
// should import components
|
|
|
|
expect(html).toContain('This is a custom component with a named export.')
|
2022-08-02 15:05:02 +00:00
|
|
|
// should apply attributes to client-only components
|
|
|
|
expect(html).toContain('<div style="color:red;" class="client-only"></div>')
|
2023-01-09 11:20:33 +00:00
|
|
|
// should render server-only components
|
|
|
|
expect(html).toContain('<div class="server-only" style="background-color:gray;"> server-only component </div>')
|
2022-08-02 15:05:02 +00:00
|
|
|
// should register global components automatically
|
2022-07-27 13:05:34 +00:00
|
|
|
expect(html).toContain('global component registered automatically')
|
|
|
|
expect(html).toContain('global component via suffix')
|
2022-04-05 18:38:23 +00:00
|
|
|
|
2022-04-07 00:39:44 +00:00
|
|
|
await expectNoClientErrors('/')
|
2022-03-08 18:03:21 +00:00
|
|
|
})
|
|
|
|
|
2022-09-22 13:54:34 +00:00
|
|
|
it('respects aliases in page metadata', async () => {
|
|
|
|
const html = await $fetch('/some-alias')
|
|
|
|
expect(html).toContain('Hello Nuxt 3!')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('respects redirects in page metadata', async () => {
|
|
|
|
const { headers } = await fetch('/redirect', { redirect: 'manual' })
|
|
|
|
expect(headers.get('location')).toEqual('/')
|
|
|
|
})
|
|
|
|
|
2022-10-10 10:18:20 +00:00
|
|
|
it('validates routes', async () => {
|
|
|
|
const { status } = await fetch('/forbidden')
|
|
|
|
expect(status).toEqual(404)
|
|
|
|
})
|
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
it('render 404', async () => {
|
|
|
|
const html = await $fetch('/not-found')
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
// Snapshot
|
|
|
|
// expect(html).toMatchInlineSnapshot()
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
expect(html).toContain('[...slug].vue')
|
|
|
|
expect(html).toContain('404 at not-found')
|
2022-04-05 18:38:23 +00:00
|
|
|
|
2023-01-19 19:37:07 +00:00
|
|
|
// Middleware still runs after validation: https://github.com/nuxt/nuxt/issues/15650
|
2023-01-14 00:23:20 +00:00
|
|
|
expect(html).toContain('Middleware ran: true')
|
|
|
|
|
2022-04-07 00:39:44 +00:00
|
|
|
await expectNoClientErrors('/not-found')
|
2022-03-22 18:12:54 +00:00
|
|
|
})
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-05-03 09:31:58 +00:00
|
|
|
it('preserves query', async () => {
|
|
|
|
const html = await $fetch('/?test=true')
|
|
|
|
|
|
|
|
// Snapshot
|
|
|
|
// expect(html).toMatchInlineSnapshot()
|
|
|
|
|
|
|
|
// should render text
|
|
|
|
expect(html).toContain('Path: /?test=true')
|
|
|
|
|
|
|
|
await expectNoClientErrors('/?test=true')
|
|
|
|
})
|
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
it('/nested/[foo]/[bar].vue', async () => {
|
|
|
|
const html = await $fetch('/nested/one/two')
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
// Snapshot
|
|
|
|
// expect(html).toMatchInlineSnapshot()
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
expect(html).toContain('nested/[foo]/[bar].vue')
|
|
|
|
expect(html).toContain('foo: one')
|
|
|
|
expect(html).toContain('bar: two')
|
|
|
|
})
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
it('/nested/[foo]/index.vue', async () => {
|
|
|
|
const html = await $fetch('/nested/foobar')
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
// TODO: should resolved to same entry
|
|
|
|
// const html2 = await $fetch('/nested/foobar/index')
|
|
|
|
// expect(html).toEqual(html2)
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
// Snapshot
|
|
|
|
// expect(html).toMatchInlineSnapshot()
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
expect(html).toContain('nested/[foo]/index.vue')
|
|
|
|
expect(html).toContain('foo: foobar')
|
2022-04-05 18:38:23 +00:00
|
|
|
|
2022-04-07 00:39:44 +00:00
|
|
|
await expectNoClientErrors('/nested/foobar')
|
2022-03-22 18:12:54 +00:00
|
|
|
})
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
it('/nested/[foo]/user-[group].vue', async () => {
|
|
|
|
const html = await $fetch('/nested/foobar/user-admin')
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
// Snapshot
|
|
|
|
// expect(html).toMatchInlineSnapshot()
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
expect(html).toContain('nested/[foo]/user-[group].vue')
|
|
|
|
expect(html).toContain('foo: foobar')
|
|
|
|
expect(html).toContain('group: admin')
|
2022-04-05 18:38:23 +00:00
|
|
|
|
2022-04-07 00:39:44 +00:00
|
|
|
await expectNoClientErrors('/nested/foobar/user-admin')
|
2022-03-22 18:12:54 +00:00
|
|
|
})
|
2022-03-25 11:55:05 +00:00
|
|
|
|
|
|
|
it('/parent', async () => {
|
|
|
|
const html = await $fetch('/parent')
|
|
|
|
expect(html).toContain('parent/index')
|
2022-04-05 18:38:23 +00:00
|
|
|
|
2022-04-07 00:39:44 +00:00
|
|
|
await expectNoClientErrors('/parent')
|
2022-03-25 11:55:05 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('/another-parent', async () => {
|
|
|
|
const html = await $fetch('/another-parent')
|
|
|
|
expect(html).toContain('another-parent/index')
|
2022-04-05 18:38:23 +00:00
|
|
|
|
2022-04-07 00:39:44 +00:00
|
|
|
await expectNoClientErrors('/another-parent')
|
2022-03-25 11:55:05 +00:00
|
|
|
})
|
2022-08-17 15:26:51 +00:00
|
|
|
|
|
|
|
it('/client-only-components', async () => {
|
|
|
|
const html = await $fetch('/client-only-components')
|
2022-10-03 14:14:55 +00:00
|
|
|
// ensure fallbacks with classes and arbitrary attributes are rendered
|
2022-08-17 15:26:51 +00:00
|
|
|
expect(html).toContain('<div class="client-only-script" foo="bar">')
|
|
|
|
expect(html).toContain('<div class="client-only-script-setup" foo="hello">')
|
2022-09-20 06:24:45 +00:00
|
|
|
expect(html).toContain('<div>Fallback</div>')
|
2022-10-03 14:14:55 +00:00
|
|
|
// ensure components are not rendered server-side
|
2022-09-20 06:24:45 +00:00
|
|
|
expect(html).not.toContain('Should not be server rendered')
|
2022-08-17 15:26:51 +00:00
|
|
|
|
|
|
|
await expectNoClientErrors('/client-only-components')
|
2022-10-03 14:14:55 +00:00
|
|
|
|
|
|
|
const page = await createPage('/client-only-components')
|
|
|
|
|
|
|
|
await page.waitForLoadState('networkidle')
|
|
|
|
|
|
|
|
const hiddenSelectors = [
|
|
|
|
'.string-stateful-should-be-hidden',
|
|
|
|
'.client-script-should-be-hidden',
|
|
|
|
'.string-stateful-script-should-be-hidden',
|
|
|
|
'.no-state-hidden'
|
|
|
|
]
|
|
|
|
const visibleSelectors = [
|
|
|
|
'.string-stateful',
|
|
|
|
'.string-stateful-script',
|
|
|
|
'.client-only-script',
|
|
|
|
'.client-only-script-setup',
|
|
|
|
'.no-state'
|
|
|
|
]
|
|
|
|
// ensure directives are correctly applied
|
|
|
|
await Promise.all(hiddenSelectors.map(selector => page.locator(selector).isHidden()))
|
|
|
|
.then(results => results.forEach(isHidden => expect(isHidden).toBeTruthy()))
|
|
|
|
// ensure hidden components are still rendered
|
|
|
|
await Promise.all(hiddenSelectors.map(selector => page.locator(selector).innerHTML()))
|
|
|
|
.then(results => results.forEach(innerHTML => expect(innerHTML).not.toBe('')))
|
|
|
|
|
|
|
|
// ensure single root node components are rendered once on client (should not be empty)
|
|
|
|
await Promise.all(visibleSelectors.map(selector => page.locator(selector).innerHTML()))
|
|
|
|
.then(results => results.forEach(innerHTML => expect(innerHTML).not.toBe('')))
|
|
|
|
|
|
|
|
// ensure multi-root-node is correctly rendered
|
|
|
|
expect(await page.locator('.multi-root-node-count').innerHTML()).toContain('0')
|
|
|
|
expect(await page.locator('.multi-root-node-button').innerHTML()).toContain('add 1 to count')
|
|
|
|
expect(await page.locator('.multi-root-node-script-count').innerHTML()).toContain('0')
|
|
|
|
expect(await page.locator('.multi-root-node-script-button').innerHTML()).toContain('add 1 to count')
|
|
|
|
|
|
|
|
// ensure components reactivity
|
|
|
|
await page.locator('.multi-root-node-button').click()
|
|
|
|
await page.locator('.multi-root-node-script-button').click()
|
|
|
|
await page.locator('.client-only-script button').click()
|
|
|
|
await page.locator('.client-only-script-setup button').click()
|
|
|
|
|
|
|
|
expect(await page.locator('.multi-root-node-count').innerHTML()).toContain('1')
|
|
|
|
expect(await page.locator('.multi-root-node-script-count').innerHTML()).toContain('1')
|
|
|
|
expect(await page.locator('.client-only-script-setup button').innerHTML()).toContain('1')
|
|
|
|
expect(await page.locator('.client-only-script button').innerHTML()).toContain('1')
|
|
|
|
|
|
|
|
// ensure components ref is working and reactive
|
|
|
|
await page.locator('button.test-ref-1').click()
|
|
|
|
await page.locator('button.test-ref-2').click()
|
|
|
|
await page.locator('button.test-ref-3').click()
|
|
|
|
await page.locator('button.test-ref-4').click()
|
|
|
|
expect(await page.locator('.client-only-script-setup button').innerHTML()).toContain('2')
|
|
|
|
expect(await page.locator('.client-only-script button').innerHTML()).toContain('2')
|
|
|
|
expect(await page.locator('.string-stateful-script').innerHTML()).toContain('1')
|
|
|
|
expect(await page.locator('.string-stateful').innerHTML()).toContain('1')
|
|
|
|
|
|
|
|
// ensure directives are reactive
|
|
|
|
await page.locator('button#show-all').click()
|
|
|
|
await Promise.all(hiddenSelectors.map(selector => page.locator(selector).isVisible()))
|
|
|
|
.then(results => results.forEach(isVisible => expect(isVisible).toBeTruthy()))
|
2022-08-17 15:26:51 +00:00
|
|
|
})
|
2022-10-11 15:26:03 +00:00
|
|
|
|
|
|
|
it('/client-only-explicit-import', async () => {
|
|
|
|
const html = await $fetch('/client-only-explicit-import')
|
|
|
|
|
|
|
|
// ensure fallbacks with classes and arbitrary attributes are rendered
|
|
|
|
expect(html).toContain('<div class="client-only-script" foo="bar">')
|
|
|
|
expect(html).toContain('<div class="lazy-client-only-script-setup" foo="hello">')
|
|
|
|
// ensure components are not rendered server-side
|
|
|
|
expect(html).not.toContain('client only script')
|
2022-10-16 09:03:52 +00:00
|
|
|
await expectNoClientErrors('/client-only-explicit-import')
|
2022-10-11 15:26:03 +00:00
|
|
|
})
|
2022-03-22 18:12:54 +00:00
|
|
|
})
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-04-05 14:02:29 +00:00
|
|
|
describe('head tags', () => {
|
|
|
|
it('should render tags', async () => {
|
2022-08-02 11:20:44 +00:00
|
|
|
const headHtml = await $fetch('/head')
|
2022-10-12 17:00:17 +00:00
|
|
|
|
2022-08-02 11:20:44 +00:00
|
|
|
expect(headHtml).toContain('<title>Using a dynamic component - Title Template Fn Change</title>')
|
|
|
|
expect(headHtml).not.toContain('<meta name="description" content="first">')
|
|
|
|
expect(headHtml).toContain('<meta charset="utf-16">')
|
2022-08-07 09:53:53 +00:00
|
|
|
expect(headHtml.match('meta charset').length).toEqual(1)
|
|
|
|
expect(headHtml).toContain('<meta name="viewport" content="width=1024, initial-scale=1">')
|
|
|
|
expect(headHtml.match('meta name="viewport"').length).toEqual(1)
|
2022-08-02 11:20:44 +00:00
|
|
|
expect(headHtml).not.toContain('<meta charset="utf-8">')
|
|
|
|
expect(headHtml).toContain('<meta name="description" content="overriding with an inline useHead call">')
|
|
|
|
expect(headHtml).toMatch(/<html[^>]*class="html-attrs-test"/)
|
|
|
|
expect(headHtml).toMatch(/<body[^>]*class="body-attrs-test"/)
|
2022-11-15 16:26:38 +00:00
|
|
|
expect(headHtml).toContain('<script src="https://a-body-appended-script.com"></script></body>')
|
2022-08-02 11:20:44 +00:00
|
|
|
|
|
|
|
const indexHtml = await $fetch('/')
|
2022-04-11 09:03:31 +00:00
|
|
|
// should render charset by default
|
2022-08-02 11:20:44 +00:00
|
|
|
expect(indexHtml).toContain('<meta charset="utf-8">')
|
2022-04-11 09:03:31 +00:00
|
|
|
// should render <Head> components
|
2022-08-02 11:20:44 +00:00
|
|
|
expect(indexHtml).toContain('<title>Basic fixture</title>')
|
2022-04-05 14:02:29 +00:00
|
|
|
})
|
2022-07-25 12:05:58 +00:00
|
|
|
|
2022-09-03 12:31:09 +00:00
|
|
|
it('should render http-equiv correctly', async () => {
|
|
|
|
const html = await $fetch('/head')
|
|
|
|
// http-equiv should be rendered kebab case
|
|
|
|
expect(html).toContain('<meta content="default-src https" http-equiv="content-security-policy">')
|
|
|
|
})
|
|
|
|
|
2022-07-25 12:05:58 +00:00
|
|
|
// TODO: Doesn't adds header in test environment
|
|
|
|
// it.todo('should render stylesheet link tag (SPA mode)', async () => {
|
|
|
|
// const html = await $fetch('/head', { headers: { 'x-nuxt-no-ssr': '1' } })
|
|
|
|
// expect(html).toMatch(/<link rel="stylesheet" href="\/_nuxt\/[^>]*.css"/)
|
|
|
|
// })
|
2022-04-05 14:02:29 +00:00
|
|
|
})
|
|
|
|
|
2022-08-29 10:02:24 +00:00
|
|
|
describe('legacy async data', () => {
|
|
|
|
it('should work with defineNuxtComponent', async () => {
|
|
|
|
const html = await $fetch('/legacy/async-data')
|
|
|
|
expect(html).toContain('<div>Hello API</div>')
|
|
|
|
expect(html).toContain('{hello:"Hello API"}')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
describe('navigate', () => {
|
|
|
|
it('should redirect to index with navigateTo', async () => {
|
2022-05-11 17:33:29 +00:00
|
|
|
const { headers } = await fetch('/navigate-to/', { redirect: 'manual' })
|
2022-03-22 18:12:54 +00:00
|
|
|
|
2022-05-11 17:33:29 +00:00
|
|
|
expect(headers.get('location')).toEqual('/')
|
2022-03-08 18:03:21 +00:00
|
|
|
})
|
2022-03-22 18:12:54 +00:00
|
|
|
})
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-09-08 08:45:39 +00:00
|
|
|
describe('errors', () => {
|
|
|
|
it('should render a JSON error page', async () => {
|
|
|
|
const res = await fetch('/error', {
|
|
|
|
headers: {
|
|
|
|
accept: 'application/json'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
expect(res.status).toBe(422)
|
|
|
|
const error = await res.json()
|
|
|
|
delete error.stack
|
|
|
|
expect(error).toMatchObject({
|
|
|
|
message: 'This is a custom error',
|
|
|
|
statusCode: 422,
|
|
|
|
statusMessage: 'This is a custom error',
|
|
|
|
url: '/error'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should render a HTML error page', async () => {
|
|
|
|
const res = await fetch('/error')
|
|
|
|
expect(await res.text()).toContain('This is a custom error')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-08-24 16:04:56 +00:00
|
|
|
describe('navigate external', () => {
|
|
|
|
it('should redirect to example.com', async () => {
|
|
|
|
const { headers } = await fetch('/navigate-to-external/', { redirect: 'manual' })
|
|
|
|
|
|
|
|
expect(headers.get('location')).toEqual('https://example.com/')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
describe('middlewares', () => {
|
|
|
|
it('should redirect to index with global middleware', async () => {
|
|
|
|
const html = await $fetch('/redirect/')
|
2022-03-16 21:39:47 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
// Snapshot
|
|
|
|
// expect(html).toMatchInlineSnapshot()
|
2022-03-16 21:39:47 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
expect(html).toContain('Hello Nuxt 3!')
|
2022-03-16 21:39:47 +00:00
|
|
|
})
|
|
|
|
|
2022-09-08 08:52:00 +00:00
|
|
|
it('should allow aborting navigation on server-side', async () => {
|
|
|
|
const res = await fetch('/?abort', {
|
|
|
|
headers: {
|
|
|
|
accept: 'application/json'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
expect(res.status).toEqual(401)
|
|
|
|
})
|
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
it('should inject auth', async () => {
|
|
|
|
const html = await $fetch('/auth')
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
// Snapshot
|
|
|
|
// expect(html).toMatchInlineSnapshot()
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
expect(html).toContain('auth.vue')
|
|
|
|
expect(html).toContain('auth: Injected by injectAuth middleware')
|
|
|
|
})
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
it('should not inject auth', async () => {
|
|
|
|
const html = await $fetch('/no-auth')
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
// Snapshot
|
|
|
|
// expect(html).toMatchInlineSnapshot()
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
expect(html).toContain('no-auth.vue')
|
|
|
|
expect(html).toContain('auth: ')
|
|
|
|
expect(html).not.toContain('Injected by injectAuth middleware')
|
|
|
|
})
|
2022-09-19 08:54:35 +00:00
|
|
|
|
|
|
|
it('should redirect to index with http 307 with navigateTo on server side', async () => {
|
|
|
|
const html = await fetch('/navigate-to-redirect', { redirect: 'manual' })
|
|
|
|
expect(html.headers.get('location')).toEqual('/')
|
|
|
|
expect(html.status).toEqual(307)
|
|
|
|
})
|
2022-03-22 18:12:54 +00:00
|
|
|
})
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-04-01 09:55:23 +00:00
|
|
|
describe('plugins', () => {
|
|
|
|
it('basic plugin', async () => {
|
|
|
|
const html = await $fetch('/plugins')
|
|
|
|
expect(html).toContain('myPlugin: Injected by my-plugin')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('async plugin', async () => {
|
|
|
|
const html = await $fetch('/plugins')
|
|
|
|
expect(html).toContain('asyncPlugin: Async plugin works! 123')
|
2022-12-10 22:44:29 +00:00
|
|
|
expect(html).toContain('useFetch works!')
|
2022-04-01 09:55:23 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
describe('layouts', () => {
|
|
|
|
it('should apply custom layout', async () => {
|
|
|
|
const html = await $fetch('/with-layout')
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
// Snapshot
|
|
|
|
// expect(html).toMatchInlineSnapshot()
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
expect(html).toContain('with-layout.vue')
|
|
|
|
expect(html).toContain('Custom Layout:')
|
2022-02-25 20:14:53 +00:00
|
|
|
})
|
2022-08-31 08:02:48 +00:00
|
|
|
it('should work with a dynamically set layout', async () => {
|
|
|
|
const html = await $fetch('/with-dynamic-layout')
|
|
|
|
|
|
|
|
// Snapshot
|
|
|
|
// expect(html).toMatchInlineSnapshot()
|
|
|
|
|
|
|
|
expect(html).toContain('with-dynamic-layout')
|
|
|
|
expect(html).toContain('Custom Layout:')
|
|
|
|
await expectNoClientErrors('/with-dynamic-layout')
|
|
|
|
})
|
2023-01-14 00:58:54 +00:00
|
|
|
it('should work with a computed layout', async () => {
|
|
|
|
const html = await $fetch('/with-computed-layout')
|
|
|
|
|
|
|
|
// Snapshot
|
|
|
|
// expect(html).toMatchInlineSnapshot()
|
|
|
|
|
|
|
|
expect(html).toContain('with-computed-layout')
|
|
|
|
expect(html).toContain('Custom Layout')
|
|
|
|
await expectNoClientErrors('/with-computed-layout')
|
|
|
|
})
|
2022-11-29 12:16:41 +00:00
|
|
|
it('should allow passing custom props to a layout', async () => {
|
|
|
|
const html = await $fetch('/layouts/with-props')
|
|
|
|
expect(html).toContain('some prop was passed')
|
|
|
|
await expectNoClientErrors('/layouts/with-props')
|
|
|
|
})
|
2022-03-22 18:12:54 +00:00
|
|
|
})
|
2022-02-25 20:14:53 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
describe('reactivity transform', () => {
|
|
|
|
it('should works', async () => {
|
|
|
|
const html = await $fetch('/')
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
expect(html).toContain('Sugar Counter 12 x 2 = 24')
|
|
|
|
})
|
|
|
|
})
|
2022-03-08 18:03:21 +00:00
|
|
|
|
2022-07-07 16:04:38 +00:00
|
|
|
describe('server tree shaking', () => {
|
|
|
|
it('should work', async () => {
|
|
|
|
const html = await $fetch('/client')
|
|
|
|
|
|
|
|
expect(html).toContain('This page should not crash when rendered')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
describe('extends support', () => {
|
2022-03-24 12:33:42 +00:00
|
|
|
describe('layouts & pages', () => {
|
|
|
|
it('extends foo/layouts/default & foo/pages/index', async () => {
|
2022-03-22 18:12:54 +00:00
|
|
|
const html = await $fetch('/foo')
|
2022-03-24 12:33:42 +00:00
|
|
|
expect(html).toContain('Extended layout from foo')
|
|
|
|
expect(html).toContain('Extended page from foo')
|
2022-03-22 18:12:54 +00:00
|
|
|
})
|
|
|
|
|
2022-03-24 12:33:42 +00:00
|
|
|
it('extends [bar/layouts/override & bar/pages/override] over [foo/layouts/override & foo/pages/override]', async () => {
|
2022-03-22 18:12:54 +00:00
|
|
|
const html = await $fetch('/override')
|
2022-03-24 12:33:42 +00:00
|
|
|
expect(html).toContain('Extended layout from bar')
|
2022-03-22 18:12:54 +00:00
|
|
|
expect(html).toContain('Extended page from bar')
|
2022-03-08 18:03:21 +00:00
|
|
|
})
|
2022-02-18 18:14:57 +00:00
|
|
|
})
|
2022-03-17 22:17:59 +00:00
|
|
|
|
2022-03-24 12:33:42 +00:00
|
|
|
describe('components', () => {
|
|
|
|
it('extends foo/components/ExtendsFoo', async () => {
|
|
|
|
const html = await $fetch('/foo')
|
|
|
|
expect(html).toContain('Extended component from foo')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('extends bar/components/ExtendsOverride over foo/components/ExtendsOverride', async () => {
|
|
|
|
const html = await $fetch('/override')
|
|
|
|
expect(html).toContain('Extended component from bar')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-03-22 18:12:54 +00:00
|
|
|
describe('middlewares', () => {
|
|
|
|
it('extends foo/middleware/foo', async () => {
|
2022-03-24 12:33:42 +00:00
|
|
|
const html = await $fetch('/foo')
|
|
|
|
expect(html).toContain('Middleware | foo: Injected by extended middleware from foo')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('extends bar/middleware/override over foo/middleware/override', async () => {
|
|
|
|
const html = await $fetch('/override')
|
|
|
|
expect(html).toContain('Middleware | override: Injected by extended middleware from bar')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('composables', () => {
|
|
|
|
it('extends foo/composables/foo', async () => {
|
|
|
|
const html = await $fetch('/foo')
|
|
|
|
expect(html).toContain('Composable | useExtendsFoo: foo')
|
|
|
|
})
|
2023-01-14 01:14:24 +00:00
|
|
|
it('allows overriding composables', async () => {
|
|
|
|
const html = await $fetch('/extends')
|
|
|
|
expect(html).toContain('test from project')
|
|
|
|
})
|
2022-03-24 12:33:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('plugins', () => {
|
|
|
|
it('extends foo/plugins/foo', async () => {
|
|
|
|
const html = await $fetch('/foo')
|
|
|
|
expect(html).toContain('Plugin | foo: String generated from foo plugin!')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('server', () => {
|
|
|
|
it('extends foo/server/api/foo', async () => {
|
|
|
|
expect(await $fetch('/api/foo')).toBe('foo')
|
2022-03-22 18:12:54 +00:00
|
|
|
})
|
2022-03-17 22:17:59 +00:00
|
|
|
|
2022-03-24 12:33:42 +00:00
|
|
|
it('extends foo/server/middleware/foo', async () => {
|
|
|
|
const { headers } = await fetch('/')
|
|
|
|
expect(headers.get('injected-header')).toEqual('foo')
|
2022-03-17 22:17:59 +00:00
|
|
|
})
|
|
|
|
})
|
2022-04-04 08:23:11 +00:00
|
|
|
|
|
|
|
describe('app', () => {
|
|
|
|
it('extends foo/app/router.options & bar/app/router.options', async () => {
|
|
|
|
const html: string = await $fetch('/')
|
2022-08-26 15:47:29 +00:00
|
|
|
const routerLinkClasses = html.match(/href="\/" class="([^"]*)"/)?.[1].split(' ')
|
2022-04-04 08:23:11 +00:00
|
|
|
expect(routerLinkClasses).toContain('foo-active-class')
|
|
|
|
expect(routerLinkClasses).toContain('bar-exact-active-class')
|
|
|
|
})
|
|
|
|
})
|
2022-03-23 14:57:35 +00:00
|
|
|
})
|
2022-03-22 15:51:26 +00:00
|
|
|
|
2022-10-08 14:18:57 +00:00
|
|
|
// Bug #7337
|
|
|
|
describe('deferred app suspense resolve', () => {
|
|
|
|
async function behaviour (path: string) {
|
|
|
|
await withLogs(async (page, logs) => {
|
|
|
|
await page.goto(url(path))
|
|
|
|
await page.waitForLoadState('networkidle')
|
|
|
|
|
|
|
|
// Wait for all pending micro ticks to be cleared in case hydration haven't finished yet.
|
|
|
|
await page.evaluate(() => new Promise(resolve => setTimeout(resolve, 0)))
|
|
|
|
|
|
|
|
const hydrationLogs = logs.filter(log => log.includes('isHydrating'))
|
|
|
|
expect(hydrationLogs.length).toBe(3)
|
|
|
|
expect(hydrationLogs.every(log => log === 'isHydrating: true'))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
it('should wait for all suspense instance on initial hydration', async () => {
|
|
|
|
await behaviour('/async-parent/child')
|
|
|
|
})
|
|
|
|
it('should wait for all suspense instance on initial hydration', async () => {
|
|
|
|
await behaviour('/internal-layout/async-parent/child')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// Bug #6592
|
|
|
|
describe('page key', () => {
|
|
|
|
it('should not cause run of setup if navigation not change page key and layout', async () => {
|
|
|
|
async function behaviour (path: string) {
|
|
|
|
await withLogs(async (page, logs) => {
|
|
|
|
await page.goto(url(`${path}/0`))
|
|
|
|
await page.waitForLoadState('networkidle')
|
|
|
|
|
|
|
|
await page.click(`[href="${path}/1"]`)
|
|
|
|
await page.waitForSelector('#page-1')
|
|
|
|
|
|
|
|
// Wait for all pending micro ticks to be cleared,
|
|
|
|
// so we are not resolved too early when there are repeated page loading
|
|
|
|
await page.evaluate(() => new Promise(resolve => setTimeout(resolve, 0)))
|
|
|
|
|
|
|
|
expect(logs.filter(l => l.includes('Child Setup')).length).toBe(1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
await behaviour('/fixed-keyed-child-parent')
|
|
|
|
await behaviour('/internal-layout/fixed-keyed-child-parent')
|
|
|
|
})
|
|
|
|
it('will cause run of setup if navigation changed page key', async () => {
|
|
|
|
async function behaviour (path: string) {
|
|
|
|
await withLogs(async (page, logs) => {
|
|
|
|
await page.goto(url(`${path}/0`))
|
|
|
|
await page.waitForLoadState('networkidle')
|
|
|
|
|
|
|
|
await page.click(`[href="${path}/1"]`)
|
|
|
|
await page.waitForSelector('#page-1')
|
|
|
|
|
|
|
|
// Wait for all pending micro ticks to be cleared,
|
|
|
|
// so we are not resolved too early when there are repeated page loading
|
|
|
|
await page.evaluate(() => new Promise(resolve => setTimeout(resolve, 0)))
|
|
|
|
|
|
|
|
expect(logs.filter(l => l.includes('Child Setup')).length).toBe(2)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
await behaviour('/keyed-child-parent')
|
|
|
|
await behaviour('/internal-layout/keyed-child-parent')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// Bug #6592
|
|
|
|
describe('layout change not load page twice', () => {
|
|
|
|
async function behaviour (path1: string, path2: string) {
|
|
|
|
await withLogs(async (page, logs) => {
|
|
|
|
await page.goto(url(path1))
|
|
|
|
await page.waitForLoadState('networkidle')
|
|
|
|
await page.click(`[href="${path2}"]`)
|
|
|
|
await page.waitForSelector('#with-layout2')
|
|
|
|
|
|
|
|
// Wait for all pending micro ticks to be cleared,
|
|
|
|
// so we are not resolved too early when there are repeated page loading
|
|
|
|
await page.evaluate(() => new Promise(resolve => setTimeout(resolve, 0)))
|
|
|
|
|
|
|
|
expect(logs.filter(l => l.includes('Layout2 Page Setup')).length).toBe(1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
it('should not cause run of page setup to repeat if layout changed', async () => {
|
|
|
|
await behaviour('/with-layout', '/with-layout2')
|
|
|
|
await behaviour('/internal-layout/with-layout', '/internal-layout/with-layout2')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-07-07 16:26:04 +00:00
|
|
|
describe('automatically keyed composables', () => {
|
|
|
|
it('should automatically generate keys', async () => {
|
|
|
|
const html = await $fetch('/keyed-composables')
|
|
|
|
expect(html).toContain('true')
|
|
|
|
expect(html).not.toContain('false')
|
|
|
|
})
|
2022-08-05 11:02:20 +00:00
|
|
|
it('should match server-generated keys', async () => {
|
|
|
|
await expectNoClientErrors('/keyed-composables')
|
|
|
|
})
|
2022-07-07 16:26:04 +00:00
|
|
|
})
|
|
|
|
|
2022-09-07 09:55:03 +00:00
|
|
|
describe.skipIf(process.env.NUXT_TEST_DEV || process.env.TEST_WITH_WEBPACK)('inlining component styles', () => {
|
|
|
|
it('should inline styles', async () => {
|
|
|
|
const html = await $fetch('/styles')
|
|
|
|
for (const style of [
|
|
|
|
'{--assets:"assets"}', // <script>
|
|
|
|
'{--scoped:"scoped"}', // <style lang=css>
|
2022-11-03 19:17:43 +00:00
|
|
|
'{--postcss:"postcss"}' // <style lang=postcss>
|
2022-09-07 09:55:03 +00:00
|
|
|
]) {
|
|
|
|
expect(html).toContain(style)
|
|
|
|
}
|
|
|
|
})
|
2022-09-07 08:41:08 +00:00
|
|
|
|
2022-11-03 19:17:43 +00:00
|
|
|
it('does not load stylesheet for page styles', async () => {
|
2022-09-07 09:55:03 +00:00
|
|
|
const html: string = await $fetch('/styles')
|
2022-11-02 10:28:41 +00:00
|
|
|
expect(html.match(/<link [^>]*href="[^"]*\.css">/g)?.filter(m => m.includes('entry'))?.map(m => m.replace(/\.[^.]*\.css/, '.css'))).toMatchInlineSnapshot(`
|
|
|
|
[
|
2022-11-03 19:17:43 +00:00
|
|
|
"<link rel=\\"preload\\" as=\\"style\\" href=\\"/_nuxt/entry.css\\">",
|
|
|
|
"<link rel=\\"stylesheet\\" href=\\"/_nuxt/entry.css\\">",
|
2022-11-02 10:28:41 +00:00
|
|
|
]
|
|
|
|
`)
|
2022-09-07 09:55:03 +00:00
|
|
|
})
|
2022-09-07 08:41:08 +00:00
|
|
|
|
2022-10-18 16:13:50 +00:00
|
|
|
it('still downloads client-only styles', async () => {
|
2022-09-07 09:55:03 +00:00
|
|
|
const page = await createPage('/styles')
|
|
|
|
await page.waitForLoadState('networkidle')
|
|
|
|
expect(await page.$eval('.client-only-css', e => getComputedStyle(e).color)).toBe('rgb(50, 50, 50)')
|
|
|
|
})
|
2022-09-07 08:41:08 +00:00
|
|
|
|
2022-09-07 09:55:03 +00:00
|
|
|
it.todo('renders client-only styles only', async () => {
|
|
|
|
const html = await $fetch('/styles')
|
|
|
|
expect(html).toContain('{--client-only:"client-only"}')
|
2022-09-03 13:03:30 +00:00
|
|
|
})
|
2022-09-07 09:55:03 +00:00
|
|
|
})
|
2022-09-03 13:03:30 +00:00
|
|
|
|
2022-08-23 19:12:22 +00:00
|
|
|
describe('prefetching', () => {
|
|
|
|
it('should prefetch components', async () => {
|
|
|
|
await expectNoClientErrors('/prefetch/components')
|
|
|
|
})
|
2022-08-30 14:41:11 +00:00
|
|
|
it('should not prefetch certain dynamic imports by default', async () => {
|
|
|
|
const html = await $fetch('/auth')
|
|
|
|
// should not prefetch global components
|
|
|
|
expect(html).not.toMatch(/<link [^>]*\/_nuxt\/TestGlobal[^>]*\.js"/)
|
|
|
|
// should not prefetch all other pages
|
|
|
|
expect(html).not.toMatch(/<link [^>]*\/_nuxt\/navigate-to[^>]*\.js"/)
|
|
|
|
})
|
2022-08-23 19:12:22 +00:00
|
|
|
})
|
|
|
|
|
2022-09-07 09:55:03 +00:00
|
|
|
describe.runIf(process.env.NUXT_TEST_DEV)('detecting invalid root nodes', () => {
|
|
|
|
it('should detect invalid root nodes in pages', async () => {
|
|
|
|
for (const path of ['1', '2', '3', '4']) {
|
|
|
|
const { consoleLogs } = await renderPage(joinURL('/invalid-root', path))
|
|
|
|
const consoleLogsWarns = consoleLogs.filter(i => i.type === 'warning').map(w => w.text).join('\n')
|
|
|
|
expect(consoleLogsWarns).toContain('does not have a single root node and will cause errors when navigating between routes')
|
|
|
|
}
|
|
|
|
})
|
2022-08-23 10:25:48 +00:00
|
|
|
|
2022-09-07 09:55:03 +00:00
|
|
|
it('should not complain if there is no transition', async () => {
|
|
|
|
for (const path of ['fine']) {
|
|
|
|
const { consoleLogs } = await renderPage(joinURL('/invalid-root', path))
|
2022-08-23 10:25:48 +00:00
|
|
|
|
2022-09-07 09:55:03 +00:00
|
|
|
const consoleLogsWarns = consoleLogs.filter(i => i.type === 'warning')
|
2022-08-23 10:25:48 +00:00
|
|
|
|
2022-09-07 09:55:03 +00:00
|
|
|
expect(consoleLogsWarns.length).toEqual(0)
|
|
|
|
}
|
2022-08-23 10:25:48 +00:00
|
|
|
})
|
2022-09-07 09:55:03 +00:00
|
|
|
})
|
2022-08-23 10:25:48 +00:00
|
|
|
|
2022-09-07 10:41:25 +00:00
|
|
|
// TODO: dynamic paths in dev
|
|
|
|
describe.skipIf(process.env.NUXT_TEST_DEV)('dynamic paths', () => {
|
2022-03-23 14:57:35 +00:00
|
|
|
it('should work with no overrides', async () => {
|
2022-08-26 15:47:29 +00:00
|
|
|
const html: string = await $fetch('/assets')
|
2022-09-03 13:03:30 +00:00
|
|
|
for (const match of html.matchAll(/(href|src)="(.*?)"|url\(([^)]*?)\)/g)) {
|
|
|
|
const url = match[2] || match[3]
|
2022-03-23 14:57:35 +00:00
|
|
|
expect(url.startsWith('/_nuxt/') || url === '/public.svg').toBeTruthy()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-01-13 15:00:57 +00:00
|
|
|
// webpack injects CSS differently
|
2022-09-07 09:55:03 +00:00
|
|
|
it.skipIf(process.env.TEST_WITH_WEBPACK)('adds relative paths to CSS', async () => {
|
2022-08-26 15:47:29 +00:00
|
|
|
const html: string = await $fetch('/assets')
|
2022-09-03 13:03:30 +00:00
|
|
|
const urls = Array.from(html.matchAll(/(href|src)="(.*?)"|url\(([^)]*?)\)/g)).map(m => m[2] || m[3])
|
2022-08-05 16:35:38 +00:00
|
|
|
const cssURL = urls.find(u => /_nuxt\/assets.*\.css$/.test(u))
|
2022-04-07 19:15:30 +00:00
|
|
|
expect(cssURL).toBeDefined()
|
2022-08-26 15:47:29 +00:00
|
|
|
const css: string = await $fetch(cssURL!)
|
2022-03-23 14:57:35 +00:00
|
|
|
const imageUrls = Array.from(css.matchAll(/url\(([^)]*)\)/g)).map(m => m[1].replace(/[-.][\w]{8}\./g, '.'))
|
|
|
|
expect(imageUrls).toMatchInlineSnapshot(`
|
2022-03-22 15:51:26 +00:00
|
|
|
[
|
|
|
|
"./logo.svg",
|
|
|
|
"../public.svg",
|
2022-06-10 15:17:11 +00:00
|
|
|
"../public.svg",
|
|
|
|
"../public.svg",
|
2022-03-22 15:51:26 +00:00
|
|
|
]
|
|
|
|
`)
|
2022-03-23 14:57:35 +00:00
|
|
|
})
|
2022-03-22 15:51:26 +00:00
|
|
|
|
2022-03-23 14:57:35 +00:00
|
|
|
it('should allow setting base URL and build assets directory', async () => {
|
|
|
|
process.env.NUXT_APP_BUILD_ASSETS_DIR = '/_other/'
|
|
|
|
process.env.NUXT_APP_BASE_URL = '/foo/'
|
|
|
|
await startServer()
|
2022-03-22 15:51:26 +00:00
|
|
|
|
2022-04-07 11:28:04 +00:00
|
|
|
const html = await $fetch('/foo/assets')
|
2022-09-03 13:03:30 +00:00
|
|
|
for (const match of html.matchAll(/(href|src)="(.*?)"|url\(([^)]*?)\)/g)) {
|
|
|
|
const url = match[2] || match[3]
|
2022-07-25 09:52:21 +00:00
|
|
|
expect(
|
|
|
|
url.startsWith('/foo/_other/') ||
|
|
|
|
url === '/foo/public.svg' ||
|
|
|
|
// TODO: webpack does not yet support dynamic static paths
|
|
|
|
(process.env.TEST_WITH_WEBPACK && url === '/public.svg')
|
|
|
|
).toBeTruthy()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should allow setting relative baseURL', async () => {
|
|
|
|
delete process.env.NUXT_APP_BUILD_ASSETS_DIR
|
|
|
|
process.env.NUXT_APP_BASE_URL = './'
|
|
|
|
await startServer()
|
|
|
|
|
|
|
|
const html = await $fetch('/assets')
|
2022-09-03 13:03:30 +00:00
|
|
|
for (const match of html.matchAll(/(href|src)="(.*?)"|url\(([^)]*?)\)/g)) {
|
|
|
|
const url = match[2] || match[3]
|
2022-07-25 09:52:21 +00:00
|
|
|
expect(
|
|
|
|
url.startsWith('./_nuxt/') ||
|
|
|
|
url === './public.svg' ||
|
|
|
|
// TODO: webpack does not yet support dynamic static paths
|
|
|
|
(process.env.TEST_WITH_WEBPACK && url === '/public.svg')
|
|
|
|
).toBeTruthy()
|
|
|
|
expect(url.startsWith('./_nuxt/_nuxt')).toBeFalsy()
|
2022-03-23 14:57:35 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-05-11 17:33:29 +00:00
|
|
|
it('should use baseURL when redirecting', async () => {
|
|
|
|
process.env.NUXT_APP_BUILD_ASSETS_DIR = '/_other/'
|
|
|
|
process.env.NUXT_APP_BASE_URL = '/foo/'
|
|
|
|
await startServer()
|
|
|
|
const { headers } = await fetch('/foo/navigate-to/', { redirect: 'manual' })
|
|
|
|
|
|
|
|
expect(headers.get('location')).toEqual('/foo/')
|
|
|
|
})
|
|
|
|
|
2022-03-23 14:57:35 +00:00
|
|
|
it('should allow setting CDN URL', async () => {
|
|
|
|
process.env.NUXT_APP_BASE_URL = '/foo/'
|
|
|
|
process.env.NUXT_APP_CDN_URL = 'https://example.com/'
|
|
|
|
process.env.NUXT_APP_BUILD_ASSETS_DIR = '/_cdn/'
|
|
|
|
await startServer()
|
|
|
|
|
2022-04-07 11:28:04 +00:00
|
|
|
const html = await $fetch('/foo/assets')
|
2022-09-03 13:03:30 +00:00
|
|
|
for (const match of html.matchAll(/(href|src)="(.*?)"|url\(([^)]*?)\)/g)) {
|
|
|
|
const url = match[2] || match[3]
|
2022-07-25 09:52:21 +00:00
|
|
|
expect(
|
|
|
|
url.startsWith('https://example.com/_cdn/') ||
|
|
|
|
url === 'https://example.com/public.svg' ||
|
|
|
|
// TODO: webpack does not yet support dynamic static paths
|
|
|
|
(process.env.TEST_WITH_WEBPACK && url === '/public.svg')
|
|
|
|
).toBeTruthy()
|
2022-03-23 14:57:35 +00:00
|
|
|
}
|
2022-03-22 15:51:26 +00:00
|
|
|
})
|
2022-08-17 15:23:13 +00:00
|
|
|
|
|
|
|
it('restore server', async () => {
|
|
|
|
process.env.NUXT_APP_BASE_URL = undefined
|
|
|
|
process.env.NUXT_APP_CDN_URL = undefined
|
|
|
|
process.env.NUXT_APP_BUILD_ASSETS_DIR = undefined
|
|
|
|
await startServer()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('app config', () => {
|
|
|
|
it('should work', async () => {
|
|
|
|
const html = await $fetch('/app-config')
|
|
|
|
|
|
|
|
const expectedAppConfig = {
|
|
|
|
fromNuxtConfig: true,
|
|
|
|
nested: {
|
|
|
|
val: 2
|
|
|
|
},
|
|
|
|
fromLayer: true,
|
|
|
|
userConfig: 123
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(html).toContain(JSON.stringify(expectedAppConfig))
|
|
|
|
})
|
2022-02-18 18:14:57 +00:00
|
|
|
})
|
2022-08-30 10:34:09 +00:00
|
|
|
|
2022-11-24 12:24:14 +00:00
|
|
|
describe('component islands', () => {
|
|
|
|
it('renders components with route', async () => {
|
|
|
|
const result: NuxtIslandResponse = await $fetch('/__nuxt_island/RouteComponent?url=/foo')
|
|
|
|
|
|
|
|
if (process.env.NUXT_TEST_DEV) {
|
|
|
|
result.head.link = result.head.link.filter(l => !l.href.includes('@nuxt+ui-templates'))
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(result).toMatchInlineSnapshot(`
|
|
|
|
{
|
|
|
|
"head": {
|
|
|
|
"link": [],
|
|
|
|
"style": [],
|
|
|
|
},
|
|
|
|
"html": "<pre> Route: /foo
|
|
|
|
</pre>",
|
|
|
|
"state": {},
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('renders pure components', async () => {
|
|
|
|
const result: NuxtIslandResponse = await $fetch(withQuery('/__nuxt_island/PureComponent', {
|
|
|
|
props: JSON.stringify({
|
|
|
|
bool: false,
|
|
|
|
number: 3487,
|
|
|
|
str: 'something',
|
|
|
|
obj: { foo: 42, bar: false, me: 'hi' }
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
|
|
|
|
if (process.env.NUXT_TEST_DEV) {
|
|
|
|
result.head.link = result.head.link.filter(l => !l.href.includes('@nuxt+ui-templates'))
|
2022-12-08 15:16:22 +00:00
|
|
|
const fixtureDir = normalize(fileURLToPath(new URL('./fixtures/basic', import.meta.url)))
|
|
|
|
for (const link of result.head.link) {
|
|
|
|
link.href = link.href.replace(fixtureDir, '/<rootDir>').replaceAll('//', '/')
|
|
|
|
link.key = link.key.replace(/-[a-zA-Z0-9]+$/, '')
|
|
|
|
}
|
2022-11-24 12:24:14 +00:00
|
|
|
}
|
|
|
|
result.head.style = result.head.style.map(s => ({
|
|
|
|
...s,
|
|
|
|
innerHTML: (s.innerHTML || '').replace(/data-v-[a-z0-9]+/, 'data-v-xxxxx'),
|
|
|
|
key: s.key.replace(/-[a-zA-Z0-9]+$/, '')
|
|
|
|
}))
|
|
|
|
|
|
|
|
if (!(process.env.NUXT_TEST_DEV || process.env.TEST_WITH_WEBPACK)) {
|
|
|
|
expect(result.head).toMatchInlineSnapshot(`
|
|
|
|
{
|
|
|
|
"link": [],
|
|
|
|
"style": [
|
|
|
|
{
|
|
|
|
"innerHTML": "pre[data-v-xxxxx]{color:blue}",
|
|
|
|
"key": "island-style",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
} else if (process.env.NUXT_TEST_DEV) {
|
|
|
|
expect(result.head).toMatchInlineSnapshot(`
|
|
|
|
{
|
|
|
|
"link": [
|
|
|
|
{
|
|
|
|
"href": "/_nuxt/components/islands/PureComponent.vue?vue&type=style&index=0&scoped=c0c0cf89&lang.css",
|
2022-12-08 15:16:22 +00:00
|
|
|
"key": "island-link",
|
|
|
|
"rel": "stylesheet",
|
|
|
|
},
|
2022-11-24 12:24:14 +00:00
|
|
|
],
|
|
|
|
"style": [],
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(result.html.replace(/data-v-\w+|"|<!--.*-->/g, '')).toMatchInlineSnapshot(`
|
|
|
|
"<div > Was router enabled: true <br > Props: <pre >{
|
|
|
|
number: 3487,
|
|
|
|
str: something,
|
|
|
|
obj: {
|
|
|
|
foo: 42,
|
|
|
|
bar: false,
|
|
|
|
me: hi
|
|
|
|
},
|
|
|
|
bool: false
|
|
|
|
}</pre></div>"
|
|
|
|
`)
|
|
|
|
|
|
|
|
expect(result.state).toMatchInlineSnapshot(`
|
|
|
|
{
|
|
|
|
"$shasRouter": true,
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-01-16 16:04:16 +00:00
|
|
|
describe.runIf(process.env.NUXT_TEST_DEV && !process.env.TEST_WITH_WEBPACK)('vite plugins', () => {
|
|
|
|
it('does not override vite plugins', async () => {
|
|
|
|
expect(await $fetch('/vite-plugin-without-path')).toBe('vite-plugin without path')
|
|
|
|
expect(await $fetch('/__nuxt-test')).toBe('vite-plugin with __nuxt prefix')
|
|
|
|
})
|
|
|
|
it('does not allow direct access to nuxt source folder', async () => {
|
|
|
|
expect(await $fetch('/app.config')).toContain('404')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-09-12 08:56:04 +00:00
|
|
|
describe.skipIf(process.env.NUXT_TEST_DEV || isWindows)('payload rendering', () => {
|
2022-09-10 13:57:16 +00:00
|
|
|
it('renders a payload', async () => {
|
|
|
|
const payload = await $fetch('/random/a/_payload.js', { responseType: 'text' })
|
|
|
|
expect(payload).toMatch(
|
2023-01-20 12:10:58 +00:00
|
|
|
/export default \{data:\{hey:\{[^}]*\},rand_a:\[[^\]]*\],".*":\{html:".*server-only component.*",head:\{link:\[\],style:\[\]\}\}\},prerenderedAt:\d*\}/
|
2022-09-10 13:57:16 +00:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not fetch a prefetched payload', async () => {
|
|
|
|
const page = await createPage()
|
|
|
|
const requests = [] as string[]
|
2022-09-13 20:20:23 +00:00
|
|
|
|
2022-09-10 13:57:16 +00:00
|
|
|
page.on('request', (req) => {
|
|
|
|
requests.push(req.url().replace(url('/'), '/'))
|
|
|
|
})
|
2022-09-13 20:20:23 +00:00
|
|
|
|
2022-09-10 13:57:16 +00:00
|
|
|
await page.goto(url('/random/a'))
|
|
|
|
await page.waitForLoadState('networkidle')
|
|
|
|
|
|
|
|
const importSuffix = process.env.NUXT_TEST_DEV && !process.env.TEST_WITH_WEBPACK ? '?import' : ''
|
|
|
|
|
|
|
|
// We are manually prefetching other payloads
|
|
|
|
expect(requests).toContain('/random/c/_payload.js')
|
|
|
|
|
|
|
|
// We are not triggering API requests in the payload
|
|
|
|
expect(requests).not.toContain(expect.stringContaining('/api/random'))
|
2023-01-20 12:10:58 +00:00
|
|
|
expect(requests).not.toContain(expect.stringContaining('/__nuxt_island'))
|
2022-09-13 20:20:23 +00:00
|
|
|
// requests.length = 0
|
2022-09-10 13:57:16 +00:00
|
|
|
|
|
|
|
await page.click('[href="/random/b"]')
|
|
|
|
await page.waitForLoadState('networkidle')
|
2022-09-13 20:20:23 +00:00
|
|
|
|
2022-09-10 13:57:16 +00:00
|
|
|
// We are not triggering API requests in the payload in client-side nav
|
|
|
|
expect(requests).not.toContain('/api/random')
|
2023-01-20 12:10:58 +00:00
|
|
|
expect(requests).not.toContain(expect.stringContaining('/__nuxt_island'))
|
2022-09-13 20:20:23 +00:00
|
|
|
|
2022-09-10 13:57:16 +00:00
|
|
|
// We are fetching a payload we did not prefetch
|
|
|
|
expect(requests).toContain('/random/b/_payload.js' + importSuffix)
|
2022-09-13 20:20:23 +00:00
|
|
|
|
2022-09-10 13:57:16 +00:00
|
|
|
// We are not refetching payloads we've already prefetched
|
2022-09-13 20:20:23 +00:00
|
|
|
// expect(requests.filter(p => p.includes('_payload')).length).toBe(1)
|
|
|
|
// requests.length = 0
|
2022-09-10 13:57:16 +00:00
|
|
|
|
|
|
|
await page.click('[href="/random/c"]')
|
|
|
|
await page.waitForLoadState('networkidle')
|
2022-09-13 20:20:23 +00:00
|
|
|
|
2022-09-10 13:57:16 +00:00
|
|
|
// We are not triggering API requests in the payload in client-side nav
|
|
|
|
expect(requests).not.toContain('/api/random')
|
2023-01-20 12:10:58 +00:00
|
|
|
expect(requests).not.toContain(expect.stringContaining('/__nuxt_island'))
|
2022-09-13 20:20:23 +00:00
|
|
|
|
2022-09-10 13:57:16 +00:00
|
|
|
// We are not refetching payloads we've already prefetched
|
|
|
|
// Note: we refetch on dev as urls differ between '' and '?import'
|
2022-09-13 20:20:23 +00:00
|
|
|
// expect(requests.filter(p => p.includes('_payload')).length).toBe(process.env.NUXT_TEST_DEV ? 1 : 0)
|
2022-09-10 13:57:16 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-09-14 15:11:00 +00:00
|
|
|
describe.skipIf(isWindows)('useAsyncData', () => {
|
2022-08-30 10:34:09 +00:00
|
|
|
it('single request resolves', async () => {
|
|
|
|
await expectNoClientErrors('/useAsyncData/single')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('two requests resolve', async () => {
|
|
|
|
await expectNoClientErrors('/useAsyncData/double')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('two requests resolve and sync', async () => {
|
|
|
|
await $fetch('/useAsyncData/refresh')
|
|
|
|
})
|
|
|
|
|
2022-10-10 10:33:16 +00:00
|
|
|
it('requests can be cancelled/overridden', async () => {
|
|
|
|
await expectNoClientErrors('/useAsyncData/override')
|
|
|
|
})
|
|
|
|
|
2022-08-30 10:34:09 +00:00
|
|
|
it('two requests made at once resolve and sync', async () => {
|
|
|
|
await expectNoClientErrors('/useAsyncData/promise-all')
|
|
|
|
})
|
|
|
|
})
|