Skip to content

Commit

Permalink
Merge branch 'main' into fix/coverage-vue-ts-html-reporter-path
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Oct 31, 2022
2 parents 6dea179 + 840f5c5 commit bba74a0
Show file tree
Hide file tree
Showing 26 changed files with 264 additions and 105 deletions.
11 changes: 11 additions & 0 deletions packages/coverage-istanbul/src/provider.ts
Expand Up @@ -79,6 +79,8 @@ export class IstanbulCoverageProvider implements CoverageProvider {
return

const sourceMap = pluginCtx.getCombinedSourcemap()
sourceMap.sources = sourceMap.sources.map(removeQueryParameters)

const code = this.instrumenter.instrumentSync(sourceCode, id, sourceMap as any)
const map = this.instrumenter.lastSourceMap() as any

Expand Down Expand Up @@ -227,3 +229,12 @@ function resolveIstanbulOptions(options: CoverageIstanbulOptions, root: string)

return resolved as ResolvedCoverageOptions & { provider: 'istanbul' }
}

/**
* Remove possible query parameters from filenames
* - From `/src/components/Header.component.ts?vue&type=script&src=true&lang.ts`
* - To `/src/components/Header.component.ts`
*/
function removeQueryParameters(filename: string) {
return filename.split('?')[0]
}
31 changes: 14 additions & 17 deletions packages/vite-node/src/client.ts
Expand Up @@ -247,24 +247,21 @@ export class ViteNodeRunner {
return Reflect.get(exports, p, receiver)
},
set(_, p, value) {
// Node also allows access of named exports via exports.default
// https://nodejs.org/api/esm.html#commonjs-namespaces
if (p !== 'default') {
if (!Reflect.has(exports, 'default'))
exports.default = {}

// returns undefined, when accessing named exports, if default is not an object
// but is still present inside hasOwnKeys, this is Node behaviour for CJS
if (exports.default === null || typeof exports.default !== 'object') {
defineExport(exports, p, () => undefined)
return true
}

exports.default[p] = value
defineExport(exports, p, () => value)
if (!Reflect.has(exports, 'default'))
exports.default = {}

// returns undefined, when accessing named exports, if default is not an object
// but is still present inside hasOwnKeys, this is Node behaviour for CJS
if (exports.default === null || typeof exports.default !== 'object') {
defineExport(exports, p, () => undefined)
return true
}
return Reflect.set(exports, p, value)

exports.default[p] = value
if (p !== 'default')
defineExport(exports, p, () => value)

return true
},
})

Expand All @@ -274,7 +271,7 @@ export class ViteNodeRunner {
const moduleProxy = {
set exports(value) {
exportAll(cjsExports, value)
cjsExports.default = value
exports.default = value
},
get exports() {
return cjsExports
Expand Down
20 changes: 13 additions & 7 deletions packages/vite-node/src/utils.ts
@@ -1,4 +1,5 @@
import { fileURLToPath, pathToFileURL } from 'url'
import { existsSync } from 'fs'
import { relative, resolve } from 'pathe'
import type { TransformResult } from 'vite'
import { isNodeBuiltin } from 'mlly'
Expand Down Expand Up @@ -75,13 +76,18 @@ export function pathFromRoot(root: string, filename: string) {
}

export function toFilePath(id: string, root: string): string {
let absolute = id.startsWith('/@fs/')
? id.slice(4)
: id.startsWith(root)
? id
: id.startsWith('/')
? resolve(root, id.slice(1))
: id
let absolute = (() => {
if (id.startsWith('/@fs/'))
return id.slice(4)
if (!id.startsWith(root) && id.startsWith('/')) {
const resolved = resolve(root, id.slice(1))
// The resolved path can have query values. Remove them before checking
// the file path.
if (existsSync(resolved.replace(/\?.*$/, '')))
return resolved
}
return id
})()

if (absolute.startsWith('//'))
absolute = absolute.slice(1)
Expand Down
7 changes: 7 additions & 0 deletions packages/vitest/src/node/cli-wrapper.ts
Expand Up @@ -61,6 +61,13 @@ async function main() {
}
}

// if not specified, don't run through spawn,
// because it prints stderr messages in the wrong order compared to stdout
if (retries <= 0) {
await import('./cli')
return
}

const nodeArgs: string[] = []
const vitestArgs: string[] = []

Expand Down
10 changes: 9 additions & 1 deletion packages/vitest/src/node/cli.ts
@@ -1,3 +1,4 @@
import { normalize } from 'pathe'
import cac from 'cac'
import c from 'picocolors'
import { version } from '../../package.json'
Expand Down Expand Up @@ -92,9 +93,16 @@ async function benchmark(cliFilters: string[], options: CliOptions): Promise<voi
await start('benchmark', cliFilters, options)
}

function normalizeOptions(argv: CliOptions): CliOptions {
argv.root = argv.root && normalize(argv.root)
argv.config = argv.config && normalize(argv.config)
argv.dir = argv.dir && normalize(argv.dir)
return argv
}

async function start(mode: VitestRunMode, cliFilters: string[], options: CliOptions): Promise<void> {
try {
if (await startVitest(mode, cliFilters, options) === false)
if (await startVitest(mode, cliFilters.map(normalize), normalizeOptions(options)) === false)
process.exit()
}
catch (e) {
Expand Down
8 changes: 3 additions & 5 deletions packages/vitest/src/node/logger.ts
Expand Up @@ -47,14 +47,12 @@ export class Logger {
}

private _clearScreen() {
if (!this._clearScreenPending)
if (this._clearScreenPending == null)
return

const log = this._clearScreenPending
this._clearScreenPending = undefined
// equivalent to ansi-escapes:
// stdout.write(ansiEscapes.cursorTo(0, 0) + ansiEscapes.eraseDown + log)
this.console.log(`\u001B[1;1H\u001B[J${log}`)
this.console.log(`\x1Bc${log}`)
}

printError(err: unknown, fullStack = false, type?: string) {
Expand Down Expand Up @@ -84,7 +82,7 @@ export class Logger {
}

printBanner() {
this.log()
this.clearScreen('', true)

const versionTest = this.ctx.config.watch
? c.blue(`v${version}`)
Expand Down

0 comments on commit bba74a0

Please sign in to comment.