nuxt-spec 0.2.0-alpha.2 → 0.2.0-alpha.3

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/README.md CHANGED
@@ -37,7 +37,7 @@ If you don't want to use the CLI tool, or you want to understand its flow better
37
37
  1) Add following dependency into your `package.json`:
38
38
 
39
39
  ```
40
- "nuxt-spec": "0.2.0-alpha.2"
40
+ "nuxt-spec": "0.2.0-alpha.3"
41
41
  ```
42
42
 
43
43
  2) Add following section into your `nuxt.config.ts`:
@@ -89,7 +89,7 @@ test/
89
89
  └── vitest-unit.test.ts
90
90
  ```
91
91
 
92
- You can use sample files from the [project repository](https://github.com/AloisSeckar/nuxt-spec/tree/v0.2.0-alpha.2/test).
92
+ You can use sample files from the [project repository](https://github.com/AloisSeckar/nuxt-spec/tree/v0.2.0-alpha.3/test).
93
93
 
94
94
  ### Install and execute
95
95
 
@@ -249,11 +249,11 @@ Planned future development:
249
249
  - reason about (not) using Vitest browser mode (or make it optional)
250
250
  - solution for visual regression testing - (currently there is experimental custom solution)
251
251
 
252
- See [CHANGELOG.md](https://github.com/AloisSeckar/nuxt-spec/blob/v0.2.0-alpha.2/CHANGELOG.md) for the latest updates and features.
252
+ See [CHANGELOG.md](https://github.com/AloisSeckar/nuxt-spec/blob/v0.2.0-alpha.3/CHANGELOG.md) for the latest updates and features.
253
253
 
254
254
  ## Configuration
255
255
 
256
- By default, `nuxt-spec` uses Vitest configuration defined in [`/config/index.mjs`](https://github.com/AloisSeckar/nuxt-spec/blob/v0.2.0-alpha.2/config/index.mjs). The configuration is based on [Nuxt team recommendations](https://nuxt.com/docs/4.x/getting-started/testing) and our best judgement.
256
+ By default, `nuxt-spec` uses Vitest configuration defined in [`/config/index.mjs`](https://github.com/AloisSeckar/nuxt-spec/blob/v0.2.0-alpha.3/config/index.mjs). The configuration is based on [Nuxt team recommendations](https://nuxt.com/docs/4.x/getting-started/testing) and our best judgement.
257
257
 
258
258
  To add/override your custom config, you can create (or scaffold via CLI tool) a file named `vitest.config.ts` in the root of your project with the following content:
259
259
 
@@ -313,7 +313,15 @@ import { compareScreenshot, gotoPage, getDataHtml, getAPIResultHtml, } from 'nux
313
313
  // takes a screenshot of current viewport and compares it with stored baseline
314
314
  // if screenshot doesn't exist, it will be created as baseline
315
315
  // if screenshots don't match, the method will cause Vitest test to fail
316
- await compareScreenshot(page, 'screenshot.png')
316
+ // accepts optional object with extra options:
317
+ // - `fileName` - name of the screenshot file (default is based on current route)
318
+ // - `selector` - CSS selector of the element to capture (default is full page)
319
+ // - `targetDir` - directory where the screenshots should be stored (default is `./test/e2e/`)
320
+
321
+ await compareScreenshot(page) // will produce "index.png" file in `./test/e2e/` directory
322
+ await compareScreenshot(page, { fileName: 'homepage.png' }) // will produce "homepage.png"
323
+ await compareScreenshot(page, { fileName: 'component.png', selector: '#test' }) // will produce "component.png" only with id="test" element
324
+ await compareScreenshot(page, { fileName: 'homepage.png', targetDir: '/screenshots' }) // will produce "homepage.png" in `/screenshots` directory
317
325
 
318
326
  // navigates to given URL and returns the instance of NuxtPage (from @nuxt/test-utils)
319
327
  const page: NuxtPage = await gotoPage('url')
@@ -333,7 +341,7 @@ const html: string = await getAPIResultHtml('/', '#api-fetch', '/your-api', '#ap
333
341
  const html: string = await getAPIResultHtml(page, '#api-fetch', '/your-api', '#api-result')
334
342
  ```
