Repository navigation
feat(nuxt): expose import.meta.envName - #34844
Conversation
|
|
@nuxt/kit
@nuxt/nitro-server
nuxt
@nuxt/rspack-builder
@nuxt/schema
@nuxt/vite-builder
@nuxt/webpack-builder
commit: |
Merging this PR will not alter performance
Comparing Footnotes
|
cda09d7 to
547fe27
Compare
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
💤 Files with no reviewable changes (1)
WalkthroughThis pull request introduces 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/basic.test.ts`:
- Line 159: The assertion is brittle because
page.getByTestId('env-name').textContent() returns raw text with surrounding
whitespace; update the test so you normalize the retrieved value before
asserting (e.g., trim or collapse whitespace) when comparing to
expectedEnvName—locate the call using getByTestId('env-name') and change the
comparison to use the normalized text (trimmed or matched with a flexible
matcher) against expectedEnvName.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 5636b6a2-1420-41af-b766-2e2d1ec99d2f
📒 Files selected for processing (13)
docs/4.api/6.advanced/2.import-meta.mdpackages/kit/src/env.tspackages/kit/src/loader/config.tspackages/kit/test/load-nuxt-config.spec.tspackages/nuxt/src/app/types/augments.tspackages/schema/src/config/common.tspackages/schema/src/types/schema.tspackages/vite/src/shared/client.tspackages/vite/src/shared/server.tspackages/webpack/src/presets/base.tstest/basic.test.tstest/fixtures/basic-types/app/app-types.tstest/fixtures/basic/app/pages/env-name.vue
| it('exposes the current env name at runtime', async () => { | ||
| const expectedEnvName = isDev ? 'development' : 'production' | ||
| const { page } = await renderPage('/env-name') | ||
| expect(await page.getByTestId('env-name').textContent()).toBe(expectedEnvName) |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
# Inspect around the reported line and also find expectedEnvName definition/usage.
echo "== test/basic.test.ts (around 130-180) =="
sed -n '120,190p' test/basic.test.ts
echo
echo "== Find where expectedEnvName is defined (within file) =="
rg -n "expectedEnvName" test/basic.test.ts
echo
echo "== Show some broader context around any template/fixture rendering mentioning env-name =="
rg -n "env-name" test/basic.test.tsRepository: nuxt/nuxt
Length of output: 3627
🌐 Web query:
Playwright Locator.textContent() includes whitespace newlines textContent() behavior recommended to use innerText or toHaveText trim
💡 Result:
When working with text in Playwright, understanding the differences between methods and assertions is crucial for avoiding flaky tests caused by whitespace and hidden elements [1][2]. textContent vs. innerText The locator.textContent method returns the raw text content of a node and all its descendants [1][3]. It includes all whitespace (including newlines) and retrieves text from both visible and hidden elements [2][3]. In contrast, locator.innerText retrieves the visible text content of an element [2][4][3]. Because it is "visible-text aware," it requires layout information, making it more performance-heavy than textContent [3]. It generally aligns more closely with what a user sees on the screen, as it excludes hidden text [2][3]. Recommended Assertion Practice To avoid flakiness and manage whitespace issues, the official Playwright recommendation is to prefer web-first assertions over manual extraction [1]. Instead of manually extracting text with textContent and comparing it, use the built-in assertion expect(locator).toHaveText or expect(locator).toContainText [1][5][6]. These assertions are designed to be resilient [7][8]. Key points regarding whitespace and assertions: 1. Normalization: Playwright's text-based matching (including locators like getByText and assertions like toHaveText) automatically normalizes whitespace [9][10][1]. This means it treats multiple spaces as a single space and ignores leading/trailing whitespace, which is usually the desired behavior for reliable tests [9][11]. 2. Avoiding Flakiness: If you manually extract text using textContent and try to trim or regex it yourself, you risk introducing race conditions [1]. Web-first assertions (expect(locator).toHaveText) include built-in retry logic that waits for the element to reach the expected state, significantly reducing flakiness [1][7][8]. 3. Flexible Matching: If you need to verify that an element contains specific text while ignoring surrounding whitespace, use expect(locator).toContainText [12][13]. If you need to perform more complex logic, you can use regular expressions within these assertions (e.g., await expect(locator).toHaveText(/\S/)) [12]. In summary, avoid manual text extraction for validation [1]. Use expect(locator).toHaveText or expect(locator).toContainText to handle whitespace normalization and waiting logic automatically [1][6].
Citations:
- 1: https://playwright.dev/docs/api/class-locator
- 2: https://www.codekru.com/playwright/innertext-vs-textcontent-in-playwright
- 3: https://stackoverflow.com/questions/35213147/difference-between-textcontent-vs-innertext
- 4: https://www.youtube.com/watch?v=MxAZiAbv45Q
- 5: https://github.com/microsoft/playwright/blob/main/docs/src/test-assertions-js.md
- 6: https://playwright.dev/docs/next/test-assertions
- 7: https://github.com/microsoft/playwright/blob/main/docs/src/best-practices-js.md
- 8: https://playwright.dev/docs/best-practices
- 9: https://github.com/microsoft/playwright/blob/main/docs/src/locators.md
- 10: https://playwright.dev/docs/locators
- 11: https://playwright.dev/docs/next/other-locators
- 12: https://stackoverflow.com/questions/77005165/in-playwright-asserting-for-the-presence-of-any-text-in-a-element
- 13: https://dev.to/kesavarthini/playwright-basics-text-validation-login-testing-checkboxes-and-radio-buttons-4pdf
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "== Search for env-name test id usage =="
rg -n "data-testid=['\"]env-name['\"]|getByTestId\\('env-name'\\)|\"env-name\"" -S . --hidden --glob '!.git/*' --glob '*.ts' --glob '*.js' --glob '*.vue' --glob '*.tsx' --glob '*.md'
echo
echo "== Search for route '/env-name' implementation =="
rg -n "['\"]\\/env-name['\"]|/env-name" -S . --hidden --glob '!.git/*' --glob '*.ts' --glob '*.js' --glob '*.vue'Repository: nuxt/nuxt
Length of output: 447
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "== env-name.vue =="
nl -ba test/fixtures/basic/app/pages/env-name.vue | sed -n '1,120p'
echo
echo "== Find references to '/env-name' (safe patterns) =="
rg -n "/env-name" -S test test/fixtures --glob '!*node_modules*' || trueRepository: nuxt/nuxt
Length of output: 133
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "== env-name.vue (with line numbers) =="
cat -n test/fixtures/basic/app/pages/env-name.vue | sed -n '1,120p'Repository: nuxt/nuxt
Length of output: 308
Whitespace-sensitive assertion on env-name (Line 159)
env-name.vue renders {{ envName }} with leading/trailing newlines/indentation inside the data-testid="env-name" element; Locator.textContent() reads raw text including that whitespace, so toBe(expectedEnvName) can be brittle/flaky.
Suggested fix
- expect(await page.getByTestId('env-name').textContent()).toBe(expectedEnvName)
+ expect((await page.getByTestId('env-name').textContent())?.trim()).toBe(expectedEnvName)📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| expect(await page.getByTestId('env-name').textContent()).toBe(expectedEnvName) | |
| expect((await page.getByTestId('env-name').textContent())?.trim()).toBe(expectedEnvName) |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@test/basic.test.ts` at line 159, The assertion is brittle because
page.getByTestId('env-name').textContent() returns raw text with surrounding
whitespace; update the test so you normalize the retrieved value before
asserting (e.g., trim or collapse whitespace) when comparing to
expectedEnvName—locate the call using getByTestId('env-name') and change the
comparison to use the normalized text (trimmed or matched with a flexible
matcher) against expectedEnvName.
atinux
left a comment
There was a problem hiding this comment.
I like the idea of having a way to have an in-between between dev and prod, many companies need to enable special flags for "staging" for instance, this is a nice way to solve it
(cherry picked from commit a90edd9)
(cherry picked from commit a90edd9)
(cherry picked from commit a90edd9)
|
Hey, sorry to piggy-back on this PR. Will this tree-shake at build time? For example: if (import.meta.envName === 'staging') {
// this entire of block will not be incuded in the production bundle
} |
|
Yes, as I understand it:
If you build with |
This exposes the resolved Nuxt config environment as
import.meta.envNamein app runtime code for both Vite and webpack builds. It also adds runtime typing and updates the v4 import-meta documentation.The change includes focused coverage for the runtime value in the basic fixture and for custom
envNameresolution in the config loader tests.Closes #30776
related #34843