Skip to content

Commit

Permalink
fix: respect base (#438)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
sheremet-va and antfu committed Jan 4, 2022
1 parent c263790 commit b43a6f4
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 4 deletions.
3 changes: 3 additions & 0 deletions examples/ruby/components/Test.component.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<p>hello world</p>
</template>
17 changes: 17 additions & 0 deletions examples/ruby/config/vite.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"all": {
"sourceCodeDir": "",
"watchAdditionalPaths": []
},
"development": {
"autoBuild": true,
"host": "127.0.0.1",
"publicOutputDir": "vite-development",
"port": 3036
},
"test": {
"autoBuild": true,
"host": "127.0.0.1",
"publicOutputDir": "vite-test"
}
}
19 changes: 19 additions & 0 deletions examples/ruby/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "vitest-test",
"private": true,
"license": "MIT",
"type": "module",
"main": "index.js",
"scripts": {
"test": "vitest",
"test:run": "vitest run"
},
"devDependencies": {
"@vitejs/plugin-vue": "^2.0.1",
"@vue/test-utils": "^2.0.0-rc.18",
"jsdom": "^19.0.0",
"vite-plugin-ruby": "^3.0.4",
"vitest": "workspace:*",
"vue": "3.2.26"
}
}
11 changes: 11 additions & 0 deletions examples/ruby/test/components/Test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { mount } from '@vue/test-utils'
import { describe, expect, it } from 'vitest'
import Test from '../../components/Test.component.vue'

describe('CoachInboxItem', () => {
it('renders', () => {
const wrapper = mount(Test)

expect(wrapper.html()).toContain('hello world')
})
})
10 changes: 10 additions & 0 deletions examples/ruby/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'vite'
import ViteRuby from 'vite-plugin-ruby'
import Vue from '@vitejs/plugin-vue'

export default defineConfig({
plugins: [Vue(), ViteRuby.default()],
test: {
environment: 'jsdom',
},
})
3 changes: 3 additions & 0 deletions packages/vitest/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export function resolveConfig(
root: viteConfig.root,
} as ResolvedConfig

if (viteConfig.base !== '/')
resolved.base = viteConfig.base

resolved.coverage = resolveC8Options(resolved.coverage, resolved.root)

resolved.depsInline = [...resolved.deps?.inline || []]
Expand Down
11 changes: 7 additions & 4 deletions packages/vitest/src/node/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createMocker } from './mocker'

export type FetchFunction = (id: string) => Promise<string | undefined>

export interface ExecuteOptions extends Pick<ResolvedConfig, 'depsInline' | 'depsExternal' | 'fallbackCJS'> {
export interface ExecuteOptions extends Pick<ResolvedConfig, 'depsInline' | 'depsExternal' | 'fallbackCJS' | 'base'> {
root: string
files: string[]
fetch: FetchFunction
Expand Down Expand Up @@ -62,7 +62,7 @@ export async function interpretedImport(path: string, interpretDefault: boolean)
}

export async function executeInViteNode(options: ExecuteOptions) {
const { moduleCache, root, files, fetch, mockMap } = options
const { moduleCache, root, files, fetch, mockMap, base } = options

const externalCache = new Map<string, false | string>()
builtinModules.forEach(m => externalCache.set(m, m))
Expand Down Expand Up @@ -206,7 +206,7 @@ export async function executeInViteNode(options: ExecuteOptions) {
}

async function cachedRequest(rawId: string, callstack: string[]) {
const id = normalizeId(rawId)
const id = normalizeId(rawId, base)

if (externalCache.get(id))
return interpretedImport(patchWindowsImportPath(id), options.interpretDefault)
Expand Down Expand Up @@ -245,7 +245,10 @@ export async function executeInViteNode(options: ExecuteOptions) {
}
}

export function normalizeId(id: string): string {
export function normalizeId(id: string, base?: string): string {
if (base && id.startsWith(base))
id = `/${id.slice(base.length)}`

return id
.replace(/^\/@id\/__x00__/, '\0') // virtual modules start with `\0`
.replace(/^\/@id\//, '')
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/runtime/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async function startViteNode(ctx: WorkerContext) {
depsExternal: config.depsExternal,
fallbackCJS: config.fallbackCJS,
interpretDefault: config.interpretDefault,
base: config.base,
}))[0]

_viteNode = { run, collect }
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ export interface UserConfig extends InlineConfig {
}

export interface ResolvedConfig extends Omit<Required<UserConfig>, 'config' | 'filters' | 'coverage' | 'testNamePattern' | 'related' | 'api'> {
base?: string

config?: string
filters?: string[]
testNamePattern?: RegExp
Expand Down
28 changes: 28 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b43a6f4

Please sign in to comment.