335
343
 
336
- For detailed description, see [utils.d.ts](https://github.com/AloisSeckar/nuxt-spec/blob/v0.2.0-alpha.2/utils/index.d.ts).
344
+ For detailed description, see [utils.d.ts](https://github.com/AloisSeckar/nuxt-spec/blob/v0.2.0-alpha.3/utils/index.d.ts).
337
345
 
338
346
  ## Contact
339
347
 
package/bin/setup.js CHANGED
@@ -37,7 +37,7 @@ export async function specSetup(autoRun = false) {
37
37
  // add nuxt-spec
38
38
  try {
39
39
  await updateJsonFile('package.json', 'dependencies', {
40
- 'nuxt-spec': '0.2.0-alpha.2',
40
+ 'nuxt-spec': '0.2.0-alpha.3',
41
41
  }, isAutoRun, 'This will add \'nuxt-spec\' dependency to your \'package.json\'. Continue?')
42
42
  } catch (error) {
43
43
  console.error('Error adding \'nuxt-spec\' dependency:\n', error.message)
@@ -107,7 +107,7 @@ export async function specSetup(autoRun = false) {
107
107
  if (pathExists('.npmrc')) {
108
108
  await updateTextFile('.npmrc', ['shamefully-hoist=true'], isAutoRun, 'This will adjust \'.npmrc\' file in your project. Continue?')
109
109
  } else {
110
- await createFileFromWebTemplate('https://raw.githubusercontent.com/AloisSeckar/nuxt-spec/refs/tags/v0.2.0-alpha.2/.npmrc',
110
+ await createFileFromWebTemplate('https://raw.githubusercontent.com/AloisSeckar/nuxt-spec/refs/tags/v0.2.0-alpha.3/.npmrc',
111
111
  '.npmrc', isAutoRun, 'This will add \'.npmrc\' file for your project. Continue?')
112
112
  }
113
113
  } catch (error) {
@@ -117,7 +117,7 @@ export async function specSetup(autoRun = false) {
117
117
 
118
118
  // 4) create vitest.config.ts
119
119
  try {
120
- await createFileFromWebTemplate('https://raw.githubusercontent.com/AloisSeckar/nuxt-spec/refs/tags/v0.2.0-alpha.2/config/vitest.config.ts.template',
120
+ await createFileFromWebTemplate('https://raw.githubusercontent.com/AloisSeckar/nuxt-spec/refs/tags/v0.2.0-alpha.3/config/vitest.config.ts.template',
121
121
  'vitest.config.ts', isAutoRun, 'This will create a new \'vitest.config.ts\' file for your project. Continue?')
122
122
  } catch (error) {
123
123
  console.error('Error setting up \'vitest.config.ts\':\n', error.message)
@@ -155,31 +155,31 @@ export async function specSetup(autoRun = false) {
155
155
  const createSampleTests = isAutoRun || await promptUser('Do you want to create sample tests in \'/test\' folder?')
156
156
  if (createSampleTests) {
157
157
  try {
158
- await createFileFromWebTemplate('https://raw.githubusercontent.com/AloisSeckar/nuxt-spec/refs/tags/v0.2.0-alpha.2/test/browser/vitest-browser.test.ts',
158
+ await createFileFromWebTemplate('https://raw.githubusercontent.com/AloisSeckar/nuxt-spec/refs/tags/v0.2.0-alpha.3/test/browser/vitest-browser.test.ts',
159
159
  'test/browser/vitest-browser.test.ts', true)
160
160
  } catch (error) {
161
161
  console.error('Error setting up \'vitest-browser.test.ts\':\n', error.message)
162
162
  }
163
163
  try {
164
- await createFileFromWebTemplate('https://raw.githubusercontent.com/AloisSeckar/nuxt-spec/refs/tags/v0.2.0-alpha.2/test/e2e/nuxt-e2e.test.ts',
164
+ await createFileFromWebTemplate('https://raw.githubusercontent.com/AloisSeckar/nuxt-spec/refs/tags/v0.2.0-alpha.3/test/e2e/nuxt-e2e.test.ts',
165
165
  'test/e2e/nuxt-e2e.test.ts', true)
166
166
  } catch (error) {
167
167
  console.error('Error setting up \'nuxt-e2e.test.ts\':\n', error.message)
168
168
  }
169
169
  try {
170
- await createFileFromWebTemplate('https://raw.githubusercontent.com/AloisSeckar/nuxt-spec/refs/tags/v0.2.0-alpha.2/test/e2e/nuxt-visual.test.ts',
170
+ await createFileFromWebTemplate('https://raw.githubusercontent.com/AloisSeckar/nuxt-spec/refs/tags/v0.2.0-alpha.3/test/e2e/nuxt-visual.test.ts',
171
171
  'test/e2e/nuxt-visual.test.ts', true)
172
172
  } catch (error) {
173
173
  console.error('Error setting up \'nuxt-visual.test.ts\':\n', error.message)
174
174
  }
175
175
  try {
176
- await createFileFromWebTemplate('https://raw.githubusercontent.com/AloisSeckar/nuxt-spec/refs/tags/v0.2.0-alpha.2/test/nuxt/nuxt-unit.test.ts',
176
+ await createFileFromWebTemplate('https://raw.githubusercontent.com/AloisSeckar/nuxt-spec/refs/tags/v0.2.0-alpha.3/test/nuxt/nuxt-unit.test.ts',
177
177
  'test/nuxt/nuxt-unit.test.ts', true)
178
178
  } catch (error) {
179
179
  console.error('Error setting up \'nuxt-unit.test.ts\':\n', error.message)
180
180
  }
181
181
  try {
182
- await createFileFromWebTemplate('https://raw.githubusercontent.com/AloisSeckar/nuxt-spec/refs/tags/v0.2.0-alpha.2/test/unit/vitest-unit.test.ts',
182
+ await createFileFromWebTemplate('https://raw.githubusercontent.com/AloisSeckar/nuxt-spec/refs/tags/v0.2.0-alpha.3/test/unit/vitest-unit.test.ts',
183
183
  'test/unit/vitest-unit.test.ts', true)
184
184
  } catch (error) {
185
185
  console.error('Error setting up \'vitest-unit.test.ts\':\n', error.message)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-spec",
3
- "version": "0.2.0-alpha.2",
3
+ "version": "0.2.0-alpha.3",
4
4
  "description": "Test-pack layer for Nuxt Applications",
5
5
  "repository": "github:AloisSeckar/nuxt-spec",
6
6
  "license": "MIT",
@@ -3,15 +3,21 @@ import { resolve } from 'node:path'
3
3
  import type { NuxtPage } from '@nuxt/test-utils'
4
4
  import { expect } from 'vitest'
5
5
 
6
- export async function compareScreenshot(page: NuxtPage, fileName: string, targetDir?: string): Promise<boolean> {
7
- const dir = resolve(process.cwd(), targetDir ?? 'test/e2e')
6
+ export async function compareScreenshot(page: NuxtPage, options?: { fileName?: string, targetDir?: string, selector?: string }): Promise<boolean> {
7
+ const dir = resolve(process.cwd(), options?.targetDir ?? 'test/e2e')
8
8
  const baselineDir = resolve(dir, '__baseline__')
9
9
  const currentDir = resolve(dir, '__current__')
10
10
 
11
- // capture full-page screenshot as PNG
12
- const screenshot = await page.screenshot({ fullPage: true })
11
+ const route = page.url().substring(page.url().lastIndexOf('/') + 1) || 'index'
12
+ const fileName = options?.fileName ?? `${route}.png`
13
+
14
+ // capture element specified by locator or a full-page screenshot as PNG
15
+ const screenshot = options?.selector
16
+ ? await page.locator(options.selector).screenshot()
17
+ : await page.screenshot({ fullPage: true })
13
18
  const baselinePath = resolve(baselineDir, fileName)
14
19
 
20
+ // @ts-expect-error - this is reliable way of reading Vitest "update" flag
15
21
  const updating = expect.getState().snapshotState?._updateSnapshot === 'all'
16
22
  if (updating || !existsSync(baselinePath)) {
17
23
  // save new baseline screenshot