Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support for define global constants #743

Merged
merged 9 commits into from Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/vite-node/src/cli.ts
Expand Up @@ -84,6 +84,9 @@ async function run(options: CliOptions = {}) {
},
})

// provide the vite define variable in this context
await runner.executeId('/@vite/env')

for (const file of files)
await runner.executeFile(file)

Expand Down
3 changes: 3 additions & 0 deletions packages/vitest/src/node/execute.ts
Expand Up @@ -14,6 +14,9 @@ export interface ExecuteOptions extends ViteNodeRunnerOptions {
export async function executeInViteNode(options: ExecuteOptions) {
const runner = new VitestRunner(options)

// provide the vite define variable in this context
await runner.executeId('/@vite/env')

const result: any[] = []
for (const file of options.files)
result.push(await runner.executeFile(file))
Expand Down
6 changes: 6 additions & 0 deletions test/coverage-test/src/Defined.vue
@@ -0,0 +1,6 @@
<script setup lang="ts">
const defined = MY_CONSTANT
</script>
<template>
{{ defined }}
</template>
9 changes: 9 additions & 0 deletions test/coverage-test/test/vue.test.ts
Expand Up @@ -5,6 +5,7 @@
import { expect, test } from 'vitest'
import { mount } from '@vue/test-utils'
import Hello from '../src/Hello.vue'
import Defined from '../src/Defined.vue'

test('vue 3 coverage', async() => {
expect(Hello).toBeTruthy()
Expand All @@ -26,3 +27,11 @@ test('vue 3 coverage', async() => {

expect(wrapper.text()).toContain('4 x 4 = 16')
})

test('define package in vm', () => {
expect(Defined).toBeTruthy()

const wrapper = mount(Defined)

expect(wrapper.text()).toContain(MY_CONSTANT)
})
1 change: 1 addition & 0 deletions test/coverage-test/typings.d.ts
@@ -0,0 +1 @@
declare const MY_CONSTANT: string
3 changes: 3 additions & 0 deletions test/coverage-test/vitest.config.ts
Expand Up @@ -5,6 +5,9 @@ export default defineConfig({
plugins: [
vue(),
],
define: {
MY_CONSTANT: '"my constant"',
},
test: {
},
})