Skip to content

Commit c67fa09

Browse files
committed
fix: quick patch for test locations when source maps are off
1 parent 351906d commit c67fa09

File tree

2 files changed

+49
-29
lines changed

2 files changed

+49
-29
lines changed

src/api/pkg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function resolveVitestConfig(showWarning: boolean, configOrWorkspaceFile: vscode
5050
if (showWarning) {
5151
const isVitestConfig = configOrWorkspaceFile.fsPath.includes('vitest.')
5252
if (isVitestConfig || isVitestInPackageJson(folder.uri.fsPath))
53-
vscode.window.showWarningMessage(`Vitest not found in "${basename(dirname(configOrWorkspaceFile.fsPath))}" folder. Please run \`npm i --save-dev vitest\` to install Vitest.'`)
53+
vscode.window.showWarningMessage(`Vitest not found in "${basename(dirname(configOrWorkspaceFile.fsPath))}" folder. Please run \`npm i --save-dev vitest\` to install Vitest.`)
5454
}
5555
log.error('[API]', `Vitest not found for ${configOrWorkspaceFile}.`)
5656
return null

src/worker/collect.ts

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function astParseFile(filepath: string, code: string) {
7272
verbose(
7373
'Collecting',
7474
filepath,
75-
code, // .replace(/\/\/# sourceMappingURL=data:application\/json;charset=utf-8;base64,[\w+/=]+/, ''),
75+
code,
7676
)
7777
}
7878
else {
@@ -136,8 +136,8 @@ export function astParseFile(filepath: string, code: string) {
136136

137137
let start: number
138138
const end = node.end
139-
// .each
140-
if (callee.type === 'CallExpression') {
139+
// .each or (0, __vite_ssr_exports_0__.test)()
140+
if (callee.type === 'CallExpression' || callee.type === 'SequenceExpression') {
141141
start = callee.end
142142
}
143143
else if (callee.type === 'TaggedTemplateExpression') {
@@ -226,40 +226,37 @@ function serializeError(ctx: WorkspaceProject, error: any): TestError[] {
226226
]
227227
}
228228

229-
export async function astCollectTests(
230-
ctx: WorkspaceProject,
231-
filepath: string,
232-
transformMode: 'web' | 'ssr',
233-
): Promise<ParsedFile> {
234-
const request = await ctx.vitenode.transformRequest(filepath, filepath, transformMode)
235-
// TODO: error cannot parse
236-
const testFilepath = relative(ctx.config.root, filepath)
237-
if (!request) {
238-
debug?.('Cannot parse', testFilepath, '(vite didn\'t return anything)')
239-
return createFailedFileTask(
240-
ctx,
241-
filepath,
242-
new Error(`Failed to parse ${testFilepath}. Vite didn't return anything.`),
243-
)
244-
}
245-
const { definitions, ast } = astParseFile(testFilepath, request.code)
229+
interface ParseOptions {
230+
name: string
231+
filepath: string
232+
allowOnly: boolean
233+
testNamePattern?: RegExp | undefined
234+
}
235+
236+
export function createFileTask(
237+
testFilepath: string,
238+
code: string,
239+
requestMap: any,
240+
options: ParseOptions,
241+
) {
242+
const { definitions, ast } = astParseFile(testFilepath, code)
246243
const file: ParsedFile = {
247-
filepath,
244+
filepath: options.filepath,
248245
type: 'suite',
249-
id: /* @__PURE__ */ generateHash(`${testFilepath}${ctx.config.name || ''}`),
246+
id: /* @__PURE__ */ generateHash(`${testFilepath}${options.name}`),
250247
name: testFilepath,
251248
mode: 'run',
252249
tasks: [],
253250
start: ast.start,
254251
end: ast.end,
255-
projectName: ctx.getName(),
252+
projectName: options.name,
256253
meta: {},
257254
pool: 'browser',
258255
file: null!,
259256
}
260257
file.file = file
261-
const indexMap = createIndexMap(request.code)
262-
const map = request.map && new TraceMap(request.map as any)
258+
const indexMap = createIndexMap(code)
259+
const map = requestMap && new TraceMap(requestMap)
263260
let lastSuite: ParsedSuite = file as any
264261
const updateLatestSuite = (index: number) => {
265262
while (lastSuite.suite && lastSuite.end < index) {
@@ -352,10 +349,10 @@ export async function astCollectTests(
352349
const hasOnly = someTasksAreOnly(file)
353350
interpretTaskModes(
354351
file,
355-
ctx.config.testNamePattern,
352+
options.testNamePattern,
356353
hasOnly,
357354
false,
358-
ctx.config.allowOnly,
355+
options.allowOnly,
359356
)
360357
markDynamicTests(file.tasks)
361358
if (!file.tasks.length) {
@@ -364,14 +361,37 @@ export async function astCollectTests(
364361
errors: [
365362
{
366363
name: 'Error',
367-
message: `No test suite found in file ${filepath}`,
364+
message: `No test suite found in file ${options.filepath}`,
368365
},
369366
],
370367
}
371368
}
372369
return file
373370
}
374371

372+
export async function astCollectTests(
373+
ctx: WorkspaceProject,
374+
filepath: string,
375+
transformMode: 'web' | 'ssr',
376+
): Promise<ParsedFile> {
377+
const request = await ctx.vitenode.transformRequest(filepath, filepath, transformMode)
378+
const testFilepath = relative(ctx.config.root, filepath)
379+
if (!request) {
380+
debug?.('Cannot parse', testFilepath, '(vite didn\'t return anything)')
381+
return createFailedFileTask(
382+
ctx,
383+
filepath,
384+
new Error(`Failed to parse ${testFilepath}. Vite didn't return anything.`),
385+
)
386+
}
387+
return createFileTask(testFilepath, request.code, request.map, {
388+
name: ctx.config.name,
389+
filepath,
390+
allowOnly: ctx.config.allowOnly,
391+
testNamePattern: ctx.config.testNamePattern,
392+
})
393+
}
394+
375395
function createIndexMap(source: string) {
376396
const map = new Map<number, { line: number; column: number }>()
377397
let index = 0

0 commit comments

Comments
 (0)