Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/utils/src/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ export class DecodedMap {
this._decodedMemo = memoizedState()
this.url = from
this.resolvedSources = (sources || []).map(s =>
resolve(s || '', from),
resolve(from, '..', s || ''),
)
}
}
Expand Down
31 changes: 31 additions & 0 deletions packages/vitest/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,37 @@ Repository: egoist/cac

---------------------------------------

## convert-source-map
License: MIT
By: Thorsten Lorenz
Repository: git://github.com/thlorenz/convert-source-map.git

> Copyright 2013 Thorsten Lorenz.
> All rights reserved.
>
> Permission is hereby granted, free of charge, to any person
> obtaining a copy of this software and associated documentation
> files (the "Software"), to deal in the Software without
> restriction, including without limitation the rights to use,
> copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the
> Software is furnished to do so, subject to the following
> conditions:
>
> The above copyright notice and this permission notice shall be
> included in all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
> OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
> NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
> HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
> WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
> OTHER DEALINGS IN THE SOFTWARE.

---------------------------------------

## empathic
License: MIT
By: Luke Edwards
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"@jridgewell/trace-mapping": "catalog:",
"@opentelemetry/api": "^1.9.0",
"@sinonjs/fake-timers": "15.0.0",
"@types/convert-source-map": "^2.0.3",
"@types/estree": "catalog:",
"@types/istanbul-lib-coverage": "catalog:",
"@types/istanbul-reports": "catalog:",
Expand All @@ -210,6 +211,7 @@
"acorn-walk": "catalog:",
"birpc": "catalog:",
"cac": "catalog:",
"convert-source-map": "^2.0.0",
"empathic": "^2.0.0",
"flatted": "catalog:",
"happy-dom": "^20.4.0",
Expand Down
45 changes: 44 additions & 1 deletion packages/vitest/src/node/test-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
} from '@vitest/runner'
import type { TaskEventData, TestArtifact } from '@vitest/runner/types/tasks'
import type { SerializedError } from '@vitest/utils'
import type { SourceMap } from 'rollup'
import type { UserConsoleLog } from '../types/general'
import type { Vitest } from './core'
import type { TestProject } from './project'
Expand All @@ -15,11 +16,13 @@ import type { TestSpecification } from './test-specification'
import type { TestRunEndReason } from './types/reporter'
import assert from 'node:assert'
import { createHash } from 'node:crypto'
import { existsSync } from 'node:fs'
import { existsSync, readFileSync } from 'node:fs'
import { copyFile, mkdir, writeFile } from 'node:fs/promises'
import path from 'node:path'
import { isPrimitive } from '@vitest/utils/helpers'
import { serializeValue } from '@vitest/utils/serialize'
import { parseErrorStacktrace } from '@vitest/utils/source-map'
import convertSourceMap from 'convert-source-map'
import mime from 'mime/lite'
import { basename, extname, resolve } from 'pathe'

Expand Down Expand Up @@ -170,6 +173,18 @@ export class TestRun {
else {
error.stacks = parseErrorStacktrace(error, {
frameFilter: project.config.onStackTrace,
getSourceMap(file) {
// This only handles external modules since
// source map is already applied for inlined modules.
// Module node exists due to Vitest fetch module,
// but transformResult should be empty for external modules.
const mod = project.vite.moduleGraph.getModuleById(file)
if (!mod?.transformResult && existsSync(file)) {
const code = readFileSync(file, 'utf-8')
const result = extractSourcemapFromFile(code, file)
return result
}
},
})
}
})
Expand Down Expand Up @@ -298,3 +313,31 @@ function sanitizeFilePath(s: string): string {
// eslint-disable-next-line no-control-regex
return s.replace(/[\x00-\x2C\x2E\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-')
}

// based on vite
// https://github.com/vitejs/vite/blob/84079a84ad94de4c1ef4f1bdb2ab448ff2c01196/packages/vite/src/node/server/sourcemap.ts#L149
function extractSourcemapFromFile(
code: string,
filePath: string,
): SourceMap | undefined {
const map = (
convertSourceMap.fromSource(code)
|| (convertSourceMap.fromMapFileSource(
code,
createConvertSourceMapReadMap(filePath),
))
)?.toObject()
return map
}

function createConvertSourceMapReadMap(originalFileName: string) {
return (filename: string) => {
// convertSourceMap can detect invalid filename from comments.
// fallback to empty source map to avoid errors.
const targetPath = path.resolve(path.dirname(originalFileName), filename)
if (existsSync(targetPath)) {
return readFileSync(targetPath, 'utf-8')
}
return '{}'
}
}
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

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

8 changes: 4 additions & 4 deletions test/cli/test/__snapshots__/stacktraces.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ Error: __TEST_STACK_TS__

FAIL error-in-package.test.js > transpiled
Error: __TEST_STACK_TRANSPILED__
❯ innerTestStack (NODE_MODULES)/@test/test-dep-error/transpiled.js:7:9
❯ testStack (NODE_MODULES)/@test/test-dep-error/transpiled.js:3:3
❯ innerTestStack (NODE_MODULES)/@test/test-dep-error/transpiled.ts:22:8
❯ testStack (NODE_MODULES)/@test/test-dep-error/transpiled.ts:12:2
❯ error-in-package.test.js:16:22
14|
15| test('transpiled', () => {
Expand All @@ -70,8 +70,8 @@ Error: __TEST_STACK_TRANSPILED__

FAIL error-in-package.test.js > transpiled inline
Error: __TEST_STACK_TRANSPILED_INLINE__
❯ innerTestStack (NODE_MODULES)/@test/test-dep-error/transpiled-inline.js:7:9
❯ testStack (NODE_MODULES)/@test/test-dep-error/transpiled-inline.js:3:3
❯ innerTestStack (NODE_MODULES)/@test/test-dep-error/transpiled-inline.ts:22:8
❯ testStack (NODE_MODULES)/@test/test-dep-error/transpiled-inline.ts:12:2
❯ error-in-package.test.js:20:28
18|
19| test('transpiled inline', () => {
Expand Down
Loading