Skip to content

Commit d1919a0

Browse files
authored
fix: use correct source maps in stacktrace (#2027)
* [vitest] use correct source maps in stacktrace use vitenode server to get a fetch result instead of a transform result. And then get the sourcemap from it. * [vitest] refactor * add tests for stack traces and sourcemaps * commit lockfile * fix linter * fix types linter * remove dynamic content from snapshot * push the correct snapshot * fix tests :) * increase timeout for runner tests * decrease timeout .. not the culprit in CI * move continue below map assignment * guard against accessing vitenode server * reduce the size of snapshots * rename timeout to prevent running more than one
1 parent 53d8f3b commit d1919a0

File tree

13 files changed

+172
-4
lines changed

13 files changed

+172
-4
lines changed

packages/vitest/src/utils/source-map.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ export async function interpretSourcePos(stackFrames: ParsedStack[], ctx: Vitest
2525
for (const frame of stackFrames) {
2626
if ('sourcePos' in frame)
2727
continue
28-
const transformResult = ctx.server.moduleGraph.getModuleById(frame.file)?.ssrTransformResult
29-
if (!transformResult)
28+
const ssrTransformResult = ctx.server.moduleGraph.getModuleById(frame.file)?.ssrTransformResult
29+
const fetchResult = ctx.vitenode?.fetchCache.get(frame.file)?.result
30+
const map = fetchResult?.map || ssrTransformResult?.map
31+
if (!map)
3032
continue
31-
const sourcePos = await getOriginalPos(transformResult.map as any as RawSourceMap | undefined, frame)
33+
const sourcePos = await getOriginalPos(map as any as RawSourceMap, frame)
3234
if (sourcePos)
3335
frame.sourcePos = sourcePos
3436
}

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

test/fails/test/__snapshots__/runner.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ exports[`should fails > nested-suite.test.ts > nested-suite.test.ts 1`] = `"Asse
1414
1515
exports[`should fails > stall.test.ts > stall.test.ts 1`] = `"TypeError: failure"`;
1616
17-
exports[`should fails > timeout.test.ts > timeout.test.ts 1`] = `"Error: Test timed out in 10ms."`;
17+
exports[`should fails > test-timeout.test.ts > test-timeout.test.ts 1`] = `"Error: Test timed out in 10ms."`;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {it, expect} from 'vitest'
2+
3+
export def add(...args)
4+
return args.reduce((do(a, b) a + b), 0)
5+
6+
it "add", do
7+
expect(add()).toBe 0
8+
expect(add(1)).toBe 3
9+
expect(add(1, 2, 3)).toBe 6
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* body */
2+
import { expect, it } from 'vitest'
3+
import { add } from './utils'
4+
5+
it('add', () => {
6+
expect(add()).toBe(100)
7+
expect(add(1)).toBe(1)
8+
return expect(add(1, 2, 3)).toBe(6)
9+
})
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* body */
2+
import { expect, it } from 'vitest'
3+
import { add } from './utils'
4+
5+
interface Num {
6+
count: number
7+
}
8+
9+
const a: Num = { count: 10 }
10+
11+
it('add', () => {
12+
expect(add(a.count)).toBe(100)
13+
expect(add(1)).toBe(1)
14+
return expect(add(1, 2, 3)).toBe(6)
15+
})

test/stacktraces/fixtures/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function add(...args: number[]) {
2+
return args.reduce((a, b) => { return a + b }, 0)
3+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { defineConfig } from 'vite'
2+
3+
export default defineConfig({
4+
plugins: [{
5+
name: 'vite-plugin-imba',
6+
transform(code, id) {
7+
if (id.endsWith('.imba')) {
8+
return {
9+
code:
10+
'\n/*body*/\nimport {it,expect} from \'vitest\';\n\nexport function add(...args){\n\t\n\treturn args.reduce(function(a,b) { return a + b; },0);\n};\n\nit("add",function() {\n\t\n\texpect(add()).toBe(0);\n\texpect(add(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UsQ0FBQyxDQUFFLENBQUMsQ0FBQyxDQUFDLENBQUMsSUFBSSxDQUFDLENBQUMsQ0FBQTtDQUFBLENBSDFCO0FBSUY7In0=',
11+
map: {
12+
version: 3,
13+
file: 'add-in-imba.test.imba',
14+
names: [],
15+
sourceRoot: '',
16+
sources: ['add-in-imba.test.imba'],
17+
sourcesContent: [
18+
'import {it, expect} from \'vitest\'\n\nexport def add(...args)\n\treturn args.reduce((do(a, b) a + b), 0)\n\nit "add", do\n\texpect(add()).toBe 0\n\texpect(add(1)).toBe 3\n\texpect(add(1, 2, 3)).toBe 6\n',
19+
],
20+
mappings:
21+
';;AAAA,MAAM,EAAE,EAAE,CAAE,MAAM,OAAO,QAAQ;;AAEjC,MAAM,CAAC,QAAG,CAAC,GAAG,IAAI,IAAI,CAAC;;CACtB,MAAM,CAAC,IAAI,CAAC,MAAM,CAAE,QAAE,CAAC,CAAC,CAAE,CAAC,IAAE,OAAA,CAAC,CAAC,CAAC,CAAC,CAAC,IAAG,CAAC,CAAC;CAAA;;AAExC,EAAE,CAAC,KAAK,CAAE,QAAE,GAAA;;CACX,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAA;CACpB,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAA;CACrB,OAAA,MAAM,CAAC,GAAG,CAAC,CAAC,CAAE,CAAC,CAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAA;CAAA,CAH1B;AAIF;',
22+
},
23+
}
24+
}
25+
},
26+
}],
27+
test: {
28+
threads: false,
29+
isolate: false,
30+
include: ['**/*.{test,spec}.{imba,js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
31+
},
32+
})

test/stacktraces/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "@vitest/test-fails",
3+
"type": "module",
4+
"private": true,
5+
"scripts": {
6+
"test": "vitest",
7+
"coverage": "vitest run --coverage"
8+
},
9+
"devDependencies": {
10+
"execa": "^6.1.0",
11+
"vitest": "workspace:*"
12+
}
13+
}

0 commit comments

Comments
 (0)