From 23560571ee2be8c42ca7f97cd0f088626e46d2b3 Mon Sep 17 00:00:00 2001 From: yamachi4416 Date: Tue, 3 Nov 2026 22:28:14 +0900 Subject: [PATCH 1/2] feat(module): add option to remove data-test attributes --- examples/remove-testid/.gitignore | 2 + .../remove-testid/app/components/Hello.vue | 8 + examples/remove-testid/app/pages/index.vue | 5 + examples/remove-testid/nuxt.config.ts | 26 ++++ examples/remove-testid/package.json | 31 ++++ examples/remove-testid/playwright.config.ts | 24 +++ examples/remove-testid/test/e2e/build.spec.ts | 15 ++ .../remove-testid/test/e2e/feature.spec.ts | 54 +++++++ .../remove-testid/test/nuxt/Hello.spec.ts | 10 ++ examples/remove-testid/tests/build.test.ts | 6 + examples/remove-testid/tsconfig.json | 17 ++ examples/remove-testid/vitest.config.ts | 21 +++ pnpm-lock.yaml | 28 ++++ src/module.ts | 45 ++++++ src/module/plugins/remove-attrs.ts | 43 ++++++ test/unit/remove-attrs.spec.ts | 146 ++++++++++++++++++ 16 files changed, 481 insertions(+) create mode 100644 examples/remove-testid/.gitignore create mode 100644 examples/remove-testid/app/components/Hello.vue create mode 100644 examples/remove-testid/app/pages/index.vue create mode 100644 examples/remove-testid/nuxt.config.ts create mode 100644 examples/remove-testid/package.json create mode 100644 examples/remove-testid/playwright.config.ts create mode 100644 examples/remove-testid/test/e2e/build.spec.ts create mode 100644 examples/remove-testid/test/e2e/feature.spec.ts create mode 100644 examples/remove-testid/test/nuxt/Hello.spec.ts create mode 100644 examples/remove-testid/tests/build.test.ts create mode 100644 examples/remove-testid/tsconfig.json create mode 100644 examples/remove-testid/vitest.config.ts create mode 100644 src/module/plugins/remove-attrs.ts create mode 100644 test/unit/remove-attrs.spec.ts diff --git a/examples/remove-testid/.gitignore b/examples/remove-testid/.gitignore new file mode 100644 index 000000000..aaa9103e4 --- /dev/null +++ b/examples/remove-testid/.gitignore @@ -0,0 +1,2 @@ +test-results/ +playwright-report/ diff --git a/examples/remove-testid/app/components/Hello.vue b/examples/remove-testid/app/components/Hello.vue new file mode 100644 index 000000000..7112ce42f --- /dev/null +++ b/examples/remove-testid/app/components/Hello.vue @@ -0,0 +1,8 @@ + diff --git a/examples/remove-testid/app/pages/index.vue b/examples/remove-testid/app/pages/index.vue new file mode 100644 index 000000000..a684a16a9 --- /dev/null +++ b/examples/remove-testid/app/pages/index.vue @@ -0,0 +1,5 @@ + diff --git a/examples/remove-testid/nuxt.config.ts b/examples/remove-testid/nuxt.config.ts new file mode 100644 index 000000000..58d07c1fc --- /dev/null +++ b/examples/remove-testid/nuxt.config.ts @@ -0,0 +1,26 @@ +const buildDir = process.env.FEATURE ? `./.nuxt/test/build-${process.env.FEATURE}` : undefined + +// https://nuxt.com/docs/api/configuration/nuxt-config +export default defineNuxtConfig({ + modules: ['@nuxt/test-utils/module'], + $env: { + custom: { + testUtils: { + removeTestIdAttribute: 'data-test', + }, + }, + }, + $production: { + testUtils: { + removeTestIdAttribute: true, + }, + }, + devtools: false, + buildDir, + compatibilityDate: '2024-04-03', + nitro: { + output: { + dir: buildDir ? `${buildDir}/output` : undefined, + }, + }, +}) diff --git a/examples/remove-testid/package.json b/examples/remove-testid/package.json new file mode 100644 index 000000000..60e2d5f07 --- /dev/null +++ b/examples/remove-testid/package.json @@ -0,0 +1,31 @@ +{ + "name": "example-remove-testid", + "private": true, + "type": "module", + "scripts": { + "build": "nuxt build", + "dev": "nuxt dev", + "generate": "nuxt generate", + "preview": "nuxt preview", + "postinstall": "nuxt prepare", + "test": "pnpm run build:feature && pnpm run --sequential '/test:.*/'", + "test:vitest": "vitest run", + "test:playwright": "playwright test", + "build:feature": "pnpm run '/build:feature:.*/'", + "build:feature:enabled": "FEATURE=enabled nuxt build", + "build:feature:disabled": "FEATURE=disabled nuxt build --envName staging", + "build:feature:custom": "FEATURE=custom nuxt build --envName custom" + }, + "dependencies": { + "nuxt": "4.6.3" + }, + "devDependencies": { + "@nuxt/test-utils": "latest", + "@playwright/test": "1.64.1", + "@vue/test-utils": "2.5.1", + "happy-dom": "21.0.4", + "tinyglobby": "0.2.17", + "typescript": "6.0.3", + "vitest": "5.0.5" + } +} diff --git a/examples/remove-testid/playwright.config.ts b/examples/remove-testid/playwright.config.ts new file mode 100644 index 000000000..6ee0f0c94 --- /dev/null +++ b/examples/remove-testid/playwright.config.ts @@ -0,0 +1,24 @@ +import process from 'node:process' +import { defineConfig, devices } from '@playwright/test' +import type { ConfigOptions } from '@nuxt/test-utils/playwright' + +export default defineConfig({ + testDir: './tests', + fullyParallel: true, + forbidOnly: !!process.env.CI, + retries: process.env.CI ? 2 : 0, + reporter: 'html', + timeout: 120_000, + use: { + trace: 'on-first-retry', + nuxt: { + rootDir: import.meta.dirname, + }, + }, + projects: [ + { + name: 'chromium-headless-shell', + use: { ...devices['Desktop Chrome'] }, + }, + ], +}) diff --git a/examples/remove-testid/test/e2e/build.spec.ts b/examples/remove-testid/test/e2e/build.spec.ts new file mode 100644 index 000000000..5c521a70a --- /dev/null +++ b/examples/remove-testid/test/e2e/build.spec.ts @@ -0,0 +1,15 @@ +import { fileURLToPath } from 'node:url' +import { expect, it } from 'vitest' +import { createPage, setup } from '@nuxt/test-utils/e2e' + +await setup({ + rootDir: fileURLToPath(new URL('../../', import.meta.url)), + browser: true, +}) + +it('should disable feature when building in tests', async () => { + const page = await createPage('/') + const text = await page.getByTestId('hello').textContent() + expect(text).toContain('Hello Nuxt!') + await page.close() +}) diff --git a/examples/remove-testid/test/e2e/feature.spec.ts b/examples/remove-testid/test/e2e/feature.spec.ts new file mode 100644 index 000000000..74bd80e85 --- /dev/null +++ b/examples/remove-testid/test/e2e/feature.spec.ts @@ -0,0 +1,54 @@ +import { fileURLToPath } from 'node:url' +import { describe, expect, it } from 'vitest' +import { createPage, setup } from '@nuxt/test-utils/e2e' + +async function setupTest(fixtureName: string) { + await setup({ + rootDir: fileURLToPath(new URL('../../', import.meta.url)), + build: false, + browser: true, + nuxtConfig: { + nitro: { + output: { + dir: fileURLToPath(new URL(`../../.nuxt/test/${fixtureName}/output`, import.meta.url)), + }, + }, + }, + }) +} + +describe('feature enabled build', async () => { + await setupTest('build-enabled') + + it('should remove default test attribute when enabled', async () => { + const page = await createPage('/') + const heading = page.getByRole('heading', { level: 1 }) + await expect(heading.isVisible()).resolves.toBeTruthy() + await expect(heading.getAttribute('data-testid')).resolves.toBeFalsy() + await expect(heading.getAttribute('data-test')).resolves.toBe('world') + }) +}) + +describe('feature disabled build', async () => { + await setupTest('build-disabled') + + it('should keep test attributes when disabled', async () => { + const page = await createPage('/') + const heading = page.getByRole('heading', { level: 1 }) + await expect(heading.isVisible()).resolves.toBeTruthy() + await expect(heading.getAttribute('data-testid')).resolves.toBe('hello') + await expect(heading.getAttribute('data-test')).resolves.toBe('world') + }) +}) + +describe('feature custom build', async () => { + await setupTest('build-custom') + + it('should remove custom test attribute when enabled', async () => { + const page = await createPage('/') + const heading = page.getByRole('heading', { level: 1 }) + await expect(heading.isVisible()).resolves.toBeTruthy() + await expect(heading.getAttribute('data-testid')).resolves.toBe('hello') + await expect(heading.getAttribute('data-test')).resolves.toBeFalsy() + }) +}) diff --git a/examples/remove-testid/test/nuxt/Hello.spec.ts b/examples/remove-testid/test/nuxt/Hello.spec.ts new file mode 100644 index 000000000..c9dab0f8a --- /dev/null +++ b/examples/remove-testid/test/nuxt/Hello.spec.ts @@ -0,0 +1,10 @@ +import { it, expect } from 'vitest' +import { mountSuspended } from '@nuxt/test-utils/runtime' + +import { Hello } from '#components' + +it('should disable feature in unit tests', async () => { + const wrapper = await mountSuspended(Hello) + expect(wrapper.attributes('data-testid')).toBe('hello') + expect(wrapper.attributes('data-test')).toBe('world') +}) diff --git a/examples/remove-testid/tests/build.test.ts b/examples/remove-testid/tests/build.test.ts new file mode 100644 index 000000000..52af9cd08 --- /dev/null +++ b/examples/remove-testid/tests/build.test.ts @@ -0,0 +1,6 @@ +import { expect, test } from '@nuxt/test-utils/playwright' + +test('should disable feature when building in tests', async ({ page, goto }) => { + await goto('/', { waitUntil: 'hydration' }) + await expect(page.getByTestId('hello')).toHaveText('Hello Nuxt!') +}) diff --git a/examples/remove-testid/tsconfig.json b/examples/remove-testid/tsconfig.json new file mode 100644 index 000000000..6ae5970c7 --- /dev/null +++ b/examples/remove-testid/tsconfig.json @@ -0,0 +1,17 @@ +{ + "files": [], + "references": [ + { + "path": "./.nuxt/tsconfig.app.json" + }, + { + "path": "./.nuxt/tsconfig.server.json" + }, + { + "path": "./.nuxt/tsconfig.shared.json" + }, + { + "path": "./.nuxt/tsconfig.node.json" + } + ] +} diff --git a/examples/remove-testid/vitest.config.ts b/examples/remove-testid/vitest.config.ts new file mode 100644 index 000000000..4c758fe0b --- /dev/null +++ b/examples/remove-testid/vitest.config.ts @@ -0,0 +1,21 @@ +import { defineConfig } from 'vitest/config' +import { defineVitestProject } from '@nuxt/test-utils/config' + +export default defineConfig({ + test: { + projects: [ + { + test: { + name: 'e2e', + dir: './test/e2e', + }, + }, + await defineVitestProject({ + test: { + name: 'nuxt', + dir: './test/nuxt', + }, + }), + ], + }, +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 111b959f4..a6781566e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -719,6 +719,34 @@ importers: specifier: 3.1.0 version: 3.1.0(@vue/compiler-dom@3.5.44)(@vue/server-renderer@3.5.44)(vitest@5.0.5)(vue@3.5.44(typescript@6.0.3)) + examples/remove-testid: + dependencies: + nuxt: + specifier: 4.6.3 + version: 4.6.3(@babel/plugin-syntax-jsx@7.29.7(@babel/core@7.29.7(supports-color@11.0.0)))(@babel/plugin-syntax-typescript@7.29.7(@babel/core@7.29.7(supports-color@11.0.0)))(@oxc-project/types@0.156.0)(@parcel/watcher@2.6.0)(@types/node@24.19.2)(@vue/compiler-sfc@3.5.44)(acorn@8.18.0)(better-sqlite3@13.0.3)(confbox@0.3.1)(db0@0.4.1)(dotenv@17.4.2)(esbuild@0.28.2)(eslint@10.14.0(jiti@2.7.0)(supports-color@11.0.0))(giget@3.3.1)(idb-keyval@6.3.0)(ioredis@5.11.1(supports-color@11.0.0))(lightningcss@1.33.0)(mlly@1.8.2)(optionator@0.9.4)(oxc-parser@0.156.0)(rolldown@1.2.15)(rollup-plugin-visualizer@7.1.1(rolldown@1.2.15)(rollup@4.67.4))(rollup@4.67.4)(srvx@1.0.5)(supports-color@11.0.0)(terser@5.51.2)(typescript@6.0.3)(vite@8.3.5(@types/node@24.19.2)(esbuild@0.28.2)(jiti@2.7.0)(terser@5.51.2)(yaml@2.9.1))(vue-tsc@3.3.14(typescript@6.0.3))(yaml@2.9.1) + devDependencies: + '@nuxt/test-utils': + specifier: workspace:* + version: link:../.. + '@playwright/test': + specifier: 1.64.1 + version: 1.64.1 + '@vue/test-utils': + specifier: 2.5.1 + version: 2.5.1(@vue/compiler-dom@3.5.44)(@vue/server-renderer@3.5.44)(vue@3.5.44(typescript@6.0.3)) + happy-dom: + specifier: 21.0.4 + version: 21.0.4 + tinyglobby: + specifier: 0.2.17 + version: 0.2.17 + typescript: + specifier: 6.0.3 + version: 6.0.3 + vitest: + specifier: 5.0.5 + version: 5.0.5(@types/node@24.19.2)(@vitest/browser-playwright@5.0.5)(@vitest/ui@5.0.5)(happy-dom@21.0.4)(jsdom@30.1.3)(vite@8.3.5(@types/node@24.19.2)(esbuild@0.28.2)(jiti@2.7.0)(terser@5.51.2)(yaml@2.9.1)) + stubs/vitest-environment-nuxt: dependencies: '@nuxt/test-utils': diff --git a/src/module.ts b/src/module.ts index f1160a7b8..cfc6be4fd 100644 --- a/src/module.ts +++ b/src/module.ts @@ -6,6 +6,7 @@ import { isCI } from 'std-env' import { setupImportMocking } from './module/mock.ts' import { NuxtRootStubPlugin } from './module/plugins/entry.ts' +import { removeAttrsNodeTransform } from './module/plugins/remove-attrs.ts' import { runInstallWizard } from './module/install-wizard.ts' import { loadKit, resolveH3Package } from './utils.ts' import { setupDevTools } from './devtools.ts' @@ -17,6 +18,30 @@ export interface NuxtVitestOptions { startOnBoot?: boolean logToConsole?: boolean vitestConfig?: VitestConfig + /** + * Remove testId attribute (case-insensitive) from Vue `