nuxt-spec 0.3.4 → 0.4.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +13 -13
- package/app/app.vue +10 -10
- package/app/components/NuxtSpecApiTestComponent.vue +24 -24
- package/app/components/NuxtSpecTestComponent.vue +9 -9
- package/app/components/index.d.ts +7 -7
- package/app/components/index.ts +2 -2
- package/app/utils/vitest-utils.ts +5 -5
- package/bin/_internal/prepublish-check.js +16 -16
- package/bin/cli.js +53 -44
- package/bin/commands/ci.js +124 -0
- package/bin/commands/setup.js +367 -0
- package/bin/commands/update.js +126 -0
- package/bin/helpers/commands.js +82 -82
- package/bin/helpers/constants.js +8 -0
- package/bin/helpers/git.js +73 -0
- package/config/index.d.ts +36 -33
- package/config/index.mjs +147 -132
- package/config/report/report-entry.html +6 -0
- package/config/report/report-head.html +34 -0
- package/{utils/screenshot/report-entry.html → config/report/report-screenshot.html} +8 -8
- package/config/report/report-tail.html +4 -0
- package/config/templates/.github/workflows/ci-test-update.yml.template +105 -0
- package/config/templates/.github/workflows/ci-test-verify.yml.template +68 -0
- package/config/templates/.gitlab/ci/ci-test-update.yml.template +69 -0
- package/config/templates/.gitlab/ci/ci-test-verify.yml.template +41 -0
- package/config/templates/.gitlab-ci.yml.template +6 -0
- package/config/templates/pnpm-workspace.yaml.template +19 -19
- package/config/templates/vitest.config.ts.template +5 -5
- package/config/utils/merge.mjs +43 -43
- package/config/utils/options.d.mts +59 -0
- package/config/utils/options.mjs +116 -0
- package/config/utils/reporter.mjs +409 -0
- package/config/utils/warnings.mjs +38 -33
- package/modules/spec-options.ts +23 -0
- package/nuxt.config.ts +20 -24
- package/package.json +37 -31
- package/utils/e2e.ts +131 -131
- package/utils/helpers/check-params.ts +44 -41
- package/utils/helpers/location-hint.ts +30 -30
- package/utils/helpers/screenshot-utils.ts +27 -0
- package/utils/index.ts +12 -12
- package/utils/playwright.ts +28 -20
- package/utils/screenshot.ts +201 -172
- package/bin/setup.js +0 -287
- package/bin/update.js +0 -94
- package/utils/screenshot/report-head.html +0 -24
- package/utils/screenshot/report-tail.html +0 -4
- package/utils/screenshot/report-utils.ts +0 -236
package/utils/e2e.ts
CHANGED
|
@@ -1,131 +1,131 @@
|
|
|
1
|
-
import { createPage, url } from '@nuxt/test-utils/e2e'
|
|
2
|
-
import type { GotoOptions, NuxtPage } from '@nuxt/test-utils'
|
|
3
|
-
import { checkPageParam, checkStringParam } from './helpers/check-params'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Extra settings object for `gotoPage()` function.
|
|
7
|
-
* All properties are optional.
|
|
8
|
-
*/
|
|
9
|
-
export type GotoPageOptions = {
|
|
10
|
-
/** Event to be awaited before NuxtPage instance is returned (defaults to `'hydration'`) */
|
|
11
|
-
waitUntil?: GotoOptions['waitUntil']
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Visit a specified URL and return the page instance for further interaction.
|
|
16
|
-
*
|
|
17
|
-
* @param pageName - Path segment appended to the base URL (e.g. `'about'` → `/<about>`)
|
|
18
|
-
* @param options - Optional extra settings (see `GotoPageOptions`)
|
|
19
|
-
* @returns Playwright page instance after navigation and `hydration` event
|
|
20
|
-
*/
|
|
21
|
-
export async function gotoPage(pageName: string, options?: GotoPageOptions): Promise<NuxtPage> {
|
|
22
|
-
const { waitUntil } = options ?? {}
|
|
23
|
-
|
|
24
|
-
// verify params
|
|
25
|
-
checkStringParam('gotoPage:pageName', pageName)
|
|
26
|
-
if (waitUntil) {
|
|
27
|
-
checkStringParam('gotoPage:options.waitUntil', waitUntil)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// construct NuxtPage instance
|
|
31
|
-
const page = await createPage()
|
|
32
|
-
const urlPath = pageName.startsWith('/') ? url(pageName) : url(`/${pageName}`)
|
|
33
|
-
await page.goto(urlPath, { waitUntil: waitUntil ?? 'hydration' })
|
|
34
|
-
|
|
35
|
-
return page
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Extra settings object for `getDataHtml()` function.
|
|
40
|
-
* All properties are optional.
|
|
41
|
-
*/
|
|
42
|
-
export type GetDataHtmlOptions = {
|
|
43
|
-
/** Event to be awaited before NuxtPage instance is returned (defaults to `'hydration'`) */
|
|
44
|
-
waitUntil?: GotoOptions['waitUntil']
|
|
45
|
-
/** CSS selector identifying the target element (defaults to `<body>` tag) */
|
|
46
|
-
element?: string
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Extract inner HTML content from a specified element on a given page.
|
|
51
|
-
* If no element is specified via `options.element`, the entire `<body>` content is returned.
|
|
52
|
-
*
|
|
53
|
-
* @param page - Playwright page instance, or a page name string (will call `gotoPage` internally)
|
|
54
|
-
* @param options - Optional extra settings (see `GetDataHtmlOptions`)
|
|
55
|
-
* @returns The inner HTML of the matched element
|
|
56
|
-
*/
|
|
57
|
-
export async function getDataHtml(page: NuxtPage | string, options?: GetDataHtmlOptions): Promise<string> {
|
|
58
|
-
const { waitUntil, element } = options ?? {}
|
|
59
|
-
|
|
60
|
-
// verify params
|
|
61
|
-
if (typeof page === 'string') {
|
|
62
|
-
checkStringParam('getDataHtml:page', page)
|
|
63
|
-
}
|
|
64
|
-
if (waitUntil) {
|
|
65
|
-
checkStringParam('getDataHtml:options.waitUntil', waitUntil)
|
|
66
|
-
}
|
|
67
|
-
if (element) {
|
|
68
|
-
checkStringParam('getDataHtml:options.element', element)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// get NuxtPage instance
|
|
72
|
-
const pageInstance = typeof page === 'string' ? await gotoPage(page, { waitUntil }) : page
|
|
73
|
-
checkPageParam('getDataHtml:pageInstance', pageInstance)
|
|
74
|
-
|
|
75
|
-
// extract target HTML
|
|
76
|
-
const dataElement = pageInstance.locator(element ?? 'body')
|
|
77
|
-
return await dataElement.innerHTML()
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Extra settings object for `getAPIResultHtml()` function.
|
|
82
|
-
* Due to nature of the function, `triggerElement`, `targetUrl`, and `responseElement` are required.
|
|
83
|
-
*/
|
|
84
|
-
export type GetAPIResultHtmlOptions = {
|
|
85
|
-
/** Event to be awaited before NuxtPage instance is returned (defaults to `'hydration'`) */
|
|
86
|
-
waitUntil?: GotoOptions['waitUntil']
|
|
87
|
-
/** CSS selector for the clickable element that triggers the API request (required) */
|
|
88
|
-
triggerElement: string
|
|
89
|
-
/** Substring matched against the response URL to identify the expected API call (required) */
|
|
90
|
-
targetUrl: string
|
|
91
|
-
/** CSS selector for the element displaying the API response (required) */
|
|
92
|
-
responseElement: string
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Execute an API call by clicking a trigger element, wait for a successful
|
|
97
|
-
* response matching the target URL, then extract the inner HTML from the
|
|
98
|
-
* response element.
|
|
99
|
-
*
|
|
100
|
-
* @param page - Playwright page instance, or a page name string (will call `gotoPage` internally)
|
|
101
|
-
* @param options - Extra settings (see `GetAPIResultHtmlOptions`)
|
|
102
|
-
* @returns The inner HTML of the response element
|
|
103
|
-
*/
|
|
104
|
-
export async function getAPIResultHtml(page: NuxtPage | string, options: GetAPIResultHtmlOptions) {
|
|
105
|
-
const { waitUntil, triggerElement, targetUrl, responseElement } = options
|
|
106
|
-
|
|
107
|
-
// verify params
|
|
108
|
-
if (typeof page === 'string') {
|
|
109
|
-
checkStringParam('getAPIResultHtml:page', page)
|
|
110
|
-
}
|
|
111
|
-
if (waitUntil) {
|
|
112
|
-
checkStringParam('getAPIResultHtml:options.waitUntil', waitUntil)
|
|
113
|
-
}
|
|
114
|
-
checkStringParam('getAPIResultHtml:triggerElement', triggerElement)
|
|
115
|
-
checkStringParam('getAPIResultHtml:targetUrl', targetUrl)
|
|
116
|
-
checkStringParam('getAPIResultHtml:responseElement', responseElement)
|
|
117
|
-
|
|
118
|
-
// get NuxtPage instance
|
|
119
|
-
const pageInstance = typeof page === 'string' ? await gotoPage(page, { waitUntil }) : page
|
|
120
|
-
checkPageParam('getAPIResultHtml:pageInstance', pageInstance)
|
|
121
|
-
|
|
122
|
-
// trigger API call
|
|
123
|
-
await pageInstance.click(triggerElement)
|
|
124
|
-
await pageInstance.waitForResponse(response =>
|
|
125
|
-
response.url().includes(targetUrl) && response.ok(),
|
|
126
|
-
)
|
|
127
|
-
|
|
128
|
-
// extract target HTML
|
|
129
|
-
const dataElement = pageInstance.locator(responseElement)
|
|
130
|
-
return await dataElement.innerHTML()
|
|
131
|
-
}
|
|
1
|
+
import { createPage, url } from '@nuxt/test-utils/e2e'
|
|
2
|
+
import type { GotoOptions, NuxtPage } from '@nuxt/test-utils'
|
|
3
|
+
import { checkPageParam, checkStringParam } from './helpers/check-params'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Extra settings object for `gotoPage()` function.
|
|
7
|
+
* All properties are optional.
|
|
8
|
+
*/
|
|
9
|
+
export type GotoPageOptions = {
|
|
10
|
+
/** Event to be awaited before NuxtPage instance is returned (defaults to `'hydration'`) */
|
|
11
|
+
waitUntil?: GotoOptions['waitUntil']
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Visit a specified URL and return the page instance for further interaction.
|
|
16
|
+
*
|
|
17
|
+
* @param pageName - Path segment appended to the base URL (e.g. `'about'` → `/<about>`)
|
|
18
|
+
* @param options - Optional extra settings (see `GotoPageOptions`)
|
|
19
|
+
* @returns Playwright page instance after navigation and `hydration` event
|
|
20
|
+
*/
|
|
21
|
+
export async function gotoPage(pageName: string, options?: GotoPageOptions): Promise<NuxtPage> {
|
|
22
|
+
const { waitUntil } = options ?? {}
|
|
23
|
+
|
|
24
|
+
// verify params
|
|
25
|
+
checkStringParam('gotoPage:pageName', pageName)
|
|
26
|
+
if (waitUntil) {
|
|
27
|
+
checkStringParam('gotoPage:options.waitUntil', waitUntil)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// construct NuxtPage instance
|
|
31
|
+
const page = await createPage()
|
|
32
|
+
const urlPath = pageName.startsWith('/') ? url(pageName) : url(`/${pageName}`)
|
|
33
|
+
await page.goto(urlPath, { waitUntil: waitUntil ?? 'hydration' })
|
|
34
|
+
|
|
35
|
+
return page
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Extra settings object for `getDataHtml()` function.
|
|
40
|
+
* All properties are optional.
|
|
41
|
+
*/
|
|
42
|
+
export type GetDataHtmlOptions = {
|
|
43
|
+
/** Event to be awaited before NuxtPage instance is returned (defaults to `'hydration'`) */
|
|
44
|
+
waitUntil?: GotoOptions['waitUntil']
|
|
45
|
+
/** CSS selector identifying the target element (defaults to `<body>` tag) */
|
|
46
|
+
element?: string
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Extract inner HTML content from a specified element on a given page.
|
|
51
|
+
* If no element is specified via `options.element`, the entire `<body>` content is returned.
|
|
52
|
+
*
|
|
53
|
+
* @param page - Playwright page instance, or a page name string (will call `gotoPage` internally)
|
|
54
|
+
* @param options - Optional extra settings (see `GetDataHtmlOptions`)
|
|
55
|
+
* @returns The inner HTML of the matched element
|
|
56
|
+
*/
|
|
57
|
+
export async function getDataHtml(page: NuxtPage | string, options?: GetDataHtmlOptions): Promise<string> {
|
|
58
|
+
const { waitUntil, element } = options ?? {}
|
|
59
|
+
|
|
60
|
+
// verify params
|
|
61
|
+
if (typeof page === 'string') {
|
|
62
|
+
checkStringParam('getDataHtml:page', page)
|
|
63
|
+
}
|
|
64
|
+
if (waitUntil) {
|
|
65
|
+
checkStringParam('getDataHtml:options.waitUntil', waitUntil)
|
|
66
|
+
}
|
|
67
|
+
if (element) {
|
|
68
|
+
checkStringParam('getDataHtml:options.element', element)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// get NuxtPage instance
|
|
72
|
+
const pageInstance = typeof page === 'string' ? await gotoPage(page, { waitUntil }) : page
|
|
73
|
+
checkPageParam('getDataHtml:pageInstance', pageInstance)
|
|
74
|
+
|
|
75
|
+
// extract target HTML
|
|
76
|
+
const dataElement = pageInstance.locator(element ?? 'body')
|
|
77
|
+
return await dataElement.innerHTML()
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Extra settings object for `getAPIResultHtml()` function.
|
|
82
|
+
* Due to nature of the function, `triggerElement`, `targetUrl`, and `responseElement` are required.
|
|
83
|
+
*/
|
|
84
|
+
export type GetAPIResultHtmlOptions = {
|
|
85
|
+
/** Event to be awaited before NuxtPage instance is returned (defaults to `'hydration'`) */
|
|
86
|
+
waitUntil?: GotoOptions['waitUntil']
|
|
87
|
+
/** CSS selector for the clickable element that triggers the API request (required) */
|
|
88
|
+
triggerElement: string
|
|
89
|
+
/** Substring matched against the response URL to identify the expected API call (required) */
|
|
90
|
+
targetUrl: string
|
|
91
|
+
/** CSS selector for the element displaying the API response (required) */
|
|
92
|
+
responseElement: string
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Execute an API call by clicking a trigger element, wait for a successful
|
|
97
|
+
* response matching the target URL, then extract the inner HTML from the
|
|
98
|
+
* response element.
|
|
99
|
+
*
|
|
100
|
+
* @param page - Playwright page instance, or a page name string (will call `gotoPage` internally)
|
|
101
|
+
* @param options - Extra settings (see `GetAPIResultHtmlOptions`)
|
|
102
|
+
* @returns The inner HTML of the response element
|
|
103
|
+
*/
|
|
104
|
+
export async function getAPIResultHtml(page: NuxtPage | string, options: GetAPIResultHtmlOptions) {
|
|
105
|
+
const { waitUntil, triggerElement, targetUrl, responseElement } = options
|
|
106
|
+
|
|
107
|
+
// verify params
|
|
108
|
+
if (typeof page === 'string') {
|
|
109
|
+
checkStringParam('getAPIResultHtml:page', page)
|
|
110
|
+
}
|
|
111
|
+
if (waitUntil) {
|
|
112
|
+
checkStringParam('getAPIResultHtml:options.waitUntil', waitUntil)
|
|
113
|
+
}
|
|
114
|
+
checkStringParam('getAPIResultHtml:triggerElement', triggerElement)
|
|
115
|
+
checkStringParam('getAPIResultHtml:targetUrl', targetUrl)
|
|
116
|
+
checkStringParam('getAPIResultHtml:responseElement', responseElement)
|
|
117
|
+
|
|
118
|
+
// get NuxtPage instance
|
|
119
|
+
const pageInstance = typeof page === 'string' ? await gotoPage(page, { waitUntil }) : page
|
|
120
|
+
checkPageParam('getAPIResultHtml:pageInstance', pageInstance)
|
|
121
|
+
|
|
122
|
+
// trigger API call
|
|
123
|
+
await pageInstance.click(triggerElement)
|
|
124
|
+
await pageInstance.waitForResponse(response =>
|
|
125
|
+
response.url().includes(targetUrl) && response.ok(),
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
// extract target HTML
|
|
129
|
+
const dataElement = pageInstance.locator(responseElement)
|
|
130
|
+
return await dataElement.innerHTML()
|
|
131
|
+
}
|
|
@@ -1,41 +1,44 @@
|
|
|
1
|
-
import type { NuxtPage } from '@nuxt/test-utils'
|
|
2
|
-
import { locationHint } from './location-hint'
|
|
3
|
-
|
|
4
|
-
// given string param must be an actual non-empty string
|
|
5
|
-
export function checkStringParam(paramName: string, paramValue?: string) {
|
|
6
|
-
if (!paramValue) {
|
|
7
|
-
console.warn(`Passed value '${paramName}' is missing or empty.\n${locationHint()}`)
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
1
|
+
import type { NuxtPage } from '@nuxt/test-utils'
|
|
2
|
+
import { locationHint } from './location-hint'
|
|
3
|
+
|
|
4
|
+
// given string param must be an actual non-empty string
|
|
5
|
+
export function checkStringParam(paramName: string, paramValue?: string) {
|
|
6
|
+
if (!paramValue) {
|
|
7
|
+
console.warn(`[Nuxt Spec] Passed value '${paramName}' is missing or empty.\n${locationHint()}`)
|
|
8
|
+
return
|
|
9
|
+
}
|
|
10
|
+
if (typeof paramValue !== 'string') {
|
|
11
|
+
console.warn(`[Nuxt Spec] Passed value '${paramName}' is not a string.\n${locationHint()}`)
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// given string param must be an actual number
|
|
16
|
+
export function checkNumberParam(paramName: string, paramValue?: number) {
|
|
17
|
+
if (paramValue === undefined || paramValue === null) {
|
|
18
|
+
console.warn(`[Nuxt Spec] Passed value '${paramName}' is missing or empty.\n${locationHint()}`)
|
|
19
|
+
return
|
|
20
|
+
}
|
|
21
|
+
if (typeof paramValue !== 'number') {
|
|
22
|
+
console.warn(`[Nuxt Spec] Passed value '${paramName}' is not a number.\n${locationHint()}`)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// given NuxtPage param must be an actual non-nullish NuxtPage instance
|
|
27
|
+
export function checkPageParam(paramName: string, paramValue?: NuxtPage) {
|
|
28
|
+
if (!paramValue) {
|
|
29
|
+
console.warn(`[Nuxt Spec] Passed value '${paramName}' is missing or empty.\n${locationHint()}`)
|
|
30
|
+
return
|
|
31
|
+
}
|
|
32
|
+
if (typeof paramValue !== 'object') {
|
|
33
|
+
console.warn(`[Nuxt Spec] Passed value '${paramName}' is not a NuxtPage.\n${locationHint()}`)
|
|
34
|
+
} else {
|
|
35
|
+
const pageInstance = paramValue as unknown as Record<string, unknown>
|
|
36
|
+
if (
|
|
37
|
+
typeof pageInstance.goto !== 'function'
|
|
38
|
+
|| typeof pageInstance.locator !== 'function'
|
|
39
|
+
|| typeof pageInstance.waitForResponse !== 'function'
|
|
40
|
+
) {
|
|
41
|
+
console.warn(`[Nuxt Spec] Passed value '${paramName}' is not a NuxtPage.\n${locationHint()}`)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
import { createRequire } from 'node:module'
|
|
2
|
-
import { dirname } from 'node:path'
|
|
3
|
-
|
|
4
|
-
// shows "(at path-to-file:line:column)" hint to locate cause from stack trace
|
|
5
|
-
// this syntax allow tools like VS Code to create clickable link directly to the source
|
|
6
|
-
export function locationHint(): string {
|
|
7
|
-
return ` (at ${getCallSite()})`
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
// path to nuxt-spec/utils directory computed on runtime
|
|
11
|
-
const pkgUtilsDir = normalizePath(dirname(createRequire(import.meta.url).resolve('nuxt-spec/utils')))
|
|
12
|
-
|
|
13
|
-
// handle possible Win backslashes
|
|
14
|
-
function normalizePath(path: string): string {
|
|
15
|
-
return path.replace(/^file:\/\//, '').replace(/\\/g, '/')
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// locate first stack frame outside of nuxt-spec utils
|
|
19
|
-
// which will the place where the utility function is being called from
|
|
20
|
-
function getCallSite(): string | undefined {
|
|
21
|
-
const stack = new Error().stack?.split('\n').slice(1) ?? []
|
|
22
|
-
for (const raw of stack) {
|
|
23
|
-
const match = raw.match(/\(?((?:file:\/\/)?[^()\s]+:\d+:\d+)\)?\s*$/)
|
|
24
|
-
if (!match?.[1]) continue
|
|
25
|
-
const location = normalizePath(match[1])
|
|
26
|
-
if (location.includes(pkgUtilsDir)) continue
|
|
27
|
-
return location
|
|
28
|
-
}
|
|
29
|
-
return undefined
|
|
30
|
-
}
|
|
1
|
+
import { createRequire } from 'node:module'
|
|
2
|
+
import { dirname } from 'node:path'
|
|
3
|
+
|
|
4
|
+
// shows "(at path-to-file:line:column)" hint to locate cause from stack trace
|
|
5
|
+
// this syntax allow tools like VS Code to create clickable link directly to the source
|
|
6
|
+
export function locationHint(): string {
|
|
7
|
+
return ` (at ${getCallSite()})`
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// path to nuxt-spec/utils directory computed on runtime
|
|
11
|
+
const pkgUtilsDir = normalizePath(dirname(createRequire(import.meta.url).resolve('nuxt-spec/utils')))
|
|
12
|
+
|
|
13
|
+
// handle possible Win backslashes
|
|
14
|
+
function normalizePath(path: string): string {
|
|
15
|
+
return path.replace(/^file:\/\//, '').replace(/\\/g, '/')
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// locate first stack frame outside of nuxt-spec utils
|
|
19
|
+
// which will the place where the utility function is being called from
|
|
20
|
+
function getCallSite(): string | undefined {
|
|
21
|
+
const stack = new Error().stack?.split('\n').slice(1) ?? []
|
|
22
|
+
for (const raw of stack) {
|
|
23
|
+
const match = raw.match(/\(?((?:file:\/\/)?[^()\s]+:\d+:\d+)\)?\s*$/)
|
|
24
|
+
if (!match?.[1]) continue
|
|
25
|
+
const location = normalizePath(match[1])
|
|
26
|
+
if (location.includes(pkgUtilsDir)) continue
|
|
27
|
+
return location
|
|
28
|
+
}
|
|
29
|
+
return undefined
|
|
30
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { resolve, sep } from 'node:path'
|
|
2
|
+
import type { DecodedPng } from 'fast-png'
|
|
3
|
+
|
|
4
|
+
// helper to keep user-provided targetDir inside the current project root
|
|
5
|
+
export function resolveWithin(base: string, segment: string): string {
|
|
6
|
+
const target = resolve(base, segment)
|
|
7
|
+
if (target !== base && !target.startsWith(base + sep)) {
|
|
8
|
+
throw new Error(`[Nuxt Spec] Invalid path: "${segment}" resolves outside of "${base}"`)
|
|
9
|
+
}
|
|
10
|
+
return target
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// helper for bridging difference between Vitest PNG saving and fast-png encoding
|
|
14
|
+
export function toRGBA(img: DecodedPng): Uint8Array {
|
|
15
|
+
const { width, height, data, channels = 4 } = img
|
|
16
|
+
if (channels === 4) return data as Uint8Array
|
|
17
|
+
const pixels = width * height
|
|
18
|
+
const rgba = new Uint8Array(pixels * 4)
|
|
19
|
+
for (let i = 0; i < pixels; i++) {
|
|
20
|
+
const src = i * 3
|
|
21
|
+
rgba[i * 4 + 0] = data[src + 0] ?? 0
|
|
22
|
+
rgba[i * 4 + 1] = data[src + 1] ?? 0
|
|
23
|
+
rgba[i * 4 + 2] = data[src + 2] ?? 0
|
|
24
|
+
rgba[i * 4 + 3] = 255
|
|
25
|
+
}
|
|
26
|
+
return rgba
|
|
27
|
+
}
|
package/utils/index.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { gotoPage, getDataHtml, getAPIResultHtml } from './e2e'
|
|
2
|
-
import { compareScreenshot } from './screenshot'
|
|
3
|
-
import type { CompareScreenshotOptions } from './screenshot'
|
|
4
|
-
|
|
5
|
-
export {
|
|
6
|
-
compareScreenshot,
|
|
7
|
-
gotoPage,
|
|
8
|
-
getDataHtml,
|
|
9
|
-
getAPIResultHtml,
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export type { CompareScreenshotOptions }
|
|
1
|
+
import { gotoPage, getDataHtml, getAPIResultHtml } from './e2e'
|
|
2
|
+
import { compareScreenshot } from './screenshot'
|
|
3
|
+
import type { CompareScreenshotOptions } from './screenshot'
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
compareScreenshot,
|
|
7
|
+
gotoPage,
|
|
8
|
+
getDataHtml,
|
|
9
|
+
getAPIResultHtml,
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type { CompareScreenshotOptions }
|
package/utils/playwright.ts
CHANGED
|
@@ -1,20 +1,28 @@
|
|
|
1
|
-
// Vitest setup file for the `e2e` project
|
|
2
|
-
// allows connecting to an external Playwright instance,
|
|
3
|
-
// if NUXT_SPEC_EXTERNAL_PLAYWRIGHT is set
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
|
|
1
|
+
// Vitest setup file for the `e2e` project
|
|
2
|
+
// allows connecting to an external Playwright instance,
|
|
3
|
+
// if `spec.externalPlaywright` or NUXT_SPEC_EXTERNAL_PLAYWRIGHT is set
|
|
4
|
+
|
|
5
|
+
import { inject } from 'vitest'
|
|
6
|
+
import { chromium, firefox, webkit } from 'playwright-core'
|
|
7
|
+
import type { Browser, BrowserType } from 'playwright-core'
|
|
8
|
+
|
|
9
|
+
declare module 'vitest' {
|
|
10
|
+
interface ProvidedContext {
|
|
11
|
+
nuxtSpecExternalPlaywright?: string
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// resolved and provided by `loadVitestConfig` (config/index.mjs)
|
|
16
|
+
const externalPlaywright = inject('nuxtSpecExternalPlaywright')
|
|
17
|
+
|
|
18
|
+
if (externalPlaywright) {
|
|
19
|
+
console.log(`[Nuxt Spec] Using external Playwright instance for e2e tests at: ${externalPlaywright}`)
|
|
20
|
+
for (const browserType of [chromium, firefox, webkit] as BrowserType[]) {
|
|
21
|
+
// @nuxt/test-utils always calls `playwright[type].launch()` with no way to opt into
|
|
22
|
+
// `connect()`, so the launch method is swapped for a connect against the WS endpoint
|
|
23
|
+
browserType.launch = (): Promise<Browser> => browserType.connect(externalPlaywright, {
|
|
24
|
+
// this allows reaching caller's localhost from within the external Playwright
|
|
25
|
+
exposeNetwork: '<loopback>',
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
}
|