Skip to content

Commit

Permalink
refactor: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jan 4, 2024
1 parent b4ae97d commit f077ec4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
8 changes: 4 additions & 4 deletions packages/browser/src/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ResolvedConfig } from 'vitest'
import type { CancelReason, VitestRunner } from '@vitest/runner'
import type { VitestExecutor } from '../../../vitest/src/runtime/execute'
import { createBrowserRunner } from './runner'
import { importId as importIdImpl } from './utils'
import { importId as _importId } from './utils'
import { setupConsoleLogSpy } from './logger'
import { createSafeRpc, rpc, rpcDone } from './rpc'
import { setupDialogsSpy } from './dialog'
Expand All @@ -24,8 +24,8 @@ const url = new URL(location.href)
const testId = url.searchParams.get('id') || 'unknown'
const reloadTries = Number(url.searchParams.get('reloadTries') || '0')

const basePath = () => config!.base! || '/'
const importId = (id: string) => importIdImpl(id, basePath())
const basePath = () => config?.base || '/'
const importId = (id: string) => _importId(id, basePath())
const viteClientPath = () => `${basePath()}@vite/client`

function getQueryPaths() {
Expand Down Expand Up @@ -207,7 +207,7 @@ async function prepareTestEnvironment(config: ResolvedConfig) {

if (!runner) {
const { VitestTestRunner } = await importId('vitest/runners') as typeof import('vitest/runners')
const BrowserRunner = createBrowserRunner(VitestTestRunner, { takeCoverage: () => takeCoverageInsideWorker(config.coverage, executor) }, basePath())
const BrowserRunner = createBrowserRunner(VitestTestRunner, { takeCoverage: () => takeCoverageInsideWorker(config.coverage, executor) })
runner = new BrowserRunner({ config, browserHashMap })
}

Expand Down
9 changes: 5 additions & 4 deletions packages/browser/src/client/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ interface CoverageHandler {
}

export function createBrowserRunner(
original: { new(config: ResolvedConfig): VitestRunner },
VitestRunner: { new(config: ResolvedConfig): VitestRunner },
coverageModule: CoverageHandler | null,
basePath: string,
): { new(options: BrowserRunnerOptions): VitestRunner } {
return class BrowserTestRunner extends original {
return class BrowserTestRunner extends VitestRunner {
public config: ResolvedConfig
hashMap = new Map<string, [test: boolean, timstamp: string]>()

Expand Down Expand Up @@ -71,8 +70,10 @@ export function createBrowserRunner(
this.hashMap.set(filepath, [false, hash])
}

const base = this.config.base || '/'

// on Windows we need the unit to resolve the test file
const prefix = `${basePath}${/^\w:/.test(filepath) ? '@fs/' : ''}`
const prefix = `${base}${/^\w:/.test(filepath) ? '@fs/' : ''}`
const query = `${test ? 'browserv' : 'v'}=${hash}`
const importpath = `${prefix}${filepath}?${query}`.replace(/\/+/g, '/')
await import(importpath)
Expand Down
10 changes: 6 additions & 4 deletions test/browser/specs/fix-4686.test.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// fix #4686

import assert from 'node:assert'
import test from 'node:test'
import runVitest from './run-vitest.mjs'
Expand All @@ -7,12 +9,12 @@ const {
browserResultJson,
passedTests,
failedTests,
} = await runVitest(['--config', 'vitest.config-basepath.mts'])
} = await runVitest(['--config', 'vitest.config-basepath.mts', 'basic.test.ts'])

await test('tests run in presence of config.base', async () => {
assert.ok(browserResultJson.testResults.length === 9, 'Not all the tests have been run')
assert.ok(passedTests.length === 8, 'Some tests failed')
assert.ok(failedTests.length === 1, 'Some tests have passed but should fail')
assert.ok(browserResultJson.testResults.length === 1, 'Not all the tests have been run')
assert.ok(passedTests.length === 1, 'Some tests failed')
assert.ok(failedTests.length === 0, 'Some tests have passed but should fail')

assert.doesNotMatch(stderr, /Unhandled Error/, 'doesn\'t have any unhandled errors')
})
2 changes: 1 addition & 1 deletion test/browser/vitest.config-basepath.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig, mergeConfig } from 'vitest/config'
import baseConfig from './vitest.config.mts'
import baseConfig from './vitest.config.mjs'

export default mergeConfig(baseConfig, defineConfig({ base: '/fix-4686' }))

0 comments on commit f077ec4

Please sign in to comment.