Skip to content

Commit

Permalink
fix: support for define global constants (#743)
Browse files Browse the repository at this point in the history
  • Loading branch information
poyoho committed Feb 13, 2022
1 parent 7448e1a commit af9ceab
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 0 deletions.
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: {
},
})

0 comments on commit af9ceab

Please sign in to comment.