Skip to content

Commit 08219a5

Browse files
committed
test: try to improve spa preloader tests
1 parent d545f9e commit 08219a5

File tree

6 files changed

+43
-27
lines changed

6 files changed

+43
-27
lines changed

docs/2.guide/3.going-further/3.modules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ And its test:
671671
```js [test/rendering.ts]
672672
import { describe, it, expect } from 'vitest'
673673
import { fileURLToPath } from 'node:url'
674-
import { setup, $fetch } from '@nuxt/test-utils'
674+
import { setup, $fetch } from '@nuxt/test-utils/e2e'
675675
676676
describe('ssr', async () => {
677677
// 2. Setup Nuxt with this fixture inside your test file

test/fixtures/spa-loader/app.vue

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<script setup lang="ts">
2-
await useAsyncData(async () => {
3-
await new Promise((r) => { setTimeout(r, 50) })
4-
return 42
5-
})
2+
if (import.meta.client) {
3+
await new Promise(resolve => setTimeout(resolve, 50))
4+
}
65
</script>
76

87
<template>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default defineNuxtPlugin(async () => {
2+
await new Promise(resolve => setTimeout(resolve, 50))
3+
})

test/spa-loader/spa-preloader-outside-disabled.test.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { fileURLToPath } from 'node:url'
22
import { describe, expect, it } from 'vitest'
33
import { isWindows } from 'std-env'
4-
import { $fetch, getBrowser, setup, url } from '@nuxt/test-utils'
4+
import { $fetch, createPage, setup, url } from '@nuxt/test-utils/e2e'
55

66
const isWebpack =
77
process.env.TEST_BUILDER === 'webpack' ||
@@ -27,20 +27,24 @@ await setup({
2727
describe('spaLoadingTemplateLocation flag is set to `within`', () => {
2828
it('should render loader inside appTag', async () => {
2929
const html = await $fetch<string>('/spa')
30-
expect(html).toContain(
31-
`<div id="__nuxt"><div data-testid="loader">loading...</div></div>`,
32-
)
30+
expect(html).toContain(`<div id="__nuxt"><div data-testid="loader">loading...</div></div>`)
3331
})
3432

3533
it('spa-loader does not appear while the app is mounting', async () => {
36-
const browser = await getBrowser()
37-
const page = await browser.newPage({})
34+
const page = await createPage()
3835
await page.goto(url('/spa'))
3936

4037
const loader = page.getByTestId('loader')
38+
const content = page.getByTestId('content')
39+
40+
await loader.waitFor({ state: 'visible' })
41+
expect(await content.isHidden()).toBeTruthy()
4142

4243
await page.waitForFunction(() => window.useNuxtApp?.() && window.useNuxtApp?.().isHydrating)
43-
expect(await loader.isHidden()).toBeTruthy()
44+
45+
expect(await content.isHidden()).toBeTruthy()
46+
47+
await content.waitFor({ state: 'visible' })
4448

4549
await page.close()
4650
}, 60_000)
Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { fileURLToPath } from 'node:url'
22
import { describe, expect, it } from 'vitest'
33
import { isWindows } from 'std-env'
4-
import { getBrowser, setup, url } from '@nuxt/test-utils'
4+
import { createPage, setup, url } from '@nuxt/test-utils/e2e'
5+
import type { Page } from 'playwright-core'
56

67
const isWebpack = process.env.TEST_BUILDER === 'webpack' || process.env.TEST_BUILDER === 'rspack'
78

@@ -22,31 +23,40 @@ await setup({
2223

2324
describe('spaLoadingTemplateLocation flag is set to `body`', () => {
2425
it('should render spa-loader', async () => {
25-
const browser = await getBrowser()
26-
const page = await browser.newPage({})
27-
await page.goto(url('/spa'))
28-
const loader = page.getByTestId('loader')
29-
expect(await loader.isVisible()).toBeTruthy()
26+
const page = await createPage()
27+
await page.goto(url('/spa'), { waitUntil: 'domcontentloaded' })
3028

29+
const loader = page.getByTestId('loader')
3130
const content = page.getByTestId('content')
31+
32+
await loader.waitFor({ state: 'visible' })
33+
expect(await content.isHidden()).toBeTruthy()
34+
3235
await content.waitFor({ state: 'visible' })
3336
expect(await loader.isHidden()).toBeTruthy()
3437

3538
await page.close()
3639
}, 60_000)
3740

3841
it('should render content without spa-loader', async () => {
39-
const browser = await getBrowser()
40-
const page = await browser.newPage({})
41-
await page.goto(url('/ssr'))
42+
const page = await createPage()
43+
await page.goto(url('/ssr'), { waitUntil: 'domcontentloaded' })
4244

43-
const loader = page.getByTestId('loader')
44-
expect(await loader.isHidden()).toBeTruthy()
45+
const [loaderIsHidden, contentIsHidden] = await getState(page)
4546

46-
const content = page.getByTestId('content')
47-
await content.waitFor({ state: 'visible' })
48-
expect(await loader.isHidden()).toBeTruthy()
47+
expect(loaderIsHidden).toBeTruthy()
48+
expect(contentIsHidden).toBeFalsy()
4949

5050
await page.close()
5151
}, 60_000)
5252
})
53+
54+
function getState (page: Page) {
55+
const loader = page.getByTestId('loader')
56+
const content = page.getByTestId('content')
57+
58+
return Promise.all([
59+
loader.isHidden(),
60+
content.isHidden(),
61+
])
62+
}

test/suspense.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { fileURLToPath } from 'node:url'
22
import { describe, expect, it } from 'vitest'
33
import { isWindows } from 'std-env'
4-
import { setup } from '@nuxt/test-utils'
4+
import { setup } from '@nuxt/test-utils/e2e'
55
import { renderPage } from './utils'
66

77
const isWebpack = process.env.TEST_BUILDER === 'webpack' || process.env.TEST_BUILDER === 'rspack'

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy