mirror of
https://github.com/vitest-dev/vitest.git
synced 2026-02-01 17:36:51 +00:00
fix: support for define global constants (#743)
This commit is contained in:
parent
7448e1a906
commit
af9ceabaef
@ -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)
|
||||
|
||||
|
||||
@ -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))
|
||||
|
||||
6
test/coverage-test/src/Defined.vue
Normal file
6
test/coverage-test/src/Defined.vue
Normal file
@ -0,0 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
const defined = MY_CONSTANT
|
||||
</script>
|
||||
<template>
|
||||
{{ defined }}
|
||||
</template>
|
||||
@ -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()
|
||||
@ -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
test/coverage-test/typings.d.ts
vendored
Normal file
1
test/coverage-test/typings.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
declare const MY_CONSTANT: string
|
||||
@ -5,6 +5,9 @@ export default defineConfig({
|
||||
plugins: [
|
||||
vue(),
|
||||
],
|
||||
define: {
|
||||
MY_CONSTANT: '"my constant"',
|
||||
},
|
||||
test: {
|
||||
},
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user