fix: support for define global constants (#743)

This commit is contained in:
yoho 2022-02-13 18:16:44 +08:00 committed by GitHub
parent 7448e1a906
commit af9ceabaef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 0 deletions

View File

@ -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)

View 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))

View File

@ -0,0 +1,6 @@
<script setup lang="ts">
const defined = MY_CONSTANT
</script>
<template>
{{ defined }}
</template>

View File

@ -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
View File

@ -0,0 +1 @@
declare const MY_CONSTANT: string

View File

@ -5,6 +5,9 @@ export default defineConfig({
plugins: [
vue(),
],
define: {
MY_CONSTANT: '"my constant"',
},
test: {
},
})