Comments (4)
I should mention I have solved this by exporting all needed env values from a separate file:
// fx utils/env.ts
export const MODE = import.meta.env.MODE
export const PROD = import.meta.env.PROD
This allows for easy mocking as well during tests.
from vitest.
I ran into the same issue (import.meta.env.DEV
) and I couldn't figure out what was causing it, thank you!
from vitest.
It looks like strip-literal
is confused by Vue component file. v1.3.0 is where strip-literal changed from acorn to js-token https://github.com/vitest-dev/vitest/releases/tag/v1.3.0 #5136, so that may be the reason (though I'm not sure how acorn could've handle Vue either).
When I locally debug the code around here:
vitest/packages/vitest/src/node/plugins/ssrReplacer.ts
Lines 16 to 18 in 6eda473
I see following transformations:
Hello.vue
- original
<template>
<SomeOtherComponent
:some-prop="
testValue
? 'A long value to break handling of env variables'
: 'it needs to break into a new line'
"
>
...
</SomeOtherComponent>
</template>
<script lang="ts">
...
console.log(import.meta.env.MODE);
- after
stripLiteral
- there are two additional
"
in:some-prop="" ... ""
, which is probably pushing an offset by two later inMagicString.overwrite
.
- there are two additional
<template>
<SomeOtherComponent
:some-prop=""
testValue
? ' '
: ' '
""
>
...
</SomeOtherComponent>
</template>
<script lang=" ">
...
console.log(import.meta.env.MODE);
- after
MagicString.overwrite
<template>
<SomeOtherComponent
:some-prop="
testValue
? 'A long value to break handling of env variables'
: 'it needs to break into a new line'
"
>
...
</SomeOtherComponent>
</template>
<script lang="ts">
...
console.log(im__vite_ssr_import_meta__.envODE);
from vitest.
This is fixed in strip-literal v2.1.0. You can upgrade the transitive dependency either by re-creating lockfile or tools such as pnpm dedupe
.
I confirmed the reproduction is fixed using package.json overrides
:
https://stackblitz.com/edit/vitest-dev-vitest-bifxjk?file=package.json
from vitest.
Related Issues (20)
- Can not connect @vitest/ui HOT 3
- Module transform causes errors HOT 2
- Absolute file path for the filter pattern leads to abnormal behaviour
- browser mode domoperation is invisible HOT 1
- Debugger stops at at end of file when "stepping into" HOT 2
- Incompatible with with Comlink Workers HOT 3
- Fails to run tests with remote modules HOT 2
- ES module import fails with v1.5.0 (but works up to v1.4.0) HOT 7
- Vitest not working with uwebsockets.js HOT 3
- Address already in use :::24678 in monorepo HOT 1
- Add `.next` and `.turbo` to defaultExclude
- Sort file alphabetically with vitest --ui HOT 1
- Add timeout argument to CLI HOT 3
- Error: Nest can't resolve dependencies HOT 3
- Multiple async beforeEach hooks registered at the same scope aren't run serially HOT 2
- instanceof checks fail when using peerDeps? HOT 5
- JUnit reporter: ‘Unknown Error: undefined’
- Following documentation, regarding a tip about `@ts-expect-error`, I cannot make tests behave as I would expect from the documentation HOT 5
- Source maps in inlined deps are off by a few lines in VS Code HOT 11
- Use unique package names for workspaces HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vitest